import { CACHE_MANAGER , Inject , Injectable , Logger } from '@nestjs/common' ; import { Cache } from 'cache-manager' ; import { Redis } from 'ioredis' ; import { CacheKeys } from '~/constants/cache.constant' ; export type TCacheKey = string ; export type TCacheResult < T > = Promise < T > ; class classdesc example example @ Injectable ( ) export class CacheService { private cache ! : Cache ; private logger = new Logger ( CacheService . name ) ; constructor ( @ Inject ( CACHE_MANAGER ) cache ) { this . cache = cache ; this . redisClient . on ( 'ready' , ( ) => { this . logger . log ( 'Redis 已准备好！' ) ; } ) ; } private get redisClient ( ) : Redis { return this . cache . store . getClient ( ) ; } public get < T > ( key ) { return this . cache . get ( key ) ; } public set < T > ( key , value , options ? ) { return this . cache . set ( key , value , options ) ; } public getClient ( ) { return this . redisClient ; } public clearAggregateCache ( ) { return Promise . all ( [ this . redisClient . del ( CacheKeys . RSS ) , this . redisClient . del ( CacheKeys . RSSXmlCatch ) , this . redisClient . del ( CacheKeys . AggregateCatch ) , this . redisClient . del ( CacheKeys . SiteMapCatch ) , this . redisClient . del ( CacheKeys . SiteMapXmlCatch ) , ] ) ; } }