node-cache-manager-redis-store icon indicating copy to clipboard operation
node-cache-manager-redis-store copied to clipboard

Upgrade redis to v4

Open dantehemerson opened this issue 2 years ago • 65 comments

To use newest types features for typescript and avoid errors with types when using latest 3.x versions

dantehemerson avatar Apr 20 '22 14:04 dantehemerson

Any update on when will this be completed?

sarcastic-verma avatar Jun 28 '22 22:06 sarcastic-verma

+1

vxm5091 avatar Jun 30 '22 02:06 vxm5091

+1

Sravanchittimalla avatar Jul 15 '22 16:07 Sravanchittimalla

+1

celianvdb avatar Jul 18 '22 07:07 celianvdb

This is in the works. Stay tuned.

dabroek avatar Jul 18 '22 12:07 dabroek

Hi, has there been any progress on this?

FoxxMD avatar Sep 01 '22 18:09 FoxxMD

+1

muratdemirci avatar Sep 05 '22 12:09 muratdemirci

+1

tvacherat avatar Sep 26 '22 10:09 tvacherat

+1

andreaziz avatar Oct 11 '22 14:10 andreaziz

+1

smentek avatar Oct 12 '22 18:10 smentek

+1

alibeknow avatar Oct 13 '22 05:10 alibeknow

+1

mehamednews avatar Oct 14 '22 05:10 mehamednews

First of all, thank you all for showing your interest. It encouraged me to finally work on this again.

I am happy to inform you that I have just released v3.0.0 of this package on npm which upgrades the redis dependency to redis@^4.3.1 and adds new TypeScript declarations.

Please let me know if you have any feedback or further requests.

dabroek avatar Oct 15 '22 01:10 dabroek

Is it work now for Nest.js ? image

DmytroHaponovMD avatar Oct 17 '22 11:10 DmytroHaponovMD

@DmytroHaponovMD This can currently be achieved using:

import { redisStore } from 'cache-manager-redis-store';
import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    CacheModule.register({
      // @ts-ignore
      store: async () => await redisStore({
        // Store-specific configuration:
        socket: {
          host: 'localhost',
          port: 6379,
        }
      })
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Unfortunately you are going to need the // @ts-ignore for now, since the interface does not allow Promise<CacheStore> to be returned.

dabroek avatar Oct 17 '22 19:10 dabroek

@DmytroHaponovMD This can currently be achieved using:

import { redisStore } from 'cache-manager-redis-store';
import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    CacheModule.register({
      // @ts-ignore
      store: async () => await redisStore({
        // Store-specific configuration:
        socket: {
          host: 'localhost',
          port: 6379,
        }
      })
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Unfortunately you are going to need the // @ts-ignore for now, since the interface does not allow Promise<CacheStore> to be returned.

this can't work becouse store field is required for option passed in redisStore function

NarHakobyan avatar Oct 20 '22 11:10 NarHakobyan

@DmytroHaponovMD This can currently be achieved using:

import { redisStore } from 'cache-manager-redis-store';
import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    CacheModule.register({
      // @ts-ignore
      store: async () => await redisStore({
        // Store-specific configuration:
        socket: {
          host: 'localhost',
          port: 6379,
        }
      })
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Unfortunately you are going to need the // @ts-ignore for now, since the interface does not allow Promise<CacheStore> to be returned.

this can't work becouse store field is required for option passed in redisStore function

Is it not why // @ts-ignore is there?

smentek avatar Oct 20 '22 12:10 smentek

@DmytroHaponovMD This can currently be achieved using:

import { redisStore } from 'cache-manager-redis-store';
import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    CacheModule.register({
      // @ts-ignore
      store: async () => await redisStore({
        // Store-specific configuration:
        socket: {
          host: 'localhost',
          port: 6379,
        }
      })
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Unfortunately you are going to need the // @ts-ignore for now, since the interface does not allow Promise<CacheStore> to be returned.

this can't work becouse store field is required for option passed in redisStore function

Is it not why // @ts-ignore is there?

// @ts-ignore works for next line, so effected line is store: async () => await redisStore({, but error is inside redisStore, there is another store property sibling of socket.

NarHakobyan avatar Oct 20 '22 12:10 NarHakobyan

@dabroek The DefinitelyTyped typings for this package need to be updated for ^3.0 https://www.npmjs.com/package/@types/cache-manager-redis-store -- this is the root of the error @DmytroHaponovMD is seeing, I believe

FoxxMD avatar Oct 20 '22 12:10 FoxxMD

This seems to be working for me:

@Module({
  imports: [
    ['true', '1', 'yes'].includes(<string>process.env.API_REDIS_STORE_IS_ACTIVE)
      ? CacheModule.register({
          isGlobal: true,
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          store: async () => {
            return await redisStore({
              // Store-specific configuration:
              socket: {
                host: process.env.API_REDIS_HOST,
                port: +(<string>process.env.API_REDIS_PORT),
              },
            });
          },
        })
      : CacheModule.register({ isGlobal: true }),
...

With

"cache-manager": "^5.1.1",
"cache-manager-redis-store": "^3.0.1",

smentek avatar Oct 20 '22 13:10 smentek

I found a workaround to use registerAsync:

import { CacheStore } from '@nestjs/common/cache/interfaces/cache-manager.interface'

CacheModule.registerAsync({
    imports: [ConfigModule],
    useFactory: async (config: ConfigService) =>
      await redisStore({
        socket: {
          host: config.get('REDIS_HOST'),
          port: +config.get('REDIS_PORT'),
        },
      }) as unknown as CacheStore,
    inject: [ConfigService],
  }),

Also:

"cache-manager": "^5.1.1",
"cache-manager-redis-store": "^3.0.1",

assisgui avatar Oct 20 '22 19:10 assisgui

@assisgui same here but had to also configure the ttl:

CacheModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (config: ConfigService) => {
    const store = await redisStore({
      socket: {
        host: config.get('REDIS_HOST'),
        port: +config.get('REDIS_PORT'),
      },
      password: config.get('REDIS_PASSWORD'),
    });

    return {
      store: store as unknown as CacheStore,
      ttl: 60 * 60 * 24 * 7,
    };
  },
  inject: [ConfigService],
}),

cskiwi avatar Oct 21 '22 14:10 cskiwi

Any update on when will this be completed?

SerhiiBabich avatar Oct 22 '22 10:10 SerhiiBabich

Using cache-manager '5.1.1' I was getting the following error:

 TypeError: store.get is not a function

It worked for me after the cache-manager downgrade version to 4.1.0

@Module({
  imports: [
    CacheModule.registerAsync({
      isGlobal: true,
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const store = await redisStore({
          socket: {
            host: config.get('REDIS_HOST'),
            port: +config.get('REDIS_PORT'),
          },
          password: config.get('REDIS_PASSWORD'),
        });
        return {
          store: store as unknown as CacheStore,
          ttl: 5
        };
      }
    })
  ]
})
export class RedisModule {}
"@nestjs/core": "9.0.11",
"cache-manager": "4.1.0",
"cache-manager-redis-store": "3.0.1",

mihailgolban avatar Oct 27 '22 12:10 mihailgolban

Using cache-manager '5.1.1' I was getting the following error:

 TypeError: store.get is not a function

It worked for me after the cache-manager downgrade version to 4.1.0

@Module({
  imports: [
    CacheModule.registerAsync({
      isGlobal: true,
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const store = await redisStore({
          socket: {
            host: config.get('REDIS_HOST'),
            port: +config.get('REDIS_PORT'),
          },
          password: config.get('REDIS_PASSWORD'),
        });
        return {
          store: store as unknown as CacheStore,
          ttl: 5
        };
      }
    })
  ]
})
export class RedisModule {}
"@nestjs/core": "9.0.11",
"cache-manager": "4.1.0",
"cache-manager-redis-store": "3.0.1",

In my case - it doesn't work, and I do not want to check all time is here fix or not. So, the question is still actual:

Any update on when will this be completed?

SerhiiBabich avatar Oct 27 '22 14:10 SerhiiBabich

CacheModule.registerAsync<RedisClientOptions>({
      isGlobal: true,
      imports: [SharedModule],
      inject: [ApiConfigService],
      useFactory: async (configService: ConfigService) => {
        const store = await redisStore({
          socket: {
            host: config.get('REDIS_HOST'),
            port: +config.get('REDIS_PORT'),
          },
          password: config.get('REDIS_PASSWORD'),
        });

        return {
          store: {
          	create: () => store as unknown as CacheStore,
          },
          ttl: 60 * 60 * 24 * 7, // 1 week
        };
      },
    }),
works fine for me, just copy and use above code :)

NarHakobyan avatar Oct 27 '22 20:10 NarHakobyan

Hi @dabroek, any news for the Type, my repo not allow ts ignore lol

conioX avatar Nov 03 '22 00:11 conioX

Just make a PR to this repository, so you can use new typings https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/cache-manager-redis-store

ivansky avatar Nov 03 '22 09:11 ivansky

I have used the example code above but it seems that Nestjs does not use Redis and use in-memory cache instead.

@assisgui same here but had to also configure the ttl:

CacheModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (config: ConfigService) => {
    const store = await redisStore({
      socket: {
        host: config.get('REDIS_HOST'),
        port: +config.get('REDIS_PORT'),
      },
      password: config.get('REDIS_PASSWORD'),
    });

    return {
      store: store as unknown as CacheStore,
      ttl: 60 * 60 * 24 * 7,
    };
  },
  inject: [ConfigService],
}),

Here is my versions

"cache-manager": "^5.1.3",
"cache-manager-redis-yet": "^4.0.0",
"@nestjs/common": "^9.2.0",
"redis": "^4.4.0",

I think caching factory function detects 2 types of store property as shown here

  • an object of store with create function.
  • a function that returns new Redis store object.

So, what I have tried is to return an object CacheStoreFactory with create function, which returns the store object and it work!

CacheModule.registerAsync<RedisClientOptions>({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => {
        const store = await redisStore({
          socket: {
            host: configService.get('REDIS_HOST'),
            port: configService.get('REDIS_PORT'),
          },
        });

        return {
          store: {
            create: () => store,
          },
        };
      },
      inject: [ConfigService],
    })

ntchjb avatar Nov 08 '22 09:11 ntchjb

Thanks @ntchjb mine is now started to use Redis instead in-memory cache, but ttl is not working idk whether it is only me or everyone also experience the same stuff, I only managed to get it working by using the @CacheTTL() decorator

firdisml avatar Nov 09 '22 08:11 firdisml