Skip to content

Commit

Permalink
Merge pull request #199 from fdhhhdjd/developer
Browse files Browse the repository at this point in the history
Developer
  • Loading branch information
fdhhhdjd authored Mar 14, 2023
2 parents d869896 + 5135a2e commit 658e8ed
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const HELPER = require('../../../../share/utils/helper');
const RANDOMS = require('../../../../share/utils/random');
const CONSTANTS = require('../../../../share/configs/constants');
const MEMORY_CACHE = require('../../../../share/utils/limited_redis');

//! MIDDLEWARE
const { globalCache } = require('../../../../share/patterns/LRU_Strategy.patterns');
Expand All @@ -10,6 +11,9 @@ const { returnReasons } = require('../../../../share/middleware/handle_error');
//! MODEL
const book_model = require('../../../../share/models/book.model');

//! SERVICE
const book_admin_service = require('../../../../share/services/admin_service/book_service');

const bookController = {
/**
* @author Nguyễn Tiến Tài
Expand Down Expand Up @@ -60,8 +64,8 @@ const bookController = {
let result;
[err, result] = await HELPER.handleRequest(book_model.createBook(data_insert));
if (result) {
// Delete data cache lru argothim
globalCache.delCache(CONSTANTS.KEY_REDIS.ALL_BOOK);
// Del key Redis
MEMORY_CACHE.delKeyCache(CONSTANTS.KEY_REDIS.ALL_BOOK);

return res.status(200).json({
status: 200,
Expand Down Expand Up @@ -157,8 +161,8 @@ const bookController = {
book_id,
});

// Delete data cache lru argothim
globalCache.delMultiCache(CONSTANTS.KEY_REDIS.ALL_BOOK, key_cache_book_detail);
// Delete Cache
book_admin_service.handleDeleteCache(key_cache_book_detail, CONSTANTS.KEY_REDIS.ALL_BOOK);

return res.status(200).json({
status: 200,
Expand Down Expand Up @@ -236,8 +240,8 @@ const bookController = {
book_id,
});

// Delete data cache lru argothim
globalCache.delMultiCache(CONSTANTS.KEY_REDIS.ALL_BOOK, key_cache_book_detail);
// Delete Cache
book_admin_service.handleDeleteCache(key_cache_book_detail, CONSTANTS.KEY_REDIS.ALL_BOOK);

return res.status(200).json({
status: 200,
Expand Down Expand Up @@ -288,7 +292,7 @@ const bookController = {

// detail book database
const cache_lru_book = globalCache.getCache(key_cache_book_detail);
if (cache_lru_book !== -1) {
if (cache_lru_book !== CONSTANTS.NO) {
return res.status(200).json({
status: 200,
message: returnReasons('200'),
Expand All @@ -304,7 +308,7 @@ const bookController = {
);
if (result_book_detail) {
// Add data cache lru argothim
globalCache.putCache(key_cache_book_detail, result_book_detail[0]);
book_admin_service.handleSetCacheLRU(key_cache_book_detail, result_book_detail[0]);

return res.status(200).json({
status: 200,
Expand Down Expand Up @@ -333,22 +337,27 @@ const bookController = {
*/
getAllBook: async (req, res) => {
try {
// detail book database
const cache_lru_book = globalCache.getCache(CONSTANTS.KEY_REDIS.ALL_BOOK);
if (cache_lru_book !== -1) {
// Detail book database
const cache_redis_book = await MEMORY_CACHE.getCache(CONSTANTS.KEY_REDIS.ALL_BOOK);
if (cache_redis_book) {
return res.status(200).json({
status: 200,
message: returnReasons('200'),
element: {
result: cache_lru_book,
result: JSON.parse(cache_redis_book),
},
});
}
// Take data db
const result_book_detail = await book_model.getAllBook({ isdeleted: CONSTANTS.DELETED_DISABLE }, '*');
if (result_book_detail) {
// Add data cache lru argothim
globalCache.putCache(CONSTANTS.KEY_REDIS.ALL_BOOK, result_book_detail);
const redisTTLWithRandom = RANDOMS.getRedisTTLWithRandom(CONSTANTS._1_MONTH);
// Add data redis
book_admin_service.handleSetCacheRedis(
CONSTANTS.KEY_REDIS.ALL_BOOK,
result_book_detail,
redisTTLWithRandom,
);

return res.status(200).json({
status: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const HELPER = require('../../../../share/utils/helper');
const CONSTANTS = require('../../../../share/configs/constants');

//! MIDDLEWARE
const { globalCache } = require('../../../../share/patterns/LRU_Strategy.patterns');
const { returnReasons } = require('../../../../share/middleware/handle_error');

//! MODEL
const borrow_book_model = require('../../../../share/models/book_borrowed.model');
const book_model = require('../../../../share/models/book.model');

//! SERVICE
const book_admin_service = require('../../../../share/services/admin_service/book_service');

const BorrowBookController = {
/**
* @author Nguyễn Tiến Tài
Expand Down Expand Up @@ -109,8 +111,9 @@ const BorrowBookController = {
const key_cache_book_detail = HELPER.getURIFromTemplate(CONSTANTS.KEY_REDIS.DETAIL_BOOK, {
book_id,
});
// Delete data cache lru argothim
globalCache.delMultiCache(CONSTANTS.KEY_REDIS.ALL_BOOK, key_cache_book_detail);

// Delete Cache
book_admin_service.handleDeleteCache(key_cache_book_detail, CONSTANTS.KEY_REDIS.ALL_BOOK);

return res.status(200).json({
status: 200,
Expand Down
8 changes: 8 additions & 0 deletions backend-manager-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,12 @@ module.exports = {
ALL_BOOK: 'book_all',
DETAIL_BOOK: 'detail_book_${book_id}',
},
/**
* @author Nguyễn Tiến Tài
* @created_at 14/03/2023
* @description True and False
* @param { String }
*/
YES: 1,
NO: 0,
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
//! LIBRARY
const LRU = require('lru-cache');

//! SHARE
const CONFIGS = require('../configs/config');
const CONSTANTS = require('../configs/constants');
/**
* @author Nguyễn Tiến Tài
* @created_at 22/12/2022
* @updaed_at 14/03/2023
* @description File Algorithm Cache LRU
*/
class LRUCache {
Expand All @@ -26,7 +31,7 @@ class LRUCache {
const val = this._cache.get(key);
return val;
}
return -1;
return CONSTANTS.NO;
} catch (error) {
throw new Error(`Error getting key ${key} from cache: ${error}`);
}
Expand All @@ -41,6 +46,18 @@ class LRUCache {
}
}

exitKeyCache(key) {
try {
const hasKey = this._cache.has(key);
if (hasKey) {
return CONSTANTS.YES;
}
return CONSTANTS.NO;
} catch (error) {
throw new Error(`Error check key ${key} from cache: ${error}`);
}
}

delMultiCache(key_1, key_2) {
try {
this._cache.delete(key_1) && this._cache.delete(key_2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! SHARE
const MEMORY_CACHE = require('../../utils/limited_redis');
const CONSTANTS = require('../../configs/constants');

//! MIDDLEWARE
const { globalCache } = require('../../patterns/LRU_Strategy.patterns');

module.exports = {
/**
* @author Nguyễn Tiến Tài
* @created_at 14/03/2023
* @description Delete Cache
* @function handleDeleteCache
*/
handleDeleteCache: async (key_cache_lru, key_cache_redis) => {
try {
// Check Key lru argothim
const check_key_lru_book_detail = globalCache.exitKeyCache(key_cache_lru);
if (check_key_lru_book_detail === CONSTANTS.YES) {
// Delete data cache lru argothim
globalCache.delCache(key_cache_lru);
}

// Check key Redis
const check_key_redis_book_all = await MEMORY_CACHE.existsKeyCache(key_cache_redis);
if (check_key_redis_book_all === CONSTANTS.YES) {
// Delete key Redis
MEMORY_CACHE.delKeyCache(key_cache_redis);
}
return false;
} catch (error) {
return true;
}
},
/**
* @author Nguyễn Tiến Tài
* @created_at 14/03/2023
* @description Save Cache Redis
* @function handleSetCacheRedis
*/
handleSetCacheRedis: async (key_cache_redis, value, ttl) => {
try {
// Check key Redis
const check_key_redis_book_all = await MEMORY_CACHE.existsKeyCache(key_cache_redis);
if (check_key_redis_book_all === CONSTANTS.NO) {
// Set data Redis
MEMORY_CACHE.setCacheEx(key_cache_redis, JSON.stringify(value), ttl);
}
return false;
} catch (error) {
return true;
}
},
/**
* @author Nguyễn Tiến Tài
* @created_at 14/03/2023
* @description Save Cache LRU
* @function handleSetCacheRedis
*/
handleSetCacheLRU: async (key_cache_lru, value) => {
try {
// Check Key lru argothim
const check_key_lru_book_detail = globalCache.exitKeyCache(key_cache_lru);
if (check_key_lru_book_detail === CONSTANTS.NO) {
// Add data cache lru argothim
globalCache.putCache(key_cache_lru, value);
}
return false;
} catch (error) {
return true;
}
},
};
16 changes: 16 additions & 0 deletions backend-manager-student/src/share/utils/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,20 @@ module.exports = {
generatorOtp() {
return OtpGenerator.generate(CONFIGS.RANDOM_NUMBER_OTP, OPTION_OTP);
},
/**
* @author Nguyễn Tiến Tài
* @param {null}
* @created_at 14/03/2023
* @description Random time ttl redis
* @returns {Number}
*/
getRedisTTLWithRandom(initialTTL) {
// Số random được thêm vào (tính bằng mili giây)
const randomTime = Math.floor(Math.random() * CONSTANTS._1_HOURS_S_REDIS); // Tối đa 1 giờ

// Tổng thời gian TTL
const ttl = initialTTL + randomTime;

return ttl;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const bookController = {

// detail book database
const cache_lru_book = globalCache.getCache(key_cache_book_detail);
if (cache_lru_book !== -1) {
if (cache_lru_book !== CONSTANTS.NO) {
return res.status(200).json({
status: 200,
message: returnReasons('200'),
Expand Down Expand Up @@ -83,7 +83,7 @@ const bookController = {
try {
// detail book database
const cache_lru_book = globalCache.getCache(CONSTANTS.KEY_REDIS.ALL_BOOK);
if (cache_lru_book !== -1) {
if (cache_lru_book !== CONSTANTS.NO) {
return res.status(200).json({
status: 200,
message: returnReasons('200'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const CONSTANTS = require('../../../../share/configs/constants');
const RANDOMS = require('../../../../share/utils/random');

//! MIDDLEWARE
const { globalCache } = require('../../../../share/patterns/LRU_Strategy.patterns');
const { returnReasons } = require('../../../../share/middleware/handle_error');

//! MODEL
Expand All @@ -13,6 +12,7 @@ const borrowed_book_model = require('../../../../share/models/book_borrowed.mode

//! SERVICE
const book_service = require('../../../../share/services/user_service/book_service');
const book_admin_service = require('../../../../share/services/admin_service/book_service');

const BorrowBookController = {
/**
Expand Down Expand Up @@ -128,8 +128,8 @@ const BorrowBookController = {
book_id,
});

// Delete data cache lru argothim
globalCache.delMultiCache(CONSTANTS.KEY_REDIS.ALL_BOOK, key_cache_book_detail);
// Delete Cache
book_admin_service.handleDeleteCache(key_cache_book_detail, CONSTANTS.KEY_REDIS.ALL_BOOK);

return res.status(200).json({
status: 200,
Expand Down

0 comments on commit 658e8ed

Please sign in to comment.