NestJS SDK Cache utilities
MultiversX NestJS Microservice Cache Utilities
This package contains a set of utilities commonly used for caching purposes in the MultiversX Microservice ecosystem.
Installation
sdk-nestjs-cache
is delivered via npm and it can be installed as follows:
npm install @multiversx/sdk-nestjs-cache
Utility
The package exports two services: an in-memory cache service and remote cache service.
Table of contents
- In Memory Cache - super fast in-memory caching system based on LRU cache
- Redis Cache - Caching system based on Redis
- Cache Service - MultiversX caching system which combines in-memory and Redis cache, forming a two-layer caching system
In memory cache
In memory cache, available through InMemoryCacheService
, is used to read and write data from and into the memory storage of your Node.js instance.
Note that if you have multiple instances of your application you must sync the local cache across all your instances.
Let's take as an example a ConfigService
that loads some non-crucial configuration from the database and can be cached for 10 seconds.
Usage example:
import { Injectable } from '@nestjs/common';
import { InMemoryCacheService } from '@multiversx/sdk-nestjs-cache';
@Injectable()
export class ConfigService {
constructor(
private readonly inMemoryCacheService: InMemoryCacheService
){}
async loadConfiguration(){
return await this.inMemoryCacheService.getOrSet(
'configurationKey',
() => this.getConfigurationFromDb(),
10, // 10 seconds
)
}
private async getConfigurationFromDb(){
// fetch configuration from db
}
}
When the .loadConfiguration()
method is called for the first time, the .getConfigurationFromDb()
method will be executed and the value returned from it will be set in cache with configurationKey
key. If another .loadConfiguration()
call comes in 10 seconds interval, the data will be returned from cache and .getConfigurationFromDb()
will not be executed again.
In memory cache methods
.get<T>(key: string): Promise<T | undefined>
- Parameters:
key
: The key of the item to retrieve from the cache.
- Returns: A
Promise
that resolves to the cached value orundefined
if the key is not present.
.getMany<T>(keys: string[]): Promise<(T | undefined)[]>
- Parameters:
keys
: An array of keys to retrieve from the cache.
- Returns: A
Promise
that resolves to an array of cached values corresponding to the provided keys.
.set<T>(key: string, value: T, ttl: number, cacheNullable?: boolean): void
- Parameters:
key
: The key under which to store the value.value
: The value to store in the cache.ttl
: Time-to-live for the cached item in seconds.cacheNullable
(optional): If set tofalse
, the method will not cache null or undefined values. Default:true
.setMany<T>(keys: string[], values: T[], ttl: number, cacheNullable?: boolean): Promise<void>
- Parameters:
keys
: An array of keys under which to store the values.values
: An array of values to store in the cache.ttl
: Time-to-live for the cached items in seconds.cacheNullable
(optional): If set tofalse
, the method will not cache null or undefined values. Default:true
.delete(key: string): Promise<void>
- Parameters:
key
: The key of the item to delete from the cache.
- Returns: A
Promise
that resolves when the item is successfully deleted.
.getOrSet<T>(key: string, createValueFunc: () => Promise<T>, ttl: number, cacheNullable?: boolean): Promise<T>
- Parameters:
key
: The key of the item to retrieve or create.createValueFunc
: A function that creates the value if the key is not present in the cache.ttl
: Time-to-live for the cached item in seconds.cacheNullable
(optional): If set tofalse
, the method will not cache null or undefined values. Default:true
- Returns: A
Promise
that resolves to the cached or newly created value.
.setOrUpdate<T>(key: string, createValueFunc: () => Promise<T>, ttl: number, cacheNullable?: boolean): Promise<T>
- Parameters:
key
: The key of the item to update or create.createValueFunc
: A function that creates the new value for the key.ttl
: Time-to-live for the cached item in seconds.cacheNullable
(optional): If set tofalse
, the method will not cache null or undefined values. Default:true
- Returns: A
Promise
that resolves to the updated or newly created value.