|
| 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 | +} |
0 commit comments