-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrolling-storage.js
166 lines (138 loc) · 3.71 KB
/
rolling-storage.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module !== "undefined" && module.exports) {
// CommonJS/Node module
module.exports = factory();
} else {
// Browser globals
root.rollingStorage = factory();
}
}(this, function () {
var rollingStorage = function (options) {
var storage = options.storage;
var namespace = options.namespace;
var ttl = options.ttl;
var maxSize = options.maxSize;
if (!storage) {
throw new Error('Attemted to create a rollingStorage instance without a storage strategy! Usually this is localStorage or sessionStorage.');
}
if (!namespace) {
throw new Error('Attemted to create a rollingStorage instance without a namespace!');
}
if (!ttl) {
throw new Error('Attemted to create a rollingStorage instance without a ttl! This is the number of milliseconds it will keep content.');
}
if (!maxSize) {
throw new Error('Attemted to create a rollingStorage instance without a maxSize! This is the largest it will allow the cache to grow.');
}
function makeKey (key) {
return 'rollingStorage-'+namespace+'-'+key;
}
var inventory = JSON.parse(storage.getItem('rollingStorage-inventory-'+namespace) || '[]');
var size = 0;
var timeouts = {};
inventory.forEach(function (item) {
size += item.size;
});
var saveInventoryTimeout = null;
function saveInventory () {
if (!saveInventoryTimeout) {
saveInventoryTimeout = setTimeout(function () {
saveInventoryTimeout = null;
storage.setItem('rollingStorage-inventory-'+namespace, JSON.stringify(inventory));
}, 4);
}
}
function remove (key) {
var i = 0;
while (inventory[i] && inventory[i].key !== key) {
i += 1;
}
var item = inventory[i];
if (item) {
storage.removeItem(makeKey(item.key));
size -= item.size;
clearTimeout(timeouts[item.key]);
delete timeouts[item.key];
inventory.splice(i, 1);
saveInventory();
}
}
// determine if anything is expired and remove it.
var now = Date.now();
inventory.slice().forEach(function (item) {
var remainingTtl = item.expiration - now;
if (remainingTtl < 0) {
remove(item.key);
} else {
timeouts[item.key] = setTimeout(function () {
remove(item.key);
}, remainingTtl);
}
});
function has (key) {
for (var i = 0; i < inventory.length; i += 1) {
if (inventory[i].key === key) {
return true;
}
}
return false;
}
function get (key) {
if (has(key)) {
return JSON.parse(storage.getItem(makeKey(key)));
} else {
return undefined;
}
}
function set (key, value) {
remove(key);
var jsonString = JSON.stringify(value);
var indexEntry = {
key: key,
expiration: Date.now() + ttl,
size: jsonString.length
};
size += indexEntry.size;
while (size > maxSize && inventory.length) {
remove(inventory[0].key);
}
clearTimeout(timeouts[key]); // in case we're saving over an old value.
timeouts[key] = setTimeout(function () {
remove(key);
}, ttl);
inventory.push(indexEntry);
var doneTrying = false;
while (!doneTrying) {
try {
storage.setItem(makeKey(key), jsonString);
saveInventory();
doneTrying = true;
} catch (e) {
if (inventory.length) {
remove(inventory[0].key);
} else {
// this will happen when the remaining storage is smaller than the item.
doneTrying = true;
}
}
}
}
function flush () {
while (inventory.length) {
remove(inventory[0].key);
}
}
var exports = {
has: has,
get: get,
set: set,
remove: remove,
flush: flush
};
return exports;
};
return rollingStorage;
}));