-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
131 lines (97 loc) · 3.63 KB
/
cache.js
File metadata and controls
131 lines (97 loc) · 3.63 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var storage = require( 'node-persist' ),
path = require( 'path' ),
logger = require( './logger.js' );
var storage_defaults = {
dir : path.join( __dirname, 'storage' ),
ttl : 24 * 60 * 60 * 1000, // 1 day
loggin: true
};
function init(app) {
storage_defaults = app.config.cache;
logger(3, 'storage_defaults', storage_defaults);
}
//storage.setItemSync('test', 'test');
var cache = function(cache_timeout) {
this.cache_timeout = cache_timeout || 0; // no cache
this.data = {};
this.cache_id = (new Date()).getTime();
logger(3, 'initializing cache: id:', this.cache_id);
};
cache.prototype.add_db = function(db, clbk) {
db = this.sanitize(db);
logger(3, 'cache.add: adding db', db);
if ( db in this.data ) {
logger(3, 'cache.add: db already in cache', db);
clbk();
} else {
logger(3, 'cache.add: creating cache for db', db);
var storate_opts = JSON.parse(JSON.stringify(storage_defaults));
storate_opts.dir = path.join( storage_defaults.dir, db );
storate_opts.ttl = this.cache_timeout;
//logger(3, 'storate_opts', storate_opts);
var myStorage = storage.create(storate_opts);
myStorage.initSync();
this.data[db] = myStorage;
clbk();
}
};
cache.prototype.clean = function() {
logger(3, "cache.clean: cleaning cache");
for ( var c in this.data ) {
logger(3, "cache.clean: cleaning cache", c);
this.data[c].clearSync();
}
};
cache.prototype.get = function(db, key, clbk) {
db = this.sanitize(db );
key = this.sanitize(key);
logger(3, "cache.get: getting cache: db", db, 'key', key);
var th = this;
if ( !(db in this.data) ) {
logger(3, "cache.get: getting cache: db", db, 'key', key, "no such db");
this.add_db(db,
function() {
th.get(db, key, clbk);
}
);
} else {
logger(3, "cache.get: getting cache: db", db, 'key', key, "db is present");
this.data[db].getItem(key,
function(err, val) {
logger(3, "cache.get: getting cache: db", db, 'key', key, "got key");
if ( err ) {
logger(3, "cache.get: getting cache: db", db, 'key', key, "got key error", err);
clbk(null);
} else {
logger(3, "cache.get: getting cache: db", db, 'key', key, "got key success");
clbk(val);
}
}
);
}
};
cache.prototype.set = function(db, key, val, clbk) {
db = this.sanitize(db );
key = this.sanitize(key);
logger(3, "cache.set: setting cache: db", db, 'key', key);
var th = this;
if ( !(db in this.data) ) {
logger(3, "cache.set: setting cache: db", db, 'key', key, "no such db. creating");
this.add_db(db,
function() {
th.set(db, key, val, clbk);
}
);
} else {
logger(3, "cache.set: setting cache: db", db, 'key', key, "db is present");
var ch = this.data[db];
var now = (new Date()).getTime();
logger(3, "cache.set: setting cache: db", db, 'key', key, 'now', now);
ch.setItem(key, val, clbk);
}
};
cache.prototype.sanitize = function(n) {
return n.replace('.', '_').replace('-', '_').replace('+', '_').replace('/', '_').replace('\\', '_').replace(' ', '_').replace('__', '_').replace('__', '_').toLowerCase();
};
exports.init = init;
exports.cache = cache;