UNPKG

785 BJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('./Cache');
7
8module.exports = class MemoryCache extends Base {
9
10 _cache = {};
11
12 getValue (key) {
13 const value = this._cache[key];
14 if (value && (value[1] === 0 || value[1] > Date.now())) {
15 return value[0];
16 }
17 }
18
19 setValue (key, value, duration) {
20 if (duration) {
21 duration = Date.now() + duration * 1000;
22 }
23 this._cache[key] = [value, duration];
24 }
25
26 removeValue (key) {
27 if (Object.prototype.hasOwnProperty.call(this._cache, key)) {
28 delete this._cache[key];
29 }
30 }
31
32 flushValues () {
33 this._cache = {};
34 }
35};
\No newline at end of file