-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcache.js
47 lines (40 loc) · 1.04 KB
/
cache.js
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
CacheStore = function(id, options) {
var self = this;
this.id = '__wizard_' + (id || 'default');
this.keys = {};
_.extend(this, {
persist: true
}, _.pick(options, 'persist'));
if (this.persist) {
var cache = Meteor._localStorage.getItem(this.id);
if (cache) {
_.each(EJSON.parse(cache), function(value, key) {
Session.set(self.prefix(key), value);
self.keys[key] = value;
});
}
}
};
_.extend(CacheStore.prototype, Session, {
prefix: function(key) {
return this.id + '__' + key;
},
set: function(key, value) {
Session.set(this.prefix(key), value);
if (this.persist) {
this.keys[key] = value;
Meteor._localStorage.setItem(this.id, EJSON.stringify(this.keys));
}
},
get: function(key) {
return Session.get(this.prefix(key));
},
clear: function() {
var self = this;
_.each(this.keys, function(value, key) {
Session.set(self.prefix(key), null);
});
if (this.persist)
Meteor._localStorage.removeItem(this.id);
}
});