老师你好 cache-manager-ioredis-yet 提示废弃了要换哪个包

来源:8-5 nestjs集成cache-manager与ioredis

春去_秋来

2025-02-13

老师你好 cache-manager-ioredis-yet 提示废弃了要换哪个包?

图片描述

图片描述

写回答

1回答

Brian

2025-02-14

是的,这个地方变成keyv了

最新代码:

import { CacheModule } from '@nestjs/cache-manager';
import { Module, Global } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import { Keyv } from 'keyv';
import { createKeyv } from '@keyv/redis';
import { CacheableMemory } from 'cacheable';

@Global()
@Module({
  imports: [
    CacheModule.registerAsync({
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const cacheType = configService.get<string>('CACHE_TYPE', 'memory');

        if (cacheType === 'redis') {
          const redisHost = configService.get<string>(
            'REDIS_HOST',
            'localhost',
          );
          const redisPort = configService.get<number>('REDIS_PORT', 6379);
          const redisPassword = configService.get<string>(
            'REDIS_PASSWORD',
            'example',
          );
          const redisUrl = `redis://${redisHost}:${redisPort}`;
          return {
            stores: [
              createKeyv({
                url: redisUrl,
                password: redisPassword,
              }),
            ],
          };
        } else {
          return {
            stores: [
              new Keyv({
                store: new CacheableMemory({
                  ttl: configService.get<number>('CACHE_TTL', 30 * 1000), // milliseconds
                  lruSize: configService.get<number>('CACHE_MAX_ITEMS', 100), // maximum number of items in cache
                }),
              }),
            ],
          };
        }
      },
    }),
  ],
  exports: [CacheModule],
})
export class CacheCommonModule {}


官方地址:https://docs.nestjs.com/techniques/caching#interacting-with-the-cache-store



0
0

NestJS 从拔高到精通,大型复杂业务架构落地实践

NestJS 从拔高到精通,大型复杂业务架构落地实践

163 学习 · 46 问题

查看课程