Skip to content

Instantly share code, notes, and snippets.

@stefano-bortolotti
Created April 17, 2013 15:20
Show Gist options
  • Save stefano-bortolotti/5405181 to your computer and use it in GitHub Desktop.
Save stefano-bortolotti/5405181 to your computer and use it in GitHub Desktop.
JS - FIFO stack
function uMemory() {
var props = {
'blocksLimit' : 5
};
var queue = [];
var blocks = {};
this.init = function (options) {
props = $.extend(props, options);
//console.dir(props);
};
this.save = function (page, data) {
queue.unshift(page);
blocks['p' + page] = data;
return checkSizeLimit(page);
}
this.get = function (page) {
var p = blocks['p' + page];
return p ? p : false;
}
this.status = function() {
/*console.log('Mem Queue ', queue);
console.log('Mem Blocks ', blocks);*/
}
this.clear = function() {
queue = [];
blocks = {};
}
var checkSizeLimit = function (page) {
/*console.log('q length: ' + queue.length + ', limit: ' + props.blocksLimit);*/
if ( queue.length > props.blocksLimit ) {
var oldest = queue.pop();
blocks['p' + oldest] = null;
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment