-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add redis cache set and get example docs #1254
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,6 +161,42 @@ import { AppController } from './app.controller'; | |
export class ApplicationModule {} | ||
``` | ||
|
||
#### Redis cache `set` and `get` example | ||
|
||
First install the type dependency: | ||
|
||
```bash | ||
$ npm install --save-dev @types/cache-manager | ||
``` | ||
|
||
```typescript | ||
// AppService.ts | ||
import { Injectable, Inject, CACHE_MANAGER } from '@nestjs/common'; | ||
import { Cache } from 'cache-manager'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) { | ||
} | ||
|
||
async getHello(): Promise<string> { | ||
// callback way | ||
this.cacheManager.set('foo', 'bar', {ttl: 100000}, (err) => { | ||
this.cacheManager.get('foo', (errGet, result) => { | ||
console.log(result); // logs "bar" | ||
}); | ||
}); | ||
|
||
// async/await way | ||
await this.cacheManager.set('foo2', 'bar2', {ttl: 100000}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be a good practice to wrap this in a try/catch block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. Just for docs it is kept like this and trying to keep minimal. I checked other part of docs, not at all place There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, makes sense. |
||
const res = await this.cacheManager.get('foo2'); | ||
console.log(res); // logs "bar2" | ||
|
||
return 'Hello World!'; | ||
} | ||
} | ||
``` | ||
|
||
#### Adjust tracking | ||
|
||
By default, Nest uses the request URL (in an HTTP app) or cache key (in websockets and microservices apps, set through the `@CacheKey()` decorator) to associate cache records with your endpoints. Nevertheless, sometimes you might want to set up tracking based on different factors, for example, using HTTP headers (e.g. `Authorization` to properly identify `profile` endpoints). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I don't understand what this example is trying to do, or how the code is working, without some greater context. Could you please try to explain:
CacheManager
, so we need some context to understand how it works).Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section adds simple example how to use cache (e.g. Redis cache) in the code. The current documention is nice but does not include any example or docs about how to simply
set
"values" by "keys" in Redis cache andget
it.Like for e.g
Yii framework's docs
Laravel's Docs
CacheManager
provide generic API regardless of what is used to store cache e.g. Redis, MySQL, file etc.Feel free to let me know if more info needed