added Cache
This commit is contained in:
parent
dacd2ee019
commit
c3d1ce7d24
8 changed files with 280 additions and 105 deletions
64
WebKit/scripts/helper/CacheStorage.js
Normal file
64
WebKit/scripts/helper/CacheStorage.js
Normal file
|
@ -0,0 +1,64 @@
|
|||
define([
|
||||
],
|
||||
|
||||
function() {
|
||||
|
||||
function CacheStorage(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
CacheStorage.prototype.mkPath = function(key) {
|
||||
return this.mkInternalPath("") + "-" + key;
|
||||
}
|
||||
|
||||
CacheStorage.prototype.mkInternalPath = function(key) {
|
||||
return "tentia-cache-" + this.name + key;
|
||||
};
|
||||
|
||||
CacheStorage.prototype.getItem = function(key) {
|
||||
return localStorage.getItem(this.mkPath(key));
|
||||
}
|
||||
|
||||
CacheStorage.prototype.setItem = function(key, value) {
|
||||
var item = this.getItem(key);
|
||||
|
||||
localStorage.setItem(this.mkPath(key), value);
|
||||
|
||||
if (!item) {
|
||||
var length_path = this.mkInternalPath("_length");
|
||||
var length = parseInt(localStorage.getItem(length_path), 10) + 1;
|
||||
localStorage.setItem(length_path, length);
|
||||
}
|
||||
}
|
||||
|
||||
CacheStorage.prototype.removeItem = function(key) {
|
||||
|
||||
var item = this.getItem(key);
|
||||
|
||||
localStorage.removeItem(this.mkPath(key));
|
||||
|
||||
if (item) {
|
||||
var length_path = this.mkInternalPath("_length");
|
||||
var length = parseInt(localStorage.getItem(length_path), 10) - 1;
|
||||
localStorage.setItem(length_path, length);
|
||||
}
|
||||
};
|
||||
|
||||
CacheStorage.prototype.clear = function() {
|
||||
for (var i = 0; i < localStorage.length; i++) {
|
||||
var key = localStorage.key(i);
|
||||
if (key.startsWith(this.mkPath(""))) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
|
||||
localStorage.setItem(this.mkInternalPath("_length"), 0);
|
||||
}
|
||||
|
||||
CacheStorage.prototype.length = function() {
|
||||
return parseInt(localStorage.getItem(this.mkInternalPath("_length")), 10);
|
||||
}
|
||||
|
||||
return CacheStorage;
|
||||
|
||||
});
|
Reference in a new issue