@nestjs/cache-manager
Advanced tools
Comparing version 2.1.1 to 2.2.0
import { DynamicModule } from '@nestjs/common'; | ||
import type { Cache as CoreCache } from 'cache-manager'; | ||
import { ConfigurableModuleClass } from './cache.module-definition'; | ||
import { CacheModuleAsyncOptions, CacheModuleOptions } from './interfaces/cache-module.interface'; | ||
/** | ||
* This is just the same as the `Cache` interface from `cache-manager` but you can | ||
* use this as a provider token as well. | ||
*/ | ||
export declare abstract class Cache { | ||
} | ||
export interface Cache extends CoreCache { | ||
} | ||
/** | ||
* Module that provides Nest cache-manager. | ||
* | ||
* @see [Caching](https://docs.nestjs.com/techniques/caching) | ||
* | ||
* @publicApi | ||
*/ | ||
export declare class CacheModule extends ConfigurableModuleClass { | ||
/** | ||
* Configure the cache manager statically. | ||
* | ||
* @param options options to configure the cache manager | ||
* | ||
* @see [Customize caching](https://docs.nestjs.com/techniques/caching#customize-caching) | ||
*/ | ||
static register<StoreConfig extends Record<any, any> = Record<string, any>>(options?: CacheModuleOptions<StoreConfig>): DynamicModule; | ||
/** | ||
* Configure the cache manager dynamically. | ||
* | ||
* @param options method for dynamically supplying cache manager configuration | ||
* options | ||
* | ||
* @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration) | ||
*/ | ||
static registerAsync<StoreConfig extends Record<any, any> = Record<string, any>>(options: CacheModuleAsyncOptions<StoreConfig>): DynamicModule; | ||
} |
@@ -9,3 +9,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CacheModule = void 0; | ||
exports.CacheModule = exports.Cache = void 0; | ||
const common_1 = require("@nestjs/common"); | ||
@@ -15,3 +15,25 @@ const cache_constants_1 = require("./cache.constants"); | ||
const cache_providers_1 = require("./cache.providers"); | ||
/** | ||
* This is just the same as the `Cache` interface from `cache-manager` but you can | ||
* use this as a provider token as well. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging | ||
class Cache { | ||
} | ||
exports.Cache = Cache; | ||
/** | ||
* Module that provides Nest cache-manager. | ||
* | ||
* @see [Caching](https://docs.nestjs.com/techniques/caching) | ||
* | ||
* @publicApi | ||
*/ | ||
let CacheModule = class CacheModule extends cache_module_definition_1.ConfigurableModuleClass { | ||
/** | ||
* Configure the cache manager statically. | ||
* | ||
* @param options options to configure the cache manager | ||
* | ||
* @see [Customize caching](https://docs.nestjs.com/techniques/caching#customize-caching) | ||
*/ | ||
static register(options = {}) { | ||
@@ -23,2 +45,10 @@ return { | ||
} | ||
/** | ||
* Configure the cache manager dynamically. | ||
* | ||
* @param options method for dynamically supplying cache manager configuration | ||
* options | ||
* | ||
* @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration) | ||
*/ | ||
static registerAsync(options) { | ||
@@ -37,6 +67,12 @@ const moduleDefinition = super.registerAsync(options); | ||
(0, common_1.Module)({ | ||
providers: [(0, cache_providers_1.createCacheManager)()], | ||
exports: [cache_constants_1.CACHE_MANAGER], | ||
providers: [ | ||
(0, cache_providers_1.createCacheManager)(), | ||
{ | ||
provide: Cache, | ||
useExisting: cache_constants_1.CACHE_MANAGER, | ||
} | ||
], | ||
exports: [cache_constants_1.CACHE_MANAGER, Cache], | ||
}) | ||
], CacheModule); | ||
exports.CacheModule = CacheModule; |
import { Provider } from '@nestjs/common'; | ||
/** | ||
* Creates a CacheManager Provider. | ||
* | ||
* @publicApi | ||
*/ | ||
export declare function createCacheManager(): Provider; |
@@ -8,2 +8,7 @@ "use strict"; | ||
const default_options_1 = require("./default-options"); | ||
/** | ||
* Creates a CacheManager Provider. | ||
* | ||
* @publicApi | ||
*/ | ||
function createCacheManager() { | ||
@@ -23,2 +28,3 @@ return { | ||
} | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
let cache = 'memory'; | ||
@@ -25,0 +31,0 @@ defaultCacheOptions.ttl *= 1000; |
@@ -0,1 +1,14 @@ | ||
/** | ||
* Decorator that sets the caching key used to store/retrieve cached items for | ||
* Web sockets or Microservice based apps. | ||
* | ||
* For example: | ||
* `@CacheKey('events')` | ||
* | ||
* @param key string naming the field to be used as a cache key | ||
* | ||
* @see [Caching](https://docs.nestjs.com/techniques/caching) | ||
* | ||
* @publicApi | ||
*/ | ||
export declare const CacheKey: (key: string) => import("@nestjs/common").CustomDecorator<string>; |
@@ -6,3 +6,16 @@ "use strict"; | ||
const cache_constants_1 = require("../cache.constants"); | ||
/** | ||
* Decorator that sets the caching key used to store/retrieve cached items for | ||
* Web sockets or Microservice based apps. | ||
* | ||
* For example: | ||
* `@CacheKey('events')` | ||
* | ||
* @param key string naming the field to be used as a cache key | ||
* | ||
* @see [Caching](https://docs.nestjs.com/techniques/caching) | ||
* | ||
* @publicApi | ||
*/ | ||
const CacheKey = (key) => (0, common_1.SetMetadata)(cache_constants_1.CACHE_KEY_METADATA, key); | ||
exports.CacheKey = CacheKey; |
import { ExecutionContext } from '@nestjs/common'; | ||
/** | ||
* Decorator that sets the cache ttl setting the duration for cache expiration. | ||
* | ||
* For example: `@CacheTTL(5)` | ||
* | ||
* @param ttl number set the cache expiration time | ||
* | ||
* @see [Caching](https://docs.nestjs.com/techniques/caching) | ||
* | ||
* @publicApi | ||
*/ | ||
type CacheTTLFactory = (ctx: ExecutionContext) => Promise<number> | number; | ||
export declare const CacheTTL: (ttl: number | CacheTTLFactory) => import("@nestjs/common").CustomDecorator<string>; | ||
export {}; |
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common'; | ||
import { HttpAdapterHost, Reflector } from '@nestjs/core'; | ||
import { Observable } from 'rxjs'; | ||
/** | ||
* @see [Caching](https://docs.nestjs.com/techniques/caching) | ||
* | ||
* @publicApi | ||
*/ | ||
export declare class CacheInterceptor implements NestInterceptor { | ||
@@ -5,0 +10,0 @@ protected readonly cacheManager: any; |
@@ -23,2 +23,7 @@ "use strict"; | ||
const cache_constants_1 = require("../cache.constants"); | ||
/** | ||
* @see [Caching](https://docs.nestjs.com/techniques/caching) | ||
* | ||
* @publicApi | ||
*/ | ||
let CacheInterceptor = class CacheInterceptor { | ||
@@ -29,2 +34,4 @@ constructor(cacheManager, reflector) { | ||
this.allowedMethods = ['GET']; | ||
// We need to check if the cache-manager package is v5 or greater | ||
// because the set method signature changed in v5 | ||
const cacheManagerPackage = (0, load_package_util_1.loadPackage)('cache-manager', 'CacheModule', () => require('cache-manager')); | ||
@@ -61,3 +68,3 @@ this.cacheManagerIsv5OrGreater = 'memoryStore' in cacheManagerPackage; | ||
catch (err) { | ||
common_1.Logger.error(`An error has occurred when inserting "key: ${key}", "value: ${response}"`, 'CacheInterceptor'); | ||
common_1.Logger.error(`An error has occurred when inserting "key: ${key}", "value: ${response}"`, err.stack, 'CacheInterceptor'); | ||
} | ||
@@ -64,0 +71,0 @@ })); |
export interface LiteralObject { | ||
[key: string]: any; | ||
} | ||
/** | ||
* Interface defining a cache store. Implement this interface to create a custom | ||
* cache store. | ||
* | ||
* @publicApi | ||
*/ | ||
export interface CacheStore { | ||
/** | ||
* Create a key/value pair in the cache. | ||
* | ||
* @param key cache key | ||
* @param value cache value | ||
*/ | ||
set<T>(key: string, value: T, options?: CacheStoreSetOptions<T> | number): Promise<void> | void; | ||
/** | ||
* Retrieve a key/value pair from the cache. | ||
* | ||
* @param key cache key | ||
*/ | ||
get<T>(key: string): Promise<T | undefined> | T | undefined; | ||
/** | ||
* Destroy a key/value pair from the cache. | ||
* | ||
* @param key cache key | ||
*/ | ||
del?(key: string): void | Promise<void>; | ||
} | ||
export interface CacheStoreSetOptions<T> { | ||
/** | ||
* Time to live - amount of time in seconds that a response is cached before it | ||
* is deleted. Defaults based on your cache manager settings. | ||
*/ | ||
ttl?: ((value: T) => number) | number; | ||
} | ||
/** | ||
* Interface defining a factory to create a cache store. | ||
* | ||
* @publicApi | ||
*/ | ||
export type CacheStoreFactory = { | ||
/** | ||
* Return a configured cache store. | ||
* | ||
* @param args Cache manager options received from `CacheModule.register()` | ||
* or `CacheModule.registerAsync()` | ||
*/ | ||
create(args: LiteralObject): CacheStore; | ||
} | ((args: LiteralObject) => CacheStore | Promise<CacheStore>); | ||
/** | ||
* Interface defining Cache Manager configuration options. | ||
* | ||
* @publicApi | ||
*/ | ||
export interface CacheManagerOptions { | ||
/** | ||
* Cache storage manager. Default is `'memory'` (in-memory store). See | ||
* [Different stores](https://docs.nestjs.com/techniques/caching#different-stores) | ||
* for more info. | ||
*/ | ||
store?: string | CacheStoreFactory | CacheStore; | ||
/** | ||
* Time to live - amount of time that a response is cached before it | ||
* is deleted. Subsequent request will call through the route handler and refresh | ||
* the cache. Defaults to 5 seconds. In `cache-manager@^4` this value is in seconds. | ||
* In `cache-manager@^5` this value is in milliseconds. | ||
*/ | ||
ttl?: number; | ||
/** | ||
* Maximum number of responses to store in the cache. Defaults to 100. | ||
*/ | ||
max?: number; | ||
isCacheableValue?: (value: any) => boolean; | ||
} |
import { ConfigurableModuleAsyncOptions, Provider, Type } from '@nestjs/common'; | ||
import { CacheManagerOptions } from './cache-manager.interface'; | ||
export type CacheModuleOptions<StoreConfig extends Record<any, any> = Record<string, any>> = CacheManagerOptions & StoreConfig & { | ||
/** | ||
* If "true', register `CacheModule` as a global module. | ||
*/ | ||
isGlobal?: boolean; | ||
}; | ||
/** | ||
* Interface describing a `CacheOptionsFactory`. Providers supplying configuration | ||
* options for the Cache module must implement this interface. | ||
* | ||
* @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration) | ||
* | ||
* @publicApi | ||
*/ | ||
export interface CacheOptionsFactory<StoreConfig extends Record<any, any> = Record<string, any>> { | ||
createCacheOptions(): Promise<CacheModuleOptions<StoreConfig>> | CacheModuleOptions<StoreConfig>; | ||
} | ||
/** | ||
* Options for dynamically configuring the Cache module. | ||
* | ||
* @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration) | ||
* | ||
* @publicApi | ||
*/ | ||
export interface CacheModuleAsyncOptions<StoreConfig extends Record<any, any> = Record<string, any>> extends ConfigurableModuleAsyncOptions<CacheModuleOptions<StoreConfig>, keyof CacheOptionsFactory> { | ||
/** | ||
* Injection token resolving to an existing provider. The provider must implement | ||
* the `CacheOptionsFactory` interface. | ||
*/ | ||
useExisting?: Type<CacheOptionsFactory<StoreConfig>>; | ||
/** | ||
* Injection token resolving to a class that will be instantiated as a provider. | ||
* The class must implement the `CacheOptionsFactory` interface. | ||
*/ | ||
useClass?: Type<CacheOptionsFactory<StoreConfig>>; | ||
/** | ||
* Function returning options (or a Promise resolving to options) to configure the | ||
* cache module. | ||
*/ | ||
useFactory?: (...args: any[]) => Promise<CacheModuleOptions<StoreConfig>> | CacheModuleOptions<StoreConfig>; | ||
/** | ||
* Dependencies that a Factory may inject. | ||
*/ | ||
inject?: any[]; | ||
/** | ||
* Extra providers to be registered within a scope of this module. | ||
*/ | ||
extraProviders?: Provider[]; | ||
/** | ||
* If "true', register `CacheModule` as a global module. | ||
*/ | ||
isGlobal?: boolean; | ||
} |
{ | ||
"name": "@nestjs/cache-manager", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Nest - modern, fast, powerful node.js web framework (@cache-manager)", | ||
@@ -22,24 +22,24 @@ "author": "Kamil Mysliwiec", | ||
"devDependencies": { | ||
"@commitlint/cli": "18.2.0", | ||
"@commitlint/config-angular": "18.1.0", | ||
"@nestjs/common": "10.2.7", | ||
"@nestjs/core": "10.2.7", | ||
"@nestjs/platform-express": "10.2.7", | ||
"@nestjs/testing": "10.2.7", | ||
"@types/jest": "29.5.7", | ||
"@types/node": "20.8.10", | ||
"@types/supertest": "2.0.15", | ||
"@typescript-eslint/eslint-plugin": "6.9.1", | ||
"@typescript-eslint/parser": "6.9.1", | ||
"cache-manager": "5.2.4", | ||
"@commitlint/cli": "18.4.4", | ||
"@commitlint/config-angular": "18.4.4", | ||
"@nestjs/common": "10.3.0", | ||
"@nestjs/core": "10.3.0", | ||
"@nestjs/platform-express": "10.3.0", | ||
"@nestjs/testing": "10.3.0", | ||
"@types/jest": "29.5.11", | ||
"@types/node": "20.10.7", | ||
"@types/supertest": "6.0.2", | ||
"@typescript-eslint/eslint-plugin": "6.18.0", | ||
"@typescript-eslint/parser": "6.18.0", | ||
"cache-manager": "5.3.2", | ||
"cache-manager-redis-store": "^3.0.1", | ||
"eslint": "8.52.0", | ||
"eslint-config-prettier": "9.0.0", | ||
"eslint-plugin-import": "2.29.0", | ||
"eslint": "8.56.0", | ||
"eslint-config-prettier": "9.1.0", | ||
"eslint-plugin-import": "2.29.1", | ||
"husky": "8.0.3", | ||
"jest": "29.7.0", | ||
"lint-staged": "15.0.2", | ||
"prettier": "3.0.3", | ||
"reflect-metadata": "0.1.13", | ||
"release-it": "16.2.1", | ||
"lint-staged": "15.2.0", | ||
"prettier": "3.1.1", | ||
"reflect-metadata": "0.1.14", | ||
"release-it": "17.0.1", | ||
"rimraf": "5.0.5", | ||
@@ -49,3 +49,3 @@ "rxjs": "7.8.1", | ||
"ts-jest": "29.1.1", | ||
"typescript": "5.2.2" | ||
"typescript": "5.3.3" | ||
}, | ||
@@ -52,0 +52,0 @@ "peerDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30762
640