Skip to content

Commit

Permalink
added TTL support
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Nov 17, 2011
1 parent 944a330 commit c06b4cb
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions jstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/**
* $.jStorage
*
*
* USAGE:
*
* jStorage requires Prototype, MooTools or jQuery! If jQuery is used, then
Expand All @@ -47,22 +47,22 @@
*
* -flush()
* $.jStorage.flush() -> clears the cache
*
*
* -storageObj()
* $.jStorage.storageObj() -> returns a read-ony copy of the actual storage
*
*
* -storageSize()
* $.jStorage.storageSize() -> returns the size of the storage in bytes
*
* -index()
* $.jStorage.index() -> returns the used keys as an array
*
*
* -storageAvailable()
* $.jStorage.storageAvailable() -> returns true if storage is available
*
*
* -reInit()
* $.jStorage.reInit() -> reloads the data from browser storage
*
*
* <value> can be any JSON-able value, including objects and arrays.
*
**/
Expand All @@ -71,17 +71,17 @@
if(!$ || !($.toJSON || Object.toJSON || window.JSON)){
throw new Error("jQuery, MooTools or Prototype needs to be loaded before jStorage!");
}

var
/* This is the object, that holds the cached values */
/* This is the object, that holds the cached values */
_storage = {},

/* Actual browser storage (localStorage or globalStorage['domain']) */
_storage_service = {jStorage:"{}"},

/* DOM element for older IE versions, holds userData behavior */
_storage_elm = null,

/* How much space does the storage take */
_storage_size = 0,

Expand All @@ -92,13 +92,13 @@
json_decode = $.evalJSON || (window.JSON && (JSON.decode || JSON.parse)) || function(str){
return String(str).evalJSON();
},

/* which backend is currently used */
_backend = false,

/* Next check for TTL */
_ttl_timeout,

/**
* XML encoding and decoding as XML nodes can't be JSON'ized
* XML nodes are encoded and decoded if the node is the value to be saved
Expand All @@ -108,7 +108,7 @@
* $.jStorage.set("key", {xml: xmlNode}); // NOT OK
*/
_XMLService = {

/**
* Validates a XML node to be XML
* based on jQuery.isXML function
Expand All @@ -117,7 +117,7 @@
var documentElement = (elm ? elm.ownerDocument || elm : 0).documentElement;
return documentElement ? documentElement.nodeName !== "HTML" : false;
},

/**
* Encodes a XML node to string
* based on http://www.mercurytide.co.uk/news/article/issues-when-working-ajax/
Expand All @@ -135,7 +135,7 @@
}
return false;
},

/**
* Decodes a XML node from string
* loosely based on http://outwestmedia.com/jquery-plugins/xmldom/
Expand Down Expand Up @@ -219,11 +219,11 @@
}

_load_storage();

// remove dead keys
_handleTTL();
}

/**
* Loads the data from the storage based on the supported mechanism
* @returns undefined
Expand All @@ -237,7 +237,7 @@
}else{
_storage_service.jStorage = "{}";
}
_storage_size = _storage_service.jStorage?String(_storage_service.jStorage).length:0;
_storage_size = _storage_service.jStorage?String(_storage_service.jStorage).length:0;
}

/**
Expand Down Expand Up @@ -274,14 +274,14 @@
*/
function _handleTTL(){
var curtime, i, TTL, nextExpire = Infinity, changed = false;

clearTimeout(_ttl_timeout);

if(!_storage.__jstorage_meta || typeof _storage.__jstorage_meta.TTL != "object"){
// nothing to do here
return;
}

curtime = +new Date();
TTL = _storage.__jstorage_meta.TTL;
for(i in TTL){
Expand All @@ -295,12 +295,12 @@
}
}
}

// set next check
if(nextExpire != Infinity){
_ttl_timeout = setTimeout(_handleTTL, nextExpire - curtime);
}

// save changes
if(changed){
_save();
Expand All @@ -315,7 +315,7 @@

/**
* Sets a key's value.
*
*
* @param {String} key - Key to set. If this value is not set or not
* a string an exception is raised.
* @param value - Value to set. This can be any value that is JSON
Expand All @@ -331,10 +331,10 @@
_save();
return value;
},

/**
* Looks up a key in cache
*
*
* @param {String} key - Key to look up.
* @param {mixed} def - Default value to return, if key didn't exist.
* @returns the key value, default value or <null>
Expand All @@ -352,10 +352,10 @@
}
return typeof(def) == 'undefined' ? null : def;
},

/**
* Deletes a key from cache.
*
*
* @param {String} key - Key to delete.
* @returns true if key existed or false if it didn't
*/
Expand All @@ -364,7 +364,7 @@
if(key in _storage){
delete _storage[key];
// remove from TTL list
if(_storage.__jstorage_meta &&
if(_storage.__jstorage_meta &&
typeof _storage.__jstorage_meta.TTL == "object" &&
key in _storage.__jstorage_meta.TTL){
delete _storage.__jstorage_meta.TTL[key];
Expand All @@ -374,10 +374,10 @@
}
return false;
},

/**
* Sets a TTL for a key, or remove it if ttl value is 0 or below
*
*
* @param {String} key - key to set the TTL for
* @param {Number} ttl - TTL timeout in milliseconds
* @returns true if key existed or false if it didn't
Expand All @@ -387,23 +387,23 @@
_checkKey(key);
ttl = Number(ttl) || 0;
if(key in _storage){

if(!_storage.__jstorage_meta){
_storage.__jstorage_meta = {};
}
if(!_storage.__jstorage_meta.TTL){
_storage.__jstorage_meta.TTL = {};
}

// Set TTL value for the key
if(ttl>0){
_storage.__jstorage_meta.TTL[key] = curtime + ttl;
}else{
delete _storage.__jstorage_meta.TTL[key];
}

_save();

_handleTTL();
return true;
}
Expand All @@ -412,30 +412,30 @@

/**
* Deletes everything in cache.
*
*
* @return true
*/
flush: function(){
_storage = {};
_save();
return true;
},

/**
* Returns a read-only copy of _storage
*
*
* @returns Object
*/
storageObj: function(){
function F() {}
F.prototype = _storage;
return new F();
},

/**
* Returns an index of all used keys as an array
* ['key1', 'key2',..'keyN']
*
*
* @returns Array
*/
index: function(){
Expand All @@ -447,47 +447,47 @@
}
return index;
},

/**
* How much space in bytes does the storage take?
*
*
* @returns Number
*/
storageSize: function(){
return _storage_size;
},

/**
* Which backend is currently in use?
*
*
* @returns String
*/
currentBackend: function(){
return _backend;
},

/**
* Test if storage is available
*
*
* @returns Boolean
*/
storageAvailable: function(){
return !!_backend;
},

/**
* Reloads the data from browser storage
*
*
* @returns undefined
*/
reInit: function(){
var new_storage_elm, data;
if(_storage_elm && _storage_elm.addBehavior){
new_storage_elm = document.createElement('link');

_storage_elm.parentNode.replaceChild(new_storage_elm, _storage_elm);
_storage_elm = new_storage_elm;

/* Use a DOM element to act as userData storage */
_storage_elm.style.behavior = 'url(#default#userData)';

Expand All @@ -502,7 +502,7 @@
_storage_service.jStorage = data;
_backend = "userDataBehavior";
}

_load_storage();
}
};
Expand Down

0 comments on commit c06b4cb

Please sign in to comment.