您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 晋城分类信息网,免费分类信息发布

memcache 怎么存储的对象

2024/3/16 19:15:12发布20次查看
memchache 将对象序列化后保存 memcahce将值序列化成字节数组,然后存储到缓存中。 如下例,我们将user对象序列化到文件a.txt中,同时将user保存到缓存中,通过比较文件和缓存中的值 发现,两者是一样的。 public class user implements serializable { priv
memchache 将对象序列化后保存memcahce将值序列化成字节数组,然后存储到缓存中。
如下例,我们将user对象序列化到文件a.txt中,同时将user保存到缓存中,通过比较文件和缓存中的值
发现,两者是一样的。
public class user implements serializable{ private string name; private string address; public user(string name, string address) { super(); this.name = name; this.address = address; }}
public static void main(string[] args) throws interruptedexception, executionexception, filenotfoundexception, ioexception { memcachedclient mcc = null; try{ // 本地连接 memcached 服务 mcc = new memcachedclient(new inetsocketaddress(127.0.0.1, 11211)); system.out.println(connection to server sucessful.); }catch(exception ex){ system.out.println( ex.getmessage() ); } user u = new user(junwang,qingdao city); objectoutputstream oos = new objectoutputstream(new fileoutputstream(new file(a.txt)) ); oos.writeobject(u); future fo = mcc.set(myuser, 5*60*1000, u); // 查看存储状态 system.out.println(set status: + fo.get()); // 输出值 system.out.println(myuser value in cache - + mcc.get(myuser)); // 关闭连接 mcc.shutdown(); }
查看源代码查看源代码,可以发现正是将对象序列化,然后保存。证明了我们上述的猜想。
baseserializingtranscoder.javaprotected byte[] serialize(object o) { if (o == null) { throw new nullpointerexception(can't serialize null); } byte[] rv=null; bytearrayoutputstream bos = null; objectoutputstream os = null; try { bos = new bytearrayoutputstream(); os = new objectoutputstream(bos); os.writeobject(o); os.close(); bos.close(); rv = bos.tobytearray(); } catch (ioexception e) { throw new illegalargumentexception(non-serializable object, e); } finally { closeutil.close(os); closeutil.close(bos); } return rv; }
结论值的存储,都是序列化成字节数组,然后保存
晋城分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录