-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (75 loc) · 2.15 KB
/
index.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
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
var MEMORY = 'MEMORY';
var LOCAL = 'LOCAL';
// Constructor
function CacheIt(type, name, expiry) {
if (type !== LOCAL && type !== MEMORY) {
throw 'Invalid cache type';
}
if (!name) {
throw 'Cache name is required';
}
if (!expiry || expiry === 0) {
throw 'Expiry time for cache is required and should be greater than zero';
}
this.cache = {
createdOn: new Date().getTime(),
expiry: expiry,
data: {}
};
this.name = name;
this.type = type;
if (this.type === LOCAL) {
if (!sessionStorage.getItem(name)) {
sessionStorage.setItem(name, JSON.stringify(this.cache));
}
}
}
// class methods
CacheIt.prototype.clear = function () {
this.cache.createdOn = new Date().getTime();
this.cache.data = {};
if (this.type === LOCAL) {
var myCache = JSON.parse(sessionStorage.getItem(this.name));
myCache = this.cache;
sessionStorage.setItem(this.name, JSON.stringify(myCache));
}
};
CacheIt.prototype.get = function (key) {
var now = new Date().getTime(), myCache;
if (this.type === LOCAL) {
myCache = JSON.parse(sessionStorage.getItem(this.name));
} else { // default to memory cache
myCache = this.cache;
}
// check if the data is still valid
if (now - myCache.createdOn >= myCache.expiry * 1000) {
this.clear();
return; // data has expired, so return a miss
}
var record = myCache.data[key];
if (!record) {
// the record is not in the cache
return;
}
return record;
};
CacheIt.prototype.put = function (key, record) {
try {
if (!key) {
// no key, no cache!
return false;
}
if (this.type === LOCAL) {
var localCache = JSON.parse(sessionStorage.getItem(this.name));
localCache.data[key] = record;
sessionStorage.setItem(this.name, JSON.stringify(localCache));
} else { // default to memory cache
this.cache.data[key] = record;
}
return true;
} catch (e) {
return false;
}
};
// export the class
module.exports = CacheIt;