file module import { CacheModuleOptions , CacheOptionsFactory , Injectable } from '@nestjs/common' ; import * as redisStore from 'cache-manager-redis-store' ; import * as APP_CONFIG from '../app.config' ; @ Injectable ( ) export class CacheConfigService implements CacheOptionsFactory { public retryStrategy ( ) { return { retry_strategy : ( options ) => { if ( options . error && options . error . code === 'ECONNREFUSED' ) { console . warn ( 'Redis 连接出了问题：' , options . error ) ; return new Error ( 'Redis 服务器拒绝连接' ) ; } if ( options . total_retry_time > 1000 * 60 ) { return new Error ( '重试时间已用完' ) ; } if ( options . attempt > 6 ) { return new Error ( '尝试次数已达极限' ) ; } return Math . min ( options . attempt * 100 , 3000 ) ; } , } ; } public createCacheOptions ( ) { return { store : redisStore , ... APP_CONFIG . REDIS } ; } }