Skip to content

Commit

Permalink
Merge pull request #13 from jpodwys/localstorage-fix
Browse files Browse the repository at this point in the history
Fix a localstorage quota bug
  • Loading branch information
Joe Podwys authored Feb 18, 2019
2 parents b68b1bc + 5ae6ab9 commit d81b4ef
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 6 deletions.
9 changes: 6 additions & 3 deletions cacheModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function cacheModule(config){
refreshKeys: {}
};
var storageKey;
var interval;

setupBrowserStorage();
log(false, 'Cache-module client created with the following defaults:', {type: self.type, defaultExpiration: self.defaultExpiration, verbose: self.verbose, readOnly: self.readOnly});
Expand Down Expand Up @@ -164,7 +165,7 @@ function cacheModule(config){
delete cache.db[keys];
delete cache.expirations[keys];
delete cache.refreshKeys[keys];
if(cb) cb(null, 1);
if(cb) cb(null, 1);
}
overwriteBrowserStorage();
}
Expand All @@ -180,6 +181,8 @@ function cacheModule(config){
cache.refreshKeys = {};
if(cb) cb();
overwriteBrowserStorage();
if(interval) clearInterval(interval);
backgroundRefreshEnabled = false;
}

/**
Expand Down Expand Up @@ -217,8 +220,8 @@ function cacheModule(config){
var db = cache;
try {
db = JSON.stringify(db);
store.setItem(storageKey, db);
} catch (err) { /* Do nothing */ }
store.setItem(storageKey, db);
}
}

Expand Down Expand Up @@ -252,7 +255,7 @@ function cacheModule(config){
throw new Error('BACKGROUND_REFRESH_INTERVAL_EXCEPTION: backgroundRefreshInterval cannot be greater than backgroundRefreshMinTtl.');
}
}
setInterval(backgroundRefresh, self.backgroundRefreshInterval);
interval = setInterval(backgroundRefresh, self.backgroundRefreshInterval);
}
}

Expand Down
208 changes: 208 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "cache-service-cache-module",
"version": "2.0.0",
"version": "2.0.1",
"description": "A cache plugin for cache-service.",
"main": "cacheModule.js",
"devDependencies": {
"mocha": "2.2.4",
"expect": "1.6.0",
"mocha": "^5.2.0",
"mock-localstorage": "0.1.3"
},
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion test/server/cache-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ beforeEach(function(){
cacheModule.flush();
});

after(function(){
cacheModule.flush();
});

describe('cacheModule Tests', function () {
it('Getting absent key should return null', function (done) {
cacheModule.get(key, function (err, result){
Expand Down Expand Up @@ -92,7 +96,7 @@ describe('cacheModule Tests', function () {
var refresh = function(key, cb){
cb(null, 1);
}
cacheModule.set(key, value, 1, refresh, function (err, result){
cacheModule.set(key, value, 1, refresh, function (err, result){
setTimeout(function(){
cacheModule.get(key, function (err, response){
expect(response).toBe(1);
Expand Down

0 comments on commit d81b4ef

Please sign in to comment.