This repository has been archived by the owner on May 28, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
"use strict"; | ||
|
||
let config = require("../../config"); | ||
let logger = require("../logger"); | ||
let redis = require("../redis"); | ||
|
||
let _ = require("lodash"); | ||
/** | ||
* Cacher factory for memory cache | ||
* | ||
* Similar: https://github.com/mpneuried/nodecache/blob/master/_src/lib/node_cache.coffee | ||
* | ||
* @class Cacher | ||
*/ | ||
class Cacher { | ||
|
||
/** | ||
* Creates an instance of Cacher. | ||
* | ||
* @param {any} type | ||
* @param {any} prefix | ||
* @param {any} ttl | ||
* | ||
* @memberOf Cacher | ||
*/ | ||
constructor(prefix, ttl) { | ||
logger.debug("Memory Cacher created. Prefix: " + prefix); | ||
|
||
// Cache container | ||
this.cache = {}; | ||
|
||
this.prefix = prefix ? prefix + ":" : ""; | ||
this.ttl = ttl; | ||
|
||
if (ttl) { | ||
this.timer = setInterval(() => { | ||
this.checkTTL(); | ||
}, 30 * 1000); | ||
} | ||
} | ||
|
||
/** | ||
* Get data from cache by key | ||
* | ||
* @param {any} key | ||
* @returns {Promise} | ||
* | ||
* @memberOf Cacher | ||
*/ | ||
get(key) { | ||
return new Promise((resolve, reject) => { | ||
logger.debug("Cacher: Get from cache by key:", key); | ||
let item = this.cache[this.prefix + key]; | ||
if (item) | ||
resolve(item.data); | ||
else | ||
resolve(null); | ||
}); | ||
} | ||
|
||
/** | ||
* Set data to cache by key | ||
* | ||
* @param {any} key | ||
* @param {any} data JSON object | ||
* @returns {Promise} | ||
* | ||
* @memberOf Cacher | ||
*/ | ||
set(key, data) { | ||
this.cache[this.prefix + key] = { | ||
data: data, | ||
expire: Date.now() + this.ttl * 1000 | ||
}; | ||
logger.debug("Cacher: Set to cache by key:", key); | ||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
* Delete a key from cache | ||
* | ||
* @param {any} key | ||
* @returns {Promise} | ||
* | ||
* @memberOf Cacher | ||
*/ | ||
del(key) { | ||
delete this.cache[this.prefix + key]; | ||
logger.debug("Cacher: Delete from cache by key:", key); | ||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
* Clean cache. Remove every key by prefix | ||
* http://stackoverflow.com/questions/4006324/how-to-atomically-delete-keys-matching-a-pattern-using-redis | ||
* alternative solution: | ||
* https://github.com/cayasso/cacheman-redis/blob/master/lib/index.js#L125 | ||
* @param {any} match Match string for SCAN. Default is "*" | ||
* @returns {Promise} | ||
* | ||
* @memberOf Cacher | ||
*/ | ||
clean(match) { | ||
// TODO: match not supported yet | ||
logger.debug("Cacher: Clear all entries from cache."); | ||
this.cache = {}; | ||
return Promise.resolve(); | ||
} | ||
|
||
checkTTL() { | ||
let self = this; | ||
let now = Date.now(); | ||
let keys = Object.keys(this.cache); | ||
keys.forEach((key) => { | ||
let item = this.cache[key]; | ||
|
||
if (item.expire && item.expire < now) { | ||
logger.debug(`Cacher: Item '${key} is expired!`); | ||
delete self.cache[key]; | ||
} | ||
}); | ||
} | ||
|
||
} | ||
module.exports = Cacher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
let config = require("../../config"); | ||
let logger = require("../logger"); | ||
let redis = require("../redis"); | ||
|
||
let _ = require("lodash"); | ||
|
||
let MemoryCacher = require("./cacher-memory"); | ||
let RedisCacher = require("./cacher-redis"); | ||
|
||
module.exports = function(type, prefix, ttl) { | ||
switch(type) { | ||
case "redis": return new RedisCacher(prefix, ttl); | ||
default: return new MemoryCacher(prefix, ttl); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters