Skip to content

Instantly share code, notes, and snippets.

@airscholar
Last active August 2, 2022 13:56
Show Gist options
  • Save airscholar/966fe1299beb7896e948bd1e325b2a79 to your computer and use it in GitHub Desktop.
Save airscholar/966fe1299beb7896e948bd1e325b2a79 to your computer and use it in GitHub Desktop.
import { Module } from '@nestjs/common';
import { TodoService } from './todo.service';
import { TodoController } from './todo.controller';
@Module({
imports: [],
controllers: [TodoController],
providers: [TodoService],
})
export class TodoModule {}
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