@sourceloop/cache / Exports / CacheService
Class: CacheService¶
Implements¶
Table of contents¶
Constructors¶
Properties¶
Methods¶
- buildKey
- executeAndSave
- getFromCache
- invalidate
- invalidatePrefix
- invalidateTags
- replacer
- saveInCache
- saveInsertionTimes
- verifyInsertionTime
Constructors¶
constructor¶
• new CacheService(logger, context, configuration, user?)
Parameters¶
| Name | Type |
|---|---|
logger |
ILogger |
context |
Context |
configuration |
ICacheComponentOptions & { ttl: number } |
user? |
IAuthUserWithPermissions<string, string, string> |
Defined in¶
Properties¶
configuration¶
• Private Readonly configuration: ICacheComponentOptions & { ttl: number }
Defined in¶
context¶
• Private Readonly context: Context
Defined in¶
deletionMarkerPrefix¶
• Private Readonly deletionMarkerPrefix: "deletion-marker-"
Defined in¶
insertionMarkerPrefix¶
• Private Readonly insertionMarkerPrefix: "insertion-marker-"
Defined in¶
logger¶
• logger: ILogger
Implementation of¶
Defined in¶
store¶
• Private Readonly store: ICacheStore
Defined in¶
user¶
• Private Optional Readonly user: IAuthUserWithPermissions<string, string, string>
Defined in¶
Methods¶
buildKey¶
▸ buildKey(...args): string
The function takes in any number of arguments, converts them to JSON strings, concatenates them with underscores, hashes the resulting string using SHA256 algorithm, and returns the hash value as a string.
Parameters¶
| Name | Type | Description |
|---|---|---|
...args |
any[] |
args is a rest parameter that allows the function to accept an indefinite number of arguments. These arguments can be of any data type. |
Returns¶
string
The buildKey function is returning a string that is the SHA-256 hash of the concatenated
stringified values of all the arguments passed to the function, separated by underscores.
Implementation of¶
Defined in¶
executeAndSave¶
▸ executeAndSave<T>(fn, args, methodName, prefix, options?, cacheOptions?): Promise<T>
This is an async function that executes a given function, saves the result in cache, and returns the result.
Type parameters¶
| Name |
|---|
T |
Parameters¶
| Name | Type | Description |
|---|---|---|
fn |
CacheMethod<T> |
fn is a function that will be executed and its result will be cached. It is of type CacheMethod<T>, which means it is a function that returns a Promise of type T. |
args |
any[] |
args is an array of arguments that will be passed to the function being executed and cached. |
methodName |
string |
The name of the method being executed and saved in cache. |
prefix |
string |
The prefix parameter is a string that is used as a prefix for the cache key. It helps to differentiate between different types of cached data and avoid key collisions. |
options? |
ICachedMethodOptions |
options is an optional parameter of type ICachedMethodOptions which contains additional options for the cached method. It can include a forceUpdate boolean flag to indicate whether the cached value should be ignored and the method should be executed again, and a tags array to specify tags |
cacheOptions? |
ICacheOptions |
cacheOptions is an optional parameter of type ICacheOptions that can be passed to the executeAndSave method. It is used to specify additional options for caching the result of the method call. These options can include things like the expiration time for the cached value, |
Returns¶
Promise<T>
The function executeAndSave returns a Promise of type T.
Implementation of¶
Defined in¶
getFromCache¶
▸ getFromCache<T>(prefix, key, tags): Promise<undefined | T>
This is an asynchronous function that retrieves data from a cache based on a prefix, key, and tags.
Type parameters¶
| Name |
|---|
T |
Parameters¶
| Name | Type | Description |
|---|---|---|
prefix |
string |
A string that serves as a prefix for the cache key. This helps to organize and differentiate cached data for different purposes or components. |
key |
string |
The key is a string that is used to identify the cached data. It is used along with the prefix and tags to build a unique key for the cached data. When retrieving data from the cache, the key is used to look up the cached data. |
tags |
string[] |
The tags parameter is an array of strings that are used to group related cache entries together. This allows for efficient invalidation of multiple cache entries at once by simply invalidating all entries with a certain tag. |
Returns¶
Promise<undefined | T>
a Promise that resolves to a value of type T or undefined. The type T is a generic
type that can be any type specified by the caller of the function.
Implementation of¶
Defined in¶
invalidate¶
▸ invalidate(prefix, tags): Promise<void>
This function invalidates cache entries based on a given prefix and tags.
Parameters¶
| Name | Type | Description |
|---|---|---|
prefix |
string |
The prefix parameter is a string that represents the prefix of the cache keys that need to be invalidated. |
tags |
string[] |
The tags parameter is an array of strings representing the tags that need to be invalidated. These tags are used to identify cached data that needs to be removed. |
Returns¶
Promise<void>
Implementation of¶
Defined in¶
invalidatePrefix¶
▸ Private invalidatePrefix(prefix): [string, number, number]
Parameters¶
| Name | Type |
|---|---|
prefix |
string |
Returns¶
[string, number, number]
Defined in¶
invalidateTags¶
▸ Private invalidateTags(tags): [string, number, number][]
Parameters¶
| Name | Type |
|---|---|
tags |
string[] |
Returns¶
[string, number, number][]
Defined in¶
replacer¶
▸ Private Readonly replacer(val, cache?): AnyObject | "[Circular]"
Parameters¶
| Name | Type |
|---|---|
val |
AnyObject |
cache? |
WeakSet<AnyObject> |
Returns¶
AnyObject | "[Circular]"
Defined in¶
saveInCache¶
▸ saveInCache<T>(prefix, key, tags, value, options?): Promise<void>
This is an async function that saves a value in cache with a given prefix, key, tags, and options.
Type parameters¶
| Name |
|---|
T |
Parameters¶
| Name | Type | Description |
|---|---|---|
prefix |
string |
A string that will be used as a prefix for the cache key. This can be used to group related cache entries together. |
key |
string |
The key is a string that represents the unique identifier for the cached data. It is used to retrieve the data from the cache later. |
tags |
string[] |
An array of strings that represent tags associated with the cached value. These tags can be used to group and retrieve cached values later. |
value |
T |
The value to be saved in the cache. It can be of any type, as the function is generic and can handle any type of value. |
options? |
ICacheOptions |
ICacheOptions is an optional parameter that can be passed to the function. It is an interface that defines the following properties: |
Returns¶
Promise<void>
Implementation of¶
Defined in¶
saveInsertionTimes¶
▸ Private saveInsertionTimes(key): Promise<void>
Parameters¶
| Name | Type |
|---|---|
key |
string |
Returns¶
Promise<void>
Defined in¶
verifyInsertionTime¶
▸ Private verifyInsertionTime(prefix, key, tags): Promise<boolean>
The function verifyInsertionTime checks if an insertion is valid based on insertion and deletion
markers and tag deletion times.
Parameters¶
| Name | Type | Description |
|---|---|---|
prefix |
string |
The prefix parameter in the verifyInsertionTime function is a string that represents a prefix used in building keys for data storage. |
key |
string |
The key parameter in the verifyInsertionTime function is a string that represents a unique identifier for the data being inserted. It is used to build keys for insertion and deletion markers in the data store. |
tags |
string[] |
The tags parameter in the verifyInsertionTime function is an array of strings representing tags associated with a key. |
Returns¶
Promise<boolean>
The function verifyInsertionTime returns a boolean value. It returns false under the
following conditions:
1. If insertionValue is falsy (null, undefined, 0, false).
2. If any of the tagDeletionTimes is not falsy and greater than or equal to insertionValue.
3. If deletionValue is