JSCache 1.2

Introduction

JSCache 1.2 is a simple script written in JavaScript, which makes it possible to cache data in user's browser memory. JSCache use a globalStorage mechanism, added in HTML 5. Data will be memorize even after browser closed, and when you run again data will be accessible.

In Firefox the default storage size for an entire domain is 5MB, but Opera, probably may only provide 3MB. Now only Firefox serves globalStorage. By that reason unfortunately, script will not be comprehensive.

JSCache meaningfully accelerates time of access to data downloaded earlier. It reduces frequency of communication between browser and server through AJAX. It will certainly matter with web applications.

Download

How to use?

// Define constructor
var cache = new JSCache('Domain');

// add data to cache
// parameters: ID - [String] identyficator 
//            data - [String] data to caching
//            options - it is not require to give it:
//             *ttl: [Int] 'time to live' - time in seconds, after
//                   that object will be removed
//             *overwrite: [true/false] accept values true or false, implicitly 
//                         it is possible to overwrite object with the same ID after expiration.
cache.save('ID' [String], 'dane' [String], [{ttl: [Int], overwrite: [true/false]}]);

// returns object from cache
// accept parameter ID
cache.get('ID');

// remove object from cache
// accept parameter ID
// removes an item from the cache for a given ID
cache.remove('ID');

// removes all items from cache
cache.flush();

Examples

var cache = new JSCache('localhost.localdomain');

var myJSONObject = {"bindings": [
    {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
    {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
    {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
  ]
};

cache.save('as', myJSONObject, {ttl: 5});

var ob = cache.get('obj1');
alert(ob.bindings[0].method);
cache.save('foo', Math.random(), {ttl: 2});

cache.save('foo', Math.random(), {overwrite: true});

cache.save('foo', Math.random(), {ttl: 5000; overwrite: true});

alert(cache.get('foo'));

cache.save('boo', Math.random());

cache.remove('foo');

alert(cache.get('foo'));

cache.flush();

alert(cache.get('foo')+cache.get('boo'));

License

/*
 * JSCache 1.1
 *
 * (c) 2007 Jarek Kostrz
 *  e-mail: 
 *  www:  http://jscache.ajaxin.pl
 *        http://ajaxin.pl
 *  http://creativecommons.org/licenses/by/2.5/pl/
 *  In case of commercial use please inform me about that fact.
 */