原文連結:http://bbs.9ria.com/thread-284082-1-2.html
之前做一個資料快取,就順便把寫入快取,清除快取,獲取快取都整理了一下,其中也做了些參考,個人水平有限,有需要的就拿去改改用吧。
上程式碼:
package
{
importflash.net.SharedObject;
importflash.system.Security;
importflash.system.SecurityPanel;
public classcookie
{
private varso:SharedObject;
private var_name:String
publicfunction cookie(file_name:String)
{
//constructor code
so =SharedObject.getLocal(file_name,"/");
_name=file_name
}
publicfunction flushData(key:String,value:*):void
{
//新增資料
if(so.data.cookie == null)
{
varobj:Object = {};
obj[key] =value;
so.data.cookie= obj;
}else
{
so.data.cookie[key]= value;
}
trace(key+":"+so.data.cookie[key]);
flush();
}
publicfunction deleteData(key:String):void
{
//刪除資料值
if(judge(key))
{
deleteso.data.cookie[key];
flush();
}
}
publicfunction getName():String
{
//獲取資料名
return_name
}
publicfunction getData(key:String):*
{
//獲取資料值
if(judge(key))
{
returnso.data.cookie[key];
}else
{
returnnull;
}
}
publicfunction clearData():void
{
//清除快取
so.clear();
}
publicfunction judge(key:String):Boolean
{
//判斷資料是否存在
returnso.data.cookie != undefined && so.data.cookie[key] !=undefined
}
privatefunction flush():void
{
//寫入
if(so)
{
try
{
so.flush();
}
catch(e:Error)
{
Security.showSettings();
Security.showSettings( SecurityPanel.LOCAL_STORAGE );
}
}
}
}
}
標註的都比較清楚,直接例項化,呼叫方法可以了,附件為cookie.as檔案 |