/
index.ts
62 lines (57 loc) · 1.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { HeapCache } from './Heap';
import { MemoryCache } from './Memory';
export interface CachingStrategy extends GenericCachingStrategy<string> {}
export interface GenericCachingStrategy<T> {
/**
* Returns the cached value, or undefined if it doesn't exist
*/
get(key: string): T | undefined;
/**
* Sets the value in the cache, with an optional expiration (as a future Game.time)
*/
set(key: string, value: T, expiration?: number): void;
/**
* Returns the expiration time, or undefined if it doesn't exist
*/
expires(key: string): number | undefined;
/**
* Deletes the key from the cache
*/
delete(key: string): void;
/**
* Wraps the cache in a serializer to store objects more compactly.
* HeapCache notably needs no serializers and simply ignores this.
*/
with<T>(serializer: Serializer<T>): GenericCachingStrategy<T>;
/**
* Cleans up any expired cache values
*/
clean(): void;
}
export interface Serializer<T> {
/**
* Unique key used internally for caching deserialized values
*/
key: string;
/**
* If unable to serialize value, serializer should throw an error; this
* will prevent the cached item from being created
*/
serialize(value?: T): string | undefined;
/**
* If unable to deserialize value, deserializer should throw an error; this
* will prevent the cached item from being created
*/
deserialize(value?: string): T | undefined;
}
export * from './Serializers/MoveTarget';
export * from './Serializers/Number';
export * from './Serializers/RoomPosition';
export function cleanAllCaches() {
MemoryCache.clean();
HeapCache.clean();
}
export const CachingStrategies = {
HeapCache,
MemoryCache
};