File size: 2,448 Bytes
fb54e64
 
 
 
 
 
3c3455c
 
 
 
 
fb54e64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b84404f
fb54e64
7e5c924
 
98ed8ec
7e5c924
fb54e64
 
 
 
 
 
 
 
 
 
b84404f
fb54e64
7e5c924
 
98ed8ec
7e5c924
fb54e64
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
}