一个openlaszlo使用flash的共享对象实现保存客户端信息的例子

本文介绍了一个用于维护应用程序状态的本地缓存实现方案。通过定义特定的键值对来存储和检索数据,支持创建临时的本地文件存储。文章详细解释了如何进行数据的保存、读取及删除等操作。

原文在这里:
http://forum.openlaszlo.org/showthread.php?p=25458#post25458

部分摘录如下:

-----------------------------------------------
localcache.lzx
-----------------------------------------------

< library >

<!---
*  LocalCache
*
*  A dictionary  for  localy cached data.
*  It allows an application to maintain state or to enable the app
*  to provide a user  with  an interface to create a list of keys
*  and save and load data  for  each key,
*  essentially creating a temporary local file store
*  of xml documents or datasets
*
*  We have APP_NAME and CACHE_VER constants
*  that we use to version the keysets locally
*  so that anytime that an app needs to make a change
*  to the local datastructure it can bump up the version
*  to avoid datastructure version problems between instances  in  the wild
*  the appname is so that different apps from the same domain
*  and which use the same code, don ' t step on each other ' s data
*
*  expected use  for  maintaining application state
*   //  oninit
*   if  (gLocalCache.testWriteable())
*   if (gLocalCache.checkLocalKey( " mysavedstate " )) ... {
* mydata=gLocalCache.getLocalData("mydataname");
* // create LzDataElement with mydata
* }
  else   ... {
* // create key and initialize with data
* gLocalCache.saveLocalKey("mydataname");
* gLocalCache.saveLocalData("mydataname", ds_state.serialize());
* }

*  }  else   ... {
* // deal with the case where local data has been disabled
* }

*
-->

<!--  CONSTANTS  -->
< script >
var  APP_NAME  = " app " ;
var  CACHE_VER  =   " 1 " ;
</ script >

<!--  CLASSES  -->
< 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 ...{
ifthis.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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值