原文在这里:
http://forum.openlaszlo.org/showthread.php?p=25458#post25458
部分摘录如下:
-----------------------------------------------
localcache.lzx
-----------------------------------------------
<
library
>
< script >
var APP_NAME = " app " ;
var CACHE_VER = " 1 " ;
script >
< script >
[CDATA[
// --------------------------------------------------------------------------------
// LocalCache Constructor
// --------------------------------------------------------------------------------
function LocalCache() {
// Debug.write("created");
}
LocalCache.prototype = new Object();
LocalCache.prototype.constructor = LocalCache;
Object.registerClass( " LocalCache " , LocalCache);
// --------------------------------------------------------------------------------
// testWriteable
// --------------------------------------------------------------------------------
LocalCache.prototype.testWriteable = function () {
var retval = true;
var tstWriteObj = SharedObject.getLocal("tst");
if(!tstWriteObj.flush(10000)) {
// local cache is disabled
retval = false;
}
delete tstWriteObj;
return retval;
}
// --------------------------------------------------------------------------------
// saveLocalKeyDataPair
// --------------------------------------------------------------------------------
LocalCache.prototype.saveLocalKeyDataPair = function (key, data) {
if (this.testWriteable()){
if (this.checkLocalKey(key)){
this.saveLocalData(key,data);
return "Saved the data, overwriting anything that was there";
} else {
this.saveLocalKey(key);
this.saveLocalData(key,data);
return "Created the key and saved the data.";
}
} else {
return "The user has disabled the local shared object";
}
}
// --------------------------------------------------------------------------------
// getLocalData
// --------------------------------------------------------------------------------
LocalCache.prototype.getLocalData = function (key) {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
return localObj.data[key];
}
// --------------------------------------------------------------------------------
// getLocalKeys
// --------------------------------------------------------------------------------
LocalCache.prototype.getLocalKeys = function () {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
return localObj.data.keys;
}
// --------------------------------------------------------------------------------
// saveLocalKey
// --------------------------------------------------------------------------------
LocalCache.prototype.saveLocalKey = function (key) {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
if(localObj.data.keys == null) {
localObj.data.keys = new Array(key);
} else {
if( this.checkLocalKey(key)){
// then it already exists
} else {
localObj.data.keys.push(key);
localObj.data[key] = "";
}
}
localObj.flush();
delete localObj;
}
// --------------------------------------------------------------------------------
// checkLocalKey
// --------------------------------------------------------------------------------
LocalCache.prototype.checkLocalKey = function (key) {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
var len = localObj.data.keys.length;
var retval = false;
for(i=0; i<len; i++){
if(localObj.data.keys[i] == key){
// it exists
retval = true;
}
}
return retval;
}
// --------------------------------------------------------------------------------
// deleteLocalKey
// --------------------------------------------------------------------------------
LocalCache.prototype.deleteLocalKey = function (key) {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
var len = localObj.data.keys.length;
// delete the data if it exists first
this.deleteLocalData(key);
// then find and remove the key from the list
for(i=0; i<len; i++){
if(localObj.data.keys[i] == key)
< script >
var APP_NAME = " app " ;
var CACHE_VER = " 1 " ;
script >
< script >
[CDATA[
// --------------------------------------------------------------------------------
// LocalCache Constructor
// --------------------------------------------------------------------------------
function LocalCache() {
// Debug.write("created");
}
LocalCache.prototype = new Object();
LocalCache.prototype.constructor = LocalCache;
Object.registerClass( " LocalCache " , LocalCache);
// --------------------------------------------------------------------------------
// testWriteable
// --------------------------------------------------------------------------------
LocalCache.prototype.testWriteable = function () {
var retval = true;
var tstWriteObj = SharedObject.getLocal("tst");
if(!tstWriteObj.flush(10000)) {
// local cache is disabled
retval = false;
}
delete tstWriteObj;
return retval;
}
// --------------------------------------------------------------------------------
// saveLocalKeyDataPair
// --------------------------------------------------------------------------------
LocalCache.prototype.saveLocalKeyDataPair = function (key, data) {
if (this.testWriteable()){
if (this.checkLocalKey(key)){
this.saveLocalData(key,data);
return "Saved the data, overwriting anything that was there";
} else {
this.saveLocalKey(key);
this.saveLocalData(key,data);
return "Created the key and saved the data.";
}
} else {
return "The user has disabled the local shared object";
}
}
// --------------------------------------------------------------------------------
// getLocalData
// --------------------------------------------------------------------------------
LocalCache.prototype.getLocalData = function (key) {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
return localObj.data[key];
}
// --------------------------------------------------------------------------------
// getLocalKeys
// --------------------------------------------------------------------------------
LocalCache.prototype.getLocalKeys = function () {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
return localObj.data.keys;
}
// --------------------------------------------------------------------------------
// saveLocalKey
// --------------------------------------------------------------------------------
LocalCache.prototype.saveLocalKey = function (key) {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
if(localObj.data.keys == null) {
localObj.data.keys = new Array(key);
} else {
if( this.checkLocalKey(key)){
// then it already exists
} else {
localObj.data.keys.push(key);
localObj.data[key] = "";
}
}
localObj.flush();
delete localObj;
}
// --------------------------------------------------------------------------------
// checkLocalKey
// --------------------------------------------------------------------------------
LocalCache.prototype.checkLocalKey = function (key) {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
var len = localObj.data.keys.length;
var retval = false;
for(i=0; i<len; i++){
if(localObj.data.keys[i] == key){
// it exists
retval = true;
}
}
return retval;
}
// --------------------------------------------------------------------------------
// deleteLocalKey
// --------------------------------------------------------------------------------
LocalCache.prototype.deleteLocalKey = function (key) {
var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);
var len = localObj.data.keys.length;
// delete the data if it exists first
this.deleteLocalData(key);
// then find and remove the key from the list
for(i=0; i<len; i++){
if(localObj.data.keys[i] == key)