Comments (3)
For listening to the events from RedisClient
, once the module has been initialized, I could write a listener for it as follows (I am using cache-manager-redis-store
):
export class RedisClientEventListener implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(RedisClientEventListener.name);
private readonly redisStore: RedisStore;
constructor(@Inject(CACHE_MANAGER) readonly cacheManager: Cache) {
this.redisStore = cacheManager.store as unknown as RedisStore;
}
onModuleInit(): void {
const client = this.redisStore.getClient();
if (!client) {
this.logger.error('no redis client initialised');
}
if (client.isReady) {
this.logger.log('redis client is connected and ready');
}
client.on('connect', () => {
this.logger.log('redis client is connecting to server...');
});
client.on('ready', () => {
this.logger.log('redis client is ready');
});
client.on('end', () => {
this.logger.log('redis client connection closed');
});
client.on('error', (error: Error) => {
this.logger.error(error, 'redis client received an error');
});
}
async onModuleDestroy(): Promise<void> {
await this.redisStore.getClient().quit();
}
}
and then the module that imports and initializes CacheModule
extends the above:
@Module({
imports: [
CacheModule.registerAsync(cacheModuleOptions),
],
controllers: [SomeController],
})
export class SomeModule extends RedisClientEventListener {}
This works well to listen to the errors and handle those and the app doesn't crash, however, if the app is unable to connect to the client during initialisation itself, these listeners don't get a change to get registered and the app crashes. So far even I haven't found a way to avoid the app from crashing when app is unable to connect while initialising CacheModule
.
from cache-manager.
anything new on this issue?
from cache-manager.
For listening to the events from
RedisClient
, once the module has been initialized, I could write a listener for it as follows (I am usingcache-manager-redis-store
):export class RedisClientEventListener implements OnModuleInit, OnModuleDestroy { private readonly logger = new Logger(RedisClientEventListener.name); private readonly redisStore: RedisStore; constructor(@Inject(CACHE_MANAGER) readonly cacheManager: Cache) { this.redisStore = cacheManager.store as unknown as RedisStore; } onModuleInit(): void { const client = this.redisStore.getClient(); if (!client) { this.logger.error('no redis client initialised'); } if (client.isReady) { this.logger.log('redis client is connected and ready'); } client.on('connect', () => { this.logger.log('redis client is connecting to server...'); }); client.on('ready', () => { this.logger.log('redis client is ready'); }); client.on('end', () => { this.logger.log('redis client connection closed'); }); client.on('error', (error: Error) => { this.logger.error(error, 'redis client received an error'); }); } async onModuleDestroy(): Promise<void> { await this.redisStore.getClient().quit(); } }
and then the module that imports and initializes
CacheModule
extends the above:@Module({ imports: [ CacheModule.registerAsync(cacheModuleOptions), ], controllers: [SomeController], }) export class SomeModule extends RedisClientEventListener {}
This works well to listen to the errors and handle those and the app doesn't crash, however, if the app is unable to connect to the client during initialisation itself, these listeners don't get a change to get registered and the app crashes. So far even I haven't found a way to avoid the app from crashing when app is unable to connect while initialising
CacheModule
.
I am stuck on the exactly the same thing :(
from cache-manager.
Related Issues (20)
- Using CacheInterceptor on endpoint and returning classes using class-validator decorators and applying @Excluded() decorator is not excluding the attribute in the cached response. HOT 1
- Set with CacheStoreSetOptions doesn't set correct TTL HOT 1
- Existing multicaching support not exposed in types HOT 4
- CacheModule.register() missing type for store property for redisStore HOT 1
- CacheModule in multiple modules not loading the config properly HOT 2
- Need detailed Error when set cache key. HOT 3
- Merge injection token for cache and cache instance HOT 7
- Dependency Dashboard
- The register method of CacheModel does not utilize the useClass specified HOT 1
- Dynamic cache key HOT 1
- TypeError: ttl must be a positive integer if specified HOT 1
- Cache prefix HOT 1
- redisInstance is not used HOT 1
- Unable to call cacheManager properly when there is a cyclic dependency HOT 1
- registerAsync + global does not work HOT 3
- enableVersioning HOT 1
- `cache-manager 5.6.0` fails to load HOT 2
- App crashing on redis WRONGPASS error and no way recover from this state HOT 1
- Set `ttl` via `this.cacheManager.set` is not working when `ttl` is set in `CacheModule.registerAsync` as global HOT 1
- Customize CacheKey according to request parameters HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cache-manager.