[storage] use haxe Serializer/Deserializer
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
package haxework.storage;
|
package haxework.storage;
|
||||||
|
|
||||||
import flash.utils.ByteArray;
|
|
||||||
|
|
||||||
interface IStorage {
|
interface IStorage {
|
||||||
public function exists(key:String):Bool;
|
public function exists(key:String):Bool;
|
||||||
public function write(key:String, value:ByteArray):Void;
|
|
||||||
public function read(key:String):Null<ByteArray>;
|
public function write<T>(key:String, value:T):Void;
|
||||||
|
|
||||||
|
public function read<T>(key:String):Null<T>;
|
||||||
|
|
||||||
|
public function clear():Void;
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
package haxework.storage;
|
package haxework.storage;
|
||||||
|
|
||||||
import flash.utils.ByteArray;
|
|
||||||
|
|
||||||
class NoStorage implements IStorage {
|
class NoStorage implements IStorage {
|
||||||
|
|
||||||
public function exists(key:String):Bool {
|
public function exists(key:String):Bool {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function write(key:String, value:ByteArray):Void {}
|
public function write<T>(key:String, value:T):Void {}
|
||||||
|
|
||||||
public function read(key:String):Null<ByteArray> {
|
public function read<T>(key:String):Null<T> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function clear():Void {}
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,36 @@
|
|||||||
package haxework.storage;
|
package haxework.storage;
|
||||||
|
|
||||||
import haxe.crypto.Md5;
|
|
||||||
import flash.net.SharedObject;
|
import flash.net.SharedObject;
|
||||||
import flash.utils.ByteArray;
|
import haxe.Serializer;
|
||||||
|
import haxe.Unserializer;
|
||||||
|
|
||||||
class SharedObjectStorage implements IStorage {
|
class SharedObjectStorage implements IStorage {
|
||||||
|
|
||||||
public function exists(key:String):Bool {
|
private var so:SharedObject;
|
||||||
var so:SharedObject = SharedObject.getLocal("storage/" + Md5.encode(key));
|
|
||||||
return so.size > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function write(key:String, value:ByteArray):Void {
|
public function new(name:String = "storage") {
|
||||||
var so:SharedObject = SharedObject.getLocal("storage/" + Md5.encode(key));
|
so = SharedObject.getLocal(name);
|
||||||
so.setProperty("value", value);
|
}
|
||||||
so.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function read(key:String):Null<ByteArray> {
|
public function exists(key:String):Bool {
|
||||||
var so:SharedObject = SharedObject.getLocal("storage/" + Md5.encode(key));
|
return Reflect.hasField(so.data, key);
|
||||||
return so.data.value;
|
}
|
||||||
}
|
|
||||||
|
public function write<T>(key:String, value:T):Void {
|
||||||
|
so.setProperty(key, Serializer.run(value));
|
||||||
|
so.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function read<T>(key:String):Null<T> {
|
||||||
|
if (exists(key)) {
|
||||||
|
var data = Reflect.field(so.data, key);
|
||||||
|
return new Unserializer(data).unserialize();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear():Void {
|
||||||
|
so.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
11
src/main/haxework/utils/ObjectUtil.hx
Normal file
11
src/main/haxework/utils/ObjectUtil.hx
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package haxework.utils;
|
||||||
|
|
||||||
|
import haxe.Unserializer;
|
||||||
|
import haxe.Serializer;
|
||||||
|
|
||||||
|
class ObjectUtil {
|
||||||
|
|
||||||
|
public static function clone<T>(object:T):T {
|
||||||
|
return new Unserializer(Serializer.run(object)).unserialize();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user