Skip to content

Commit

Permalink
Merge branch 'release/5.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
dlueth committed Mar 2, 2018
2 parents 8ab879a + aff71b0 commit 4b313f4
Show file tree
Hide file tree
Showing 26 changed files with 86 additions and 80 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ By default demand will invalidate a modules cache under the following conditions
Demand will, in addition, do its best to keep leftover garbage to a minimum. It does so by starting an automatic garbage collection for expired caches on load. In addition it will also clear a specific cache if it gets requested and is found to be invalid for any reason.
When localStorage quota is exceeded while trying to cache yet another module Qoopido.demand will load a special module ```/demand/cache/dispose``` and will try to free the required space by clearing existing caches in order of last access time, from oldest to newest.
When localStorage quota is exceeded while trying to cache yet another module Qoopido.demand will load a special module ```/demand/cache/dispose``` and will try to free the required space by clearing existing caches in order of last access time, from oldest to newest.
Beside the automatic cache invalidation demand still offers manual control by registering a ```demand.cache``` object to the global demand function. This object offers the following methods to control the cache:
Expand Down
67 changes: 11 additions & 56 deletions addon/cache/dispose.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,33 @@
(function(localStorage) {
(function() {
'use strict';

function definition(functionIterate) {
var PREFIX = 'demand',
SUFFIX = 'state',
regexMatchState = new RegExp('^\\[' + PREFIX + '\\]\\[(.+?)\\]\\[' + SUFFIX + '\\]$'),
regexMatchProperties = /^(.+?),(\d+),(\d*),(.+?),(\d+)$/;

function getState(key) {
var state = localStorage.getItem(key),
matches;

if(state && (matches = state.match(regexMatchProperties))) {
return Array.prototype.slice.call(matches, 1);
}
}

function getKey(id) {
return '[' + PREFIX + '][' + id + '][' + SUFFIX + ']';
}

functionIterate(localStorage, function(property) {
var match = property.match(regexMatchState),
state;

if(match) {
state = getState(getKey(match[1]));

if(!state[4]) {
demand.clear.path(match[1]);
}
}
});

function filterStates(property) {
var match = property.match(regexMatchState),
state;

if(match) {
state = getState(getKey(match[1]));
state[5] = match[1];

this.push(state);
}
}

function definition(functionIterate, cacheStates) {
function compareAccess(a, b) {
if(a[4] < b[4]) {
if(a.accessed < b.accessed) {
return -1;
}

if(a[4] > b[4]) {
if(a.accessed > b.accessed) {
return 1;
}

return 0;
}

return function cacheDispose(size) {
var states = [],
state;

functionIterate(localStorage, filterStates, states);
var states = cacheStates(),
item;

states.sort(compareAccess);

while(size > 0 && states.length) {
state = states.shift();
size -= state[1];
item = states.shift();
size -= item.size;

demand.clear.path(state[5]);
demand.cache.clear(item.id);
}
};
}

provide([ '/demand/function/iterate' ], definition);
}(localStorage));
provide([ '/demand/function/iterate', './states' ], definition);
}());
47 changes: 47 additions & 0 deletions addon/cache/states.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(function(localStorage) {
'use strict';

function definition(functionIterate) {
var PREFIX = 'demand',
SUFFIX = 'state',
regexMatchState = new RegExp('^\\[' + PREFIX + '\\]\\[(.+?)\\]\\[' + SUFFIX + '\\]$'),
regexMatchProperties = /^(.+?),(\d+),(\d*),(.+?),(\d+)$/;

function getState(key) {
var state = localStorage.getItem(key),
matches;

if(state && (matches = state.match(regexMatchProperties))) {
return Array.prototype.slice.call(matches, 1);
}
}

function getKey(id) {
return '[' + PREFIX + '][' + id + '][' + SUFFIX + ']';
}

function filterStates(property) {
var match = property.match(regexMatchState),
state;

if(match) {
state = getState(getKey(match[1]));

this.push({
id: match[1],
version: state[0],
size: parseInt(state[1], 10),
expires: state[2] ? new Date(parseInt(state[2], 10)) : null,
demand: state[3],
accessed: new Date(parseInt(state[4], 10))
});
}
}

return function cacheStats() {
return functionIterate(localStorage, filterStates, []);
};
}

provide([ '/demand/function/iterate' ], definition);
}(localStorage));
4 changes: 2 additions & 2 deletions dist/cache/dispose.js

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

2 changes: 1 addition & 1 deletion dist/cache/dispose.js.map

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

3 changes: 3 additions & 0 deletions dist/cache/states.js

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

1 change: 1 addition & 0 deletions dist/cache/states.js.map

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

4 changes: 2 additions & 2 deletions dist/demand.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/demand.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/handler/css.js

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

Loading

0 comments on commit 4b313f4

Please sign in to comment.