Skip to content

Commit f3d3580

Browse files
committed
feat: async cache
1 parent a0635b1 commit f3d3580

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

src/core/cache/async/interface.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*!
2+
* V4Fire Core
3+
* https://github.com/V4Fire/Core
4+
*
5+
* Released under the MIT license
6+
* https://github.com/V4Fire/Core/blob/master/LICENSE
7+
*/
8+
9+
import type { ClearFilter } from 'core/cache/interface';
10+
11+
/**
12+
* Base interface for an async cache data structure
13+
*
14+
* @typeparam V - value type
15+
* @typeparam K - key type (`string` by default)
16+
*/
17+
export default interface AsyncCache<V = unknown, K = string> {
18+
/**
19+
* Number of elements within the cache
20+
*/
21+
readonly size: Promise<number>;
22+
23+
/**
24+
* Returns true if a value by the specified key exists in the cache
25+
* @param key
26+
*/
27+
has(key: K): Promise<boolean>;
28+
29+
/**
30+
* Returns a value from the cache by the specified key
31+
* @param key
32+
*/
33+
get(key: K): Promise<CanUndef<V>>;
34+
35+
/**
36+
* Saves a value to the cache by the specified key
37+
*
38+
* @param key
39+
* @param value
40+
* @param opts
41+
*/
42+
set(key: K, value: V, opts?: {}): Promise<V>;
43+
44+
/**
45+
* Removes a value from the cache by the specified key
46+
* @param key
47+
*/
48+
remove(key: K): Promise<CanUndef<V>>;
49+
50+
/**
51+
* Clears the cache by the specified filter and returns a map of removed keys
52+
* @param [filter] - filter for removing (if not specified, then all cache values will be removed)
53+
*/
54+
clear(filter?: ClearFilter<V, K>): Promise<Map<K, V>>;
55+
56+
/**
57+
* Returns an iterator by the cache keys
58+
*/
59+
[Symbol.asyncIterator](): AsyncIterableIterator<K>;
60+
61+
/**
62+
* Returns an iterator by the cache keys
63+
*/
64+
keys(): AsyncIterableIterator<K>;
65+
66+
/**
67+
* Returns an iterator by the cache values
68+
*/
69+
values(): AsyncIterableIterator<V>;
70+
71+
/**
72+
* Returns an iterator from the cache that produces pairs of keys and values
73+
*/
74+
entries(): AsyncIterableIterator<[K, V]>;
75+
}

src/core/cache/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
export * from 'core/cache/interface';
1515

1616
export { default as AbstractCache } from 'core/cache/interface';
17+
export { default as AbstractAsyncCache } from 'core/cache/async/interface';
1718
export { default as Cache } from 'core/cache/simple';
1819
export { default as RestrictedCache } from 'core/cache/restricted';
1920
export { default as NeverCache } from 'core/cache/never';

src/core/request/interface.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import type { EventEmitter2 as EventEmitter } from 'eventemitter2';
10-
import type { AbstractCache } from 'core/cache';
10+
import type { AbstractCache, AbstractAsyncCache } from 'core/cache';
1111

1212
import type Data from 'core/data';
1313
import type { ModelMethod } from 'core/data';
@@ -43,7 +43,8 @@ export type CacheStrategy =
4343
'forever' |
4444
'never' |
4545
AbstractCache |
46-
Promise<AbstractCache>;
46+
Promise<AbstractCache> |
47+
AbstractAsyncCache;
4748

4849
export type CacheType =
4950
'memory' |

0 commit comments

Comments
 (0)