Last active
August 2, 2022 13:56
-
-
Save airscholar/966fe1299beb7896e948bd1e325b2a79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from '@nestjs/common'; | |
import { TodoService } from './todo.service'; | |
import { TodoController } from './todo.controller'; | |
@Module({ | |
imports: [], | |
controllers: [TodoController], | |
providers: [TodoService], | |
}) | |
export class TodoModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common'; | |
import { CreateTodoDto } from './dto/create-todo.dto'; | |
import { UpdateTodoDto } from './dto/update-todo.dto'; | |
import { Cache } from 'cache-manager'; | |
@Injectable() | |
export class TodoService { | |
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {} | |
create(createTodoDto: CreateTodoDto) { | |
return 'This action adds a new todo'; | |
} | |
async findAll() { | |
const todos = await this.cacheManager.get('todos'); | |
if (todos) { | |
// this returns a cached version of todos | |
return { | |
message: 'Cached Todos', | |
data: todos, | |
}; | |
} | |
//your todo logic goes here | |
const freshTodos = []; | |
// cache todos in redis | |
await this.cacheManager.set('todos', freshTodos); | |
return { | |
message: 'Fresh todos', | |
data: todos, | |
}; | |
} | |
findOne(id: number) { | |
return `This action returns a #${id} todo`; | |
} | |
update(id: number, updateTodoDto: UpdateTodoDto) { | |
return `This action updates a #${id} todo`; | |
} | |
remove(id: number) { | |
return `This action removes a #${id} todo`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment