myStorage.js
What is it?
myStorage.js is a class for cache of objects in the browser. in order to avoid unnecessary queries to the application server in Ajax it can cache the results of a Ajax request and thereby avoid seeking into application server.
Documentation
Quick Start
<html>
<head>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="myStorage.js"></script>
<script type="text/javascript">
window.onload = function () {
storage = new myStorage ();
}
</script>
</head>
<body/>
</html>
Sample with Ajax
function loadData (id) {
var key = 'item-' + id;
var item;
/** get data if exists */
if ((item = storage.get (key)) !== false) {
return item;
}
new Ajax.Request ('data.php', {
onSuccess: function (xhr) {
var json = xhr.responseText.evalJSON ();
if (json.response && json.response == 'OK') {
/** add data 1 for hour */
storage.set (key, json.data, 3600);
return json.data;
}
}
});
}