Skip to content

Commit a5bdf3b

Browse files
committed
Implemented cache clearing and added tests for it.
1 parent 0d3410c commit a5bdf3b

3 files changed

Lines changed: 102 additions & 9 deletions

File tree

build/jsoncache-0.0.1.min.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jsoncache.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
version: '#VERSION#',
2222

2323
debug: true,
24-
prefix: 'JSONCache',
25-
keySeparator: ' '
24+
prefix: 'JSONCache'
2625
};
2726

2827
var log = function () {
@@ -51,16 +50,56 @@
5150
var JSONCache = {};
5251
JSONCache.settings = settings;
5352

53+
// Remove a certain item from the localStorage cache.
54+
// If the url is given as an argument, then only that
55+
// particular item is removed, otherwise all the items
56+
// stored by JSONCache are removed.
57+
JSONCache.clear = function (url) {
58+
if (url) {
59+
// Remove a particular item.
60+
window.localStorage.removeItem(settings.prefix + ' data ' + url);
61+
window.localStorage.removeItem(settings.prefix + ' time ' + url);
62+
} else {
63+
// Remove all items (stored by JSONCache) if no url was specified.
64+
65+
// Regexp to match keys stored with JSONCache.
66+
var cacheKeyRe = new RegExp('^' + settings.prefix + ' (data|time) ');
67+
var i, key;
68+
var len = window.localStorage.length;
69+
var keysToBeRemoved = [];
70+
71+
// List all keys that are stored with JSONCache
72+
for (i = 0; i < len; ++i) {
73+
key = window.localStorage.key(i);
74+
if (cacheKeyRe.test(key)) {
75+
keysToBeRemoved.push(key);
76+
}
77+
}
78+
// Remove all listed keys.
79+
len = keysToBeRemoved.length;
80+
for (i = 0; i < len; ++i) {
81+
window.localStorage.removeItem(keysToBeRemoved[i]);
82+
}
83+
}
84+
};
85+
5486
// Provide the proxy function for testing to mock the real jQuery.getJSON calls.
5587
JSONCache.getJSONProxy = function (url, options) {
5688
$.ajax(url, options);
5789
};
5890

91+
JSONCache.getTime = function () {
92+
return (new Date()).getTime();
93+
};
94+
5995
JSONCache.getCachedJSON = function (url, options) {
96+
var now = (new Date()).getTime();
6097
var success = options.success;
61-
var key = settings.prefix + settings.keySeparator + 'data' +
62-
settings.keySeparator + url;
63-
var cachedData = window.localStorage[key];
98+
var dataKey = settings.prefix + ' data ' + url;
99+
var timeKey = settings.prefix + ' time ' + url;
100+
var cachedData = window.localStorage[dataKey];
101+
var cachedTime = window.localStorage[timeKey];
102+
64103
if (cachedData) {
65104
log('Value found from cache for url:', url);
66105
success(JSON.parse(cachedData));
@@ -70,7 +109,7 @@
70109
// Wrap the success function to cache the data.
71110
options.success = function (data) {
72111
log('Fetched data, adding to cache for url:', url);
73-
window.localStorage[key] = JSON.stringify(data);
112+
window.localStorage[dataKey] = JSON.stringify(data);
74113
success(data);
75114
};
76115
// Assure a json datatype.

tests/tests.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
var eq = strictEqual;
3030

3131
test('Requirements.', function () {
32-
expect(6);
32+
expect(7);
3333

3434
// Detect native JSON parser support.
3535
var jsonSupported = function () {
@@ -53,6 +53,7 @@
5353
eq(typeof JSONCache, 'object', 'JSONCache is required');
5454
eq(typeof JSONCache.getCachedJSON, 'function', 'JSONCache is required');
5555
eq(JSONCache.settings.browserOk, true, 'JSONCache.browserOk');
56+
eq(JSONCache.settings.prefix, 'JSONCache', 'JSONCache prefix must match the one used in tests.');
5657
});
5758

5859
test('Basic localStorage functionality', function () {
@@ -97,4 +98,56 @@
9798
});
9899
});
99100

101+
// JSONCache.clear
102+
test('Cache item clearing with empty cache.', function () {
103+
expect(2);
104+
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
105+
JSONCache.clear('http://example.org/key?param1=a%20b+c#hash-123');
106+
eq(window.localStorage.length, 0, 'localStorage should be empty in the end.');
107+
});
108+
test('Whole cache clearing with empty cache.', function () {
109+
expect(2);
110+
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
111+
JSONCache.clear();
112+
eq(window.localStorage.length, 0, 'localStorage should be empty in the end.');
113+
});
114+
test('Cache item clearing with items in the cache.', function () {
115+
expect(3);
116+
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
117+
118+
var dataKey = 'JSONCache data http://example.org/key?param1=a%20b+c#hash-123';
119+
var timeKey = 'JSONCache time http://example.org/key?param1=a%20b+c#hash-123';
120+
window.localStorage[dataKey] = 'my dätä';
121+
window.localStorage[timeKey] = '123';
122+
123+
window.localStorage['öther kéy'] = 'öther dätä';
124+
125+
// Remove a certain item from the cache.
126+
JSONCache.clear('http://example.org/key?param1=a%20b+c#hash-123');
127+
128+
eq(window.localStorage.length, 1, 'There should be one item in the localStorage.');
129+
eq(window.localStorage['öther kéy'], 'öther dätä', 'Correct data should be in the localStorage.');
130+
131+
});
132+
test('Whole cache clearing with several items in the cache.', function () {
133+
expect(3);
134+
eq(window.localStorage.length, 0, 'localStorage should be empty in the beginning.');
135+
136+
var dataKey = 'JSONCache data http://example.org/key?param1=a%20b+c#hash-123';
137+
var timeKey = 'JSONCache time http://example.org/key?param1=a%20b+c#hash-123';
138+
window.localStorage[dataKey] = 'my dätä';
139+
window.localStorage[timeKey] = '123';
140+
141+
window.localStorage['JSONCache data http://example.org/data'] = 'my dätä 2';
142+
window.localStorage['JSONCache time http://example.org/time'] = '1234';
143+
144+
window.localStorage['öther kéy'] = 'öther dätä';
145+
146+
// Clear the whole cache.
147+
JSONCache.clear();
148+
149+
eq(window.localStorage.length, 1, 'There should be one item in the localStorage.');
150+
eq(window.localStorage['öther kéy'], 'öther dätä', 'Correct data should be in the localStorage.');
151+
});
152+
100153
}(jQuery));

0 commit comments

Comments
 (0)