Skip to content

Commit

Permalink
add function delete with cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
bernandotorrez committed Jan 10, 2024
1 parent fc39844 commit 05ea9bb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
12 changes: 12 additions & 0 deletions todo-service/repositories/redis/cacheRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ class CacheRepository {
});
});
}

keys(keys) {
return new Promise((resolve, reject) => {
this._client.keys(keys, (error, count) => {
if (error) {
return reject(error);
}

return resolve(count);
});
});
}
}

module.exports = CacheRepository;
42 changes: 34 additions & 8 deletions todo-service/routes/v1/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
require('express-async-errors');
const router = express.Router();
const httpStatus = require('http-status');
const deleteCache = require('../../utils/deleteCache');

// Repositories
const TodoRepository = require('../../repositories/firebase/todoRepository');
Expand All @@ -17,14 +18,27 @@ const cacheRepository = new CacheRepository();
router.get('/', async (req, res) => {
const { next } = req.query;

const todos = await todoRepository.getTodosCursor(next);

res.status(httpStatus.OK).json({
code: httpStatus.OK,
status: 'SUCCESS',
message: httpStatus[`${httpStatus.OK}_NAME`],
data: todos
});
try {
const todos = await cacheRepository.get(`todo:${next}`)

res.status(httpStatus.OK).json({
code: httpStatus.OK,
status: 'SUCCESS',
message: httpStatus[`${httpStatus.OK}_NAME`],
data: JSON.parse(todos)
});
} catch (error) {
const todos = await todoRepository.getTodosCursor(next);

await cacheRepository.set(`todo:${next}`, JSON.stringify(todos), 60);

res.status(httpStatus.OK).json({
code: httpStatus.OK,
status: 'SUCCESS',
message: httpStatus[`${httpStatus.OK}_NAME`],
data: todos
});
}
});

router.post('/', async (req, res) => {
Expand All @@ -34,6 +48,10 @@ router.post('/', async (req, res) => {
todo_name: req.body.todo_name,
});

if(createTodo) {
deleteCache('todo:*')
}

res.status(httpStatus.OK).json({
code: httpStatus.OK,
status: 'SUCCESS',
Expand Down Expand Up @@ -66,6 +84,10 @@ router.put('/:uuid?', async (req, res) => {
todo_name
});

if(updateTodo) {
deleteCache('todo:*')
}

res.status(httpStatus.OK).json({
code: httpStatus.OK,
status: 'SUCCESS',
Expand All @@ -79,6 +101,10 @@ router.delete('/:uuid?', async (req, res) => {

const todo = await todoRepository.deleteTodo(uuid);

if(todo) {
deleteCache('todo:*')
}

res.status(httpStatus.OK).json({
code: httpStatus.OK,
status: 'SUCCESS',
Expand Down
16 changes: 16 additions & 0 deletions todo-service/utils/deleteCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const CacheRepository = require('../repositories/redis/cacheRepository');
const cacheRepository = new CacheRepository();

bulkDelete = async (prefix) => {
const keys = await cacheRepository.keys(prefix);

keys.forEach(async key => {
const deleted = await cacheRepository.delete(key);

if(deleted) {
console.info(`(Redis) Key : ${key} is deleted`);
}
});
};

module.exports = bulkDelete;

0 comments on commit 05ea9bb

Please sign in to comment.