file module import { CACHE_MANAGER , Inject , Injectable } from '@nestjs/common' ; import * as schedule from 'node-schedule' ; export interface ICacheManager { store : any ; get ( key ) : any ; set ( key , value , options ? ) : any ; } export type TCacheKey = string ; export type TCacheResult < T > = Promise < T > ; export interface ICacheIoResult < T > { get ( ) : TCacheResult < T > ; update ( ) : TCacheResult < T > ; } export interface ICachePromiseOption < T > { key : TCacheKey ; promise ( ) : TCacheResult < T > ; } export interface ICachePromiseIoOption < T > extends ICachePromiseOption < T > { ioMode ? : boolean ; } export interface ICacheIntervalTimeoutOption { error ? : number ; success ? : number ; } export interface ICacheIntervalTimingOption { error : number ; schedule : any ; } export interface ICacheIntervalOption < T > { key : TCacheKey ; promise ( ) : TCacheResult < T > ; timeout ? : ICacheIntervalTimeoutOption ; timing ? : ICacheIntervalTimingOption ; } export type TCacheIntervalResult < T > = ( ) => TCacheResult < T > ; export interface ICacheIntervalIOOption < T > extends ICacheIntervalOption < T > { ioMode ? : boolean ; } class classdesc example example example example @ Injectable ( ) export class CacheService { private cache ! : ICacheManager ; constructor ( @ Inject ( CACHE_MANAGER ) cache ) { this . cache = cache ; } private get checkCacheServiceAvailable ( ) : boolean { const client = this . cache . store . getClient ( ) ; return client . connected && client . ready ; } public get < T > ( key ) { if ( ! this . checkCacheServiceAvailable ) { return Promise . reject ( '缓存客户端没准备好' ) ; } return this . cache . get ( key ) ; } public set < T > ( key , value , options ? ) { if ( ! this . checkCacheServiceAvailable ) { return Promise . reject ( '缓存客户端没准备好' ) ; } return this . cache . set ( key , value , options ) ; } function description example example promise < T > ( options ) ; promise < T > ( options ) ; promise ( options ) { const { key , promise , ioMode = false } = options ; const promiseTask = ( resolve , reject ) => { return promise ( ) . then ( ( data ) => { this . set ( key , data ) ; resolve ( data ) ; } ) . catch ( reject ) ; } ; const handlePromiseMode = ( ) => { return new Promise ( ( resolve , reject ) => { this . get ( key ) . then ( value => { const isValidValue = value !== null && value !== undefined ; isValidValue ? resolve ( value ) : promiseTask ( resolve , reject ) ; } ) . catch ( reject ) ; } ) ; } ; const handleIoMode = ( ) => ( { get : handlePromiseMode , update : ( ) => new Promise ( promiseTask ) , } ) ; return ioMode ? handleIoMode ( ) : handlePromiseMode ( ) ; } function description example example public interval < T > ( options ) ; public interval < T > ( options ) ; public interval < T > ( options ) { const { key , promise , timeout , timing , ioMode = false } = options ; const promiseTask = ( ) : Promise < T > => { return promise ( ) . then ( ( data ) => { this . set ( key , data ) ; return Promise . resolve ( data ) ; } ) ; } ; if ( timeout ) { const doPromise = ( ) => { promiseTask ( ) . then ( _ => { setTimeout ( doPromise , timeout . success ) ; } ) . catch ( error => { const time = timeout . error || timeout . success ; setTimeout ( doPromise , time ) ; console . warn ( `Redis 超时任务执行失败，${ time }s 后重试：${ error }` ) ; } ) ; } ; doPromise ( ) ; } if ( timing ) { const doPromise = ( ) => { promiseTask ( ) . then ( data => data ) . catch ( error => { console . warn ( `Redis 定时任务执行失败，${ timing . error }s 后重试：${ error }` ) ; setTimeout ( doPromise , timing . error ) ; } ) ; } ; doPromise ( ) ; schedule . scheduleJob ( timing . schedule , doPromise ) ; } const getKeyCache = ( ) => this . get ( key ) ; const handleIoMode = ( ) => ( { get : getKeyCache , update : promiseTask , } ) ; return ioMode ? handleIoMode ( ) : getKeyCache ; } }