dabroek / node-cache-manager-redis-store Goto Github PK
View Code? Open in Web Editor NEWRedis store for node-cache-manager using node_redis.
License: MIT License
Redis store for node-cache-manager using node_redis.
License: MIT License
Getting this error with cache-manager with Typescript
when trying to const redisClient = redisCache.store.getClient()
Facing ENOENT: no such file or directory, open 'node:util' when create the testing module.
jset version : 26.6.3
error : jest-runtime/build/index.js:1987:21
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? 😕
Any typescript declaration?
I have 2 questions.
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?
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!
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
TypeError: args.at is not a function
at Object.del (D:\applications\nodejs\nestjs-apps\nestjs-shop\node_modules_cache-manager-redis-store@3.0.1@cache-manager-redis-store\dist\index.js:755:24)
Has It support for Data compression methods ?
May be an interesting idea to add a keyPrefix
so that all cache elements stored with the store are - well - prefixed by it, making name-spacing easy.
To make use of already running redis connections, it may be an interesting idea to be able to pass a running node-redis instance and use that instead of spawning a new one?
To use newest types features for typescript and avoid errors with types when using latest 3.x versions
Any specific reason why del
and reset
methods don't return Promises?
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 {}
`
Is there any possible way to run the redis set commands through this package?
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...
Any chance to include something like this: https://www.npmjs.com/package/redis-sentinel to support sentinel connections? It seems node_redis
aren't adding sentinel support?
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
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
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
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",
...
}
Hey.
Do this package supports scan function?
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?
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>;
};
By using this Package my whole application routes cached in server but there is no way to exclude some routes from being chached.
lib do not expose disconnect method
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.