nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

Caching Mechanism #695

Closed KerryRitter closed 6 years ago

KerryRitter commented 6 years ago

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.

KerryRitter commented 6 years ago

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.

kamilmysliwiec commented 6 years ago

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 :)

kamilmysliwiec commented 6 years ago

Hi @KerryRitter, any updates here? 🔥

KerryRitter commented 6 years ago

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

zuohuadong commented 6 years ago

+1

frozenex commented 6 years ago

+1

kamilmysliwiec commented 6 years ago

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

xmlking commented 6 years ago

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 [...];
    }
}
danil-z commented 6 years ago

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

kyle-mccarthy commented 6 years ago

@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

mohammadrafigh commented 5 years ago

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

ruscon commented 5 years ago

@kamilmysliwiec how to invalidate cache ?

common use case

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

only manually ?

lock[bot] commented 5 years ago

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.