Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -161,6 +161,42 @@ import { AppController } from './app.controller';
export class ApplicationModule {}
```

#### Redis cache `set` and `get` example

Copy link
Member

@johnbiundo johnbiundo May 26, 2020

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:

  1. What is the goal of this section
  2. What are the relevant portions of the API shown in the example and any concepts needed to understand them (i.e., this is the first time we're seeing CacheManager, so we need some context to understand how it works).

Thank you.

Copy link
Author

@SOHELAHMED7 SOHELAHMED7 May 27, 2020

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 and get it.

Like for e.g

Yii framework's docs

// try retrieving $data from cache
$data = $cache->get($key);

if ($data === false) {
    // $data is not found in cache, calculate it from scratch
    $data = $this->calculateSomething();

    // store $data in cache so that it can be retrieved next time
    $cache->set($key, $data);
}

// $data is available here

Laravel's Docs

Cache::put('key', 'value', $seconds);

$value = Cache::get('key');

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

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});

Choose a reason for hiding this comment

The 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.

Copy link
Author

@SOHELAHMED7 SOHELAHMED7 Jul 9, 2020

Choose a reason for hiding this comment

The 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 await statement are in try-catch blocks. E.g. https://docs.nestjs.com/pipes#class-validator

Choose a reason for hiding this comment

The 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).