@mcsb/api / Exports / InMemoryCache
Class: InMemoryCache<T>
In-memory implementation of Cache. This is likely sufficient for most Cache
implementations, however larger applications may benefit from an external or more complex caching system.
Type parameters
Name | Description |
---|---|
T | Type of the values stored in the Cache . |
Hierarchy
Cache
<T
>↳
InMemoryCache
Table of contents
Constructors
Properties
Methods
Constructors
constructor
• new InMemoryCache<T
>(options
)
Constructor for the Cache
class.
Type parameters
Name | Description |
---|---|
T | 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
Inherited from
Defined in
packages/api/src/cache/Cache.ts:67
Properties
policy
• Readonly
policy: CachePolicy
<T
>
The CachePolicy that this Cache
is using.
Inherited from
Defined in
packages/api/src/cache/Cache.ts:57
Methods
access
▸ access(key
): 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
>
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.
Overrides
Defined in
packages/api/src/cache/InMemoryCache.ts:40
delete
▸ delete(key
): 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
Either void or a Promise of void that resolves when the key has successfully been deleted from the cache.
Overrides
Defined in
packages/api/src/cache/InMemoryCache.ts:52
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
Inherited from
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.
Inherited from
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
Inherited from
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.
Inherited from
Defined in
packages/api/src/cache/Cache.ts:105
keys
▸ keys(): 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
[]
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.
Overrides
Defined in
packages/api/src/cache/InMemoryCache.ts:64
write
▸ write(key
, value
): 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
Either void or a Promise of void that resolves when the value has successfully been added to the cache.
See