Nest: Caching Mechanism

Created on 16 May 2018  路  13Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

What is the motivation / use case for changing the behavior?

Supporting a simple in-memory cache provider out of the box would be a great, useful feature. Probably as an external installable package, i.e. @nest/cache. See https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-2.0 for an example.

common type todo 馃挌

Most helpful comment

it wold be nice to have Method level @Cache annotation as well for service methods

@Injectable()
export class ProductService {
 @Cache(myStore, { ttl: 60 })
    public async getProducts(): Promise<string[]> {
        // return seed data from database
        return [...];
    }
}

All 13 comments

https://www.npmjs.com/package/node-cache would probably be an ideal package to support "under the hood" of the Nest provider.

I'd be happy to write this provider if you think it's a good idea, @kamilmysliwiec.

Yeah, go for it! Maybe, let's create an interceptor that we could even put in the @nestjs/common as a built-in interceptor. CacheInterceptor sounds quite good :)

Hi @KerryRitter, any updates here? 馃敟

I haven't been able to do much lately but this is on my to-do! Anyone is free to take over, happy to help where I can

+1

+1

CacheModule just landed in 5.3.0. Read more here https://docs.nestjs.com/techniques/caching

it wold be nice to have Method level @Cache annotation as well for service methods

@Injectable()
export class ProductService {
 @Cache(myStore, { ttl: 60 })
    public async getProducts(): Promise<string[]> {
        // return seed data from database
        return [...];
    }
}

Is there a way to expose some generic cache (CacheStore) to have get and set methods to cache any arbitrary data?!

@danil-z I was able to do this by injecting the cache manager into my cache service, you can checkout an example at https://github.com/kyle-mccarthy/nest-next-starter/blob/master/src/cache/cache.service.ts

@danil-z this issue has been closed, it would be great to make another issue for this request.

@kamilmysliwiec how to invalidate cache ?

common use case

  1. api method getUser -> cache
  2. api method updateUser -> invalidate cache

only manually ?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anyx picture anyx  路  3Comments

cdiaz picture cdiaz  路  3Comments

menme95 picture menme95  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments

tronginc picture tronginc  路  3Comments