Coder Social home page Coder Social logo

node-cache-manager-redis-store's People

Contributors

dabroek avatar dependabot[bot] avatar homerjam avatar onichandame avatar sebelga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

node-cache-manager-redis-store's Issues

Cannot get cached value which has wrapped function

Thanks for you works, this module is helpful for me. When I use both of methods wrap and get, I feel uncertain in under case:

const cacheManager = require('cache-manager')
const redisStore = require('cache-manager-store-redis')

const cache = cacheManager.caching({ store: redisStore })

;(async () => {
  const i = await cache.wrap('k', async () => '100')
  console.log(i)    // 100
  console.log(await cache.get('k')) // Expect 100 but undefined

  process.exit()
})()

Its behavior is different with cache-manager's default memory storage.

const cacheManager = require('cache-manager')

const cache = cacheManager.caching({ store: 'memory', max: 100, ttl: 10 * 60 })

;(async () => {
  const i = await cache.wrap('k', async () => '100')
  console.log(i)    // 100
  console.log(await cache.get('k')) // 100

  process.exit()
})()

It is bug? 😕

Precisions

I have 2 questions.

  • The example of Multi-store, the ttl are in seconds?
  • If we need to store 3 different data (including 2 different delays), what is the best to do?
    • 3 stores, or prefix Id?
    • Impact to redis connection(s)

Support for cluster mode

Since this package is a wraper for node-redis is it possible to config a module in cluster mode?

The node-redis docs have this example:

Connecting to a cluster is a bit different. Create the client by specifying some (or all) of the nodes in your cluster and then use it like a regular client instance:

import { createCluster } from 'redis';

const cluster = createCluster({
  rootNodes: [
    {
      url: 'redis://10.0.0.1:30001'
    },
    {
      url: 'redis://10.0.0.2:30002'
    }
  ]
});

cluster.on('error', (err) => console.log('Redis Cluster Error', err));

await cluster.connect();

await cluster.set('key', 'value');
const value = await cluster.get('key');

Is it possible to do that ? If not, is it possible to add support for this in the lib?

How to cache queries?

Hi,
this is for sure not an issue, just me not knowing how to use the lib correctly but not sure where to ask besides here... hope it's ok. For some reason I have no problem getting keys (gck:xxx) into Redis but cannot get queries (gcq:xxx) to be cached.
(Part of) my test code is as follows:

const NsqlCache = require('nsql-cache'); 
const dsAdapter = require('nsql-cache-datastore'); 
const redisStore = require('cache-manager-redis-store');
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();

const REDISHOST = process.env.REDISHOST || 'localhost';
const REDISPORT = process.env.REDISPORT || 6379;

const cache = new NsqlCache({
    db: dsAdapter(datastore),
    stores: [{
        store: redisStore,
        host: REDISHOST, 
        port: REDISPORT
    }],
    config: {
        ttl: {
            keys: 60 * 60, // 1 hour
            queries: 0// infinite
        }
    },
});

ProviderController.list = async function (req, res) {
    try {
       const providerKey = datastore.key(['V2Provider', '04cb463f-e567-41da-810c-6b918b0130a1']);
       const query = datastore.createQuery('V2Provider');
       await datastore.get(providerKey); // works fine, stored in Redis DB
       const [provs] = await datastore.runQuery(query); //query runs fine but nothing stored in Redis :-(
       console.log(provs.length); //60 or so objects, not too much...
        res.send({testrun:'ok'});
    } catch (error) {
        console.log(error);
    }
};

I've been through all documentation (afaik) but cannot find what is missing here, so any help would be appreciated!

Add the posssibility to pass a redis client to the constructor

Hi,

I'm trying to use this library to implement dynamic cache objects that get re-allocated during the runtime. Because of this connections are leaking (clients are never .quit()).

Passing a Redis client in the constructor would allow me to create it on the module scope and guarantee that the same one is used during all the runtime. I don't think it would introduce any breaking changes in the library.

If you find this idea strange I've asked on the node_redis repo before and they also suggested using a top-level client redis/node-redis#1338

I can open a PR if you're okay with this

Best

Upgrade redis to v4

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

Incompatible with NestJS cache store

V3.0.1 is incompatible with NestJS . Unable to register CacheModule using the RedisStore.

"message": "Type 'typeof import(\"cache-test/node_modules/cache-manager-redis-store/dist/index\")' is not assignable to type '(string | CacheStoreFactory | CacheStore) & typeof import(\"/Users/kk/dev/nodejs/cache-test/node_modules/cache-manager-redis-store/dist/index\")'.\n  ...

Here is the code I am using in the module.ts :
`
import * as redisStore from "cache-manager-redis-store";
import { ConfigModule } from '@nestjs/config';

@module({
imports: [
ConfigModule.forRoot(),
CacheModule.register({
isGlobal: true,
store: redisStore,
url: "redis://localhost:6379",
}),
HttpModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
`

How to call .quit() or .disconnect() on redisCache.store.getClient()

Hi,

It seems that the original redis functions that allow the client to disconnect from redis are missing, or am I missing something?

var cacheManager = require('cache-manager');
var redisStore = require('cache-manager-redis-store');
 
var redisCache = cacheManager.caching({
  store: redisStore,
  host: 'localhost', // default value
  port: 6379, // default value
  auth_pass: 'XXXXX',
  db: 0,
  ttl: 600
});
 
// listen for redis connection error event
var redisClient = redisCache.store.getClient();

In this scenario how do I call redisClient.disconnect()?

I need it in my e2e tests, otherwise the cache manager hangs...

store not working when redis is not accessible

cache manager have option ignoreCacheErrors
which should threaten cache error the same as a cache miss. but it not works with redis store.
looks like it hangs and not call callback function in case of network error

[BUG] - Nest 8 RXJs 7.4 Error on returning null

Hello,
since the last update from Nest 7 to Nest 8 also RXJS got updated to > 7.4.0.
The call signature of Microservices has changed from .toPromise to lastValueFrom(...).

This change opened a possible bug if null is returned, which should be allowed in my opinion
image

The underlying node-cache-manger also discussed about allowing null values and allowed here in this really old post : Issue

Thanks for your opinion in advance :)

Best regards Meywether

Converting circular structure to JSON Problem

Hi,

Sorry i have a question for a section of your code, when i use this lib with cache-manager and Nest JS v 6.8.0 thc console show me this warning.

(node:26501) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property '_writableState' -> object with constructor 'WritableState'
    |     property 'afterWriteTickInfo' -> object with constructor 'Object'
    --- property 'stream' closes the circle
    at JSON.stringify (<anonymous>)
    at /node_modules/cache-manager-redis-store/dist/index.js:36:24

and this is my dependencies

"dependencies": {
    "@nest-modules/mailer": "^1.1.3",
    "@nestjs/common": "^6.8.0",
    "@nestjs/core": "^6.8.0",
    "@nestjs/platform-express": "^6.8.0",
    "@nestjs/typeorm": "^6.2.0",
    "cache-manager": "^3.4.3",
    "cache-manager-redis-store": "^2.0.0",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.11.0",
    ...
}

What is the expected usage such that the `redisStore` constructor receives the args from call to `caching`

v3.0.0 introduced a breaking change where instead of the module exporting a default:

const methods = {
  create: (...args) => redisStore(...args),
};

export default methods;

such that the store can be instantiated with the factory method like:

var redisStore = require('cache-manager-redis-store');

var config = {
  host: 'localhost', // default value
  port: 6379, // default value
};

var redisCache = cacheManager.caching({
  store: redisStore,
  ...config,
});

it can no longer be instantiated this way and must now be instantiated before passing it to "cache-manager" (see current README usage).

However, the implementation of this module clearly expects to receive args from the cache-manager when instantiating (e.g. isCacheableValue here). These are args that are not part of the input to the rediStore fn in the typings from this module (nor in the DefinitelyTyped types), but rather that are meant to be passed to the caching fn of cache-manager which will then pass it in to the factory method as seen in its source here (note that this is the older v4.0.1, which is the latest that is compatible with the latest v3.0.1 of this package).

I am guessing the usage of the factory method was removed, even though if it is clearly how cache-manager expects to integrate with stores, because instantiating the Redis store became an async process as another one of the breaking changes in 3.0.0.

Why is the isCacheableValue and other args still referenced when instantiating the store if they are not expected to be passed in from caching fn?

How are we even supposed to pass these args in then? Directly when instantiating the store (should the types then be updated?)

In general, should "cache-manager" caching function be made to handle async factory methods so that prior usage can be reverted?

RedisStore does not extend correctly Store

It seems the definition of RedisStore at

https://github.com/dabroek/node-cache-manager-redis-store/blob/master/dist/index.d.ts

is not compatible with the definition of Store at

https://github.com/node-cache-manager/node-cache-manager/blob/master/src/caching.ts

the methods set(), get(), keys(), reset() and ttl() have an incompatible function definitions

interface RedisStore extends Store {
    name: string;
    getClient: () => RedisClientType;
    isCacheableValue: any;
    set: (key: any, value: any, options: any, cb: any) => Promise<any>;
    get: (key: any, options: any, cb: any) => Promise<any>;
    del: (...args: any[]) => Promise<any>;
    mset: (...args: any[]) => Promise<any>;
    mget: (...args: any[]) => Promise<any>;
    mdel: (...args: any[]) => Promise<any>;
    reset: (cb: any) => Promise<any>;
    keys: (pattern: string, cb: any) => Promise<any>;
    ttl: (key: any, cb: any) => Promise<any>;
}
export type Store = {
  get<T>(key: string): Promise<T | undefined>;
  set<T>(key: string, data: T, ttl?: Ttl): Promise<void>;
  del(key: string): Promise<void>;
  reset(): Promise<void>;
  mset(args: [string, unknown][], ttl?: Ttl): Promise<void>;
  mget(...args: string[]): Promise<unknown[]>;
  mdel(...args: string[]): Promise<void>;
  keys(pattern?: string): Promise<string[]>;
  ttl(key: string): Promise<number>;
};

Exclude Route From Get Cached

By using this Package my whole application routes cached in server but there is no way to exclude some routes from being chached.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.