leeminwaan's picture
Update app/cache.js
b84404f verified
Raw
History Blame Contribute Delete
2.45 kB
const path = require('path');
const fs = require('fs');
const crypto = require('crypto')
const TEMP_FILE_DIR = path.join(__dirname, 'temp_files');
const CACHE_TIMEOUT = 60 * 60 * 1000; // 60 minutes in milliseconds
const logger = require('./winston');
const Redis = require("ioredis");
require('dotenv').config()
const redisUri = process.env.REDIS_URL;
// console.log(redisUri)
const redis = new Redis(redisUri);
const Cache = require("ioredis-cache")
const redis_cache = new Cache(redis)
/**
* Create a safe filename based on the URL (hashing, etc.).
*
* @param {string} url - The URL to create the filename from
* @return {string} The safe filename created from the URL
*/
function getCacheFilename(url) {
// Create a safe filename based on the URL (hashing, etc.)
return 'cached__key_' + crypto.createHash('md5').update(url).digest('hex');
}
const cache = async (key, value_if_not_present) => {
if(!fs.existsSync(TEMP_FILE_DIR)){
fs.mkdirSync(TEMP_FILE_DIR, { recursive: true });
}
if(!fs.existsSync(path.join(TEMP_FILE_DIR, getCacheFilename(key)))){
logger.warn(`Cache content does not exist`);
logger.warn("Try to get redis cache before running new one!")
var redis_val = await redis_cache.cache(key, value_if_not_present);
// var value = await value_if_not_present();
fs.writeFile(path.join(TEMP_FILE_DIR, getCacheFilename(key)), JSON.stringify(redis_val), (err) => {
if (err) throw err;
logger.info(`The key ${key} has been cached`);
});
return redis_val;
}
const cacheFilePath = path.join(TEMP_FILE_DIR, getCacheFilename(key));
const fileStats = fs.statSync(cacheFilePath);
const fileAge = Date.now() - fileStats.mtimeMs;
if (fileAge > CACHE_TIMEOUT) {
logger.warn(`Cache content timed out`);
logger.warn("Try to get redis cache before running new one!")
var redis_val = await redis_cache.cache(key, value_if_not_present);
// var value = await value_if_not_present();
fs.writeFile(path.join(TEMP_FILE_DIR, getCacheFilename(key)), JSON.stringify(redis_val), (err) => {
if (err) throw err;
logger.info(`The key ${key} has been cached`);
});
return redis_val;
}
logger.info("Use cached content");
return JSON.parse(fs.readFileSync(path.join(TEMP_FILE_DIR, getCacheFilename(key))));
}
module.exports = {
cache
}