Search code examples
javascripttypescriptcachingcronnestjs

Nestjs how to clear ( reset ) all cache


I'm building an API and the data get updated every day at 3 am and need to clear all cached endpoints whatever is!

I'm using the CacheModule and the decorator @UserInterceptor(CacheInterceptor) to cache whatever I need in the controller.

there a Cron function that runs every day at 3 am to update the content, I need to know what the code should put in that method to clear all cache.


Solution

  • You can inject the underlying cache manager instance:

    constructor(@Inject(CACHE_MANAGER) protected readonly cacheManager) {} 
    

    And then use it to delete the caches for all keys:

    const keys = await this.cacheManager.keys()
    await this.cacheManager.del(keys)