Class: Cache<T>
The Cache class is an implementable abstract class for storing values to be recalled later.
See
Type parameters
| Name | Type | Description |
|---|---|---|
T | any | The type of the values stored in the cache. |
Hierarchy
Cache
Table of contents
Constructors
Properties
Methods
Constructors
constructor
• new Cache<T>(options)
Constructor for the Cache class.
Type parameters
| Name | Type | Description |
|---|---|---|
T | any | The type of the values stored in the cache. |
Parameters
| Name | Type | Description |
|---|---|---|
options | CacheOptions<T> | The custom options for the Cache, type CacheOptions. |
See
Defined in
packages/api/src/cache/Cache.ts:67
Properties
policy
• Readonly policy: CachePolicy<T>
The CachePolicy that this Cache is using.
Defined in
packages/api/src/cache/Cache.ts:57
Methods
access
▸ Abstract access(key): undefined | CacheItem<T> | Promise<undefined | CacheItem<T>>
Access a given key in the Cache directly, bypassing the CachePolicy. This is primarily used by the garbage collector to check if values are stale, but may be useful in other applications as well. Unlike get, this method returns the entire CacheItem. If you just want the value, use get instead.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key to access in the Cache. |
Returns
undefined | CacheItem<T> | Promise<undefined | CacheItem<T>>
The CacheItem stored at the given key, if it exists, or undefined if it does not. May also return a Promise that resolves to the CacheItem, or undefined if it does not exist.
Defined in
packages/api/src/cache/Cache.ts:166
delete
▸ Abstract delete(key): void | Promise<void>
Delete the value at a given key from the Cache, such that a subsequent call to get or access would not return the value.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key of the value to delete from the Cache. |
Returns
void | Promise<void>
Either void or a Promise of void that resolves when the key has successfully been deleted from the cache.
Defined in
packages/api/src/cache/Cache.ts:175
destroy
▸ destroy(): void
Destroy this Cache instance, primarily by shutting down the garbage collector. Failure to do so may leave your program hanging if you do not explicitly interrupt it.
Returns
void
Defined in
packages/api/src/cache/Cache.ts:148
garbageCollect
▸ Protected garbageCollect(): Promise<void>
A function responsible for cleaning up dead Cache values, usually with the help of policy. By default, this entails iterating all values in the cache and checking if they are still valid according to the CachePolicy. If not, they are deleted from the cache using delete.
Returns
Promise<void>
a Promise that resolves after all dead values have been deleted from the cache, i.e. get, and access, and keys will no longer return them unless they're added again.
Defined in
packages/api/src/cache/Cache.ts:133
get
▸ get(key): Promise<null | T>
Get the value of a given key from the cache, if it still exists.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | Key that was used in the call to write instructing the Cache on where to store the value. |
Returns
Promise<null | T>
A Promise that resolves to the value of type T stored in the cache at the given key, if it still exists and is valid according to the CachePolicy. If the value does not exist, null will be returned.
See
Defined in
packages/api/src/cache/Cache.ts:96
▸ get(key, fn): Promise<T>
Get the value of a given key from the cache, if it exists, or call the passed generator function if not.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | Key that was used in the call to write instructing the Cache on where to store the value. |
fn | () => T | Promise<T> | If the value does not exist in the cache, this function will be called to generate the value to store in the cache. This function can return the value directly or via a Promise. |
Returns
Promise<T>
A Promise that resolves to the value of type T stored in the cache at the given key if it exists. If it does not exist, the value returned from fn is returned.
Defined in
packages/api/src/cache/Cache.ts:105
keys
▸ Abstract keys(): string[] | Promise<string[]>
Retrieve a list of all currently-stored keys stored in the Cache. These keys do not necessarily have to be associated with a still-valid value. In fact, this method is used by garbageCollect to iterate over the Cache and find stale values.
Returns
string[] | Promise<string[]>
An Array of strings where each value corresponds to a key in the Cache. If the Cache is empty, an empty Array will be returned. May also return a Promise that resolves to an Array of strings.
Defined in
packages/api/src/cache/Cache.ts:184
write
▸ Abstract write(key, value): void | Promise<void>
Write a value to the cache. This inserts a CacheItem value into the Cache with the created value being set to the time of calling this method.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key to write the value to in the cache. This key can later be used to retrieve the value from the cache, if it's still valid according to the CachePolicy. Generally these keys should be unique for each value stored in the cache. |
value | T | Value to write to the cache at the given key. Must be of type T. |
Returns
void | Promise<void>
Either void or a Promise of void that resolves when the value has successfully been added to the cache.
See
Starboard