最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

memcache怎么存储的对象

来源:动视网 责编:小采 时间:2020-11-09 16:30:21
文档

memcache怎么存储的对象

memcache怎么存储的对象:memchache 将对象序列化后保存 memcahce将值序列化成字节数组,然后存储到缓存中。 如下例,我们将user对象序列化到文件a.txt中,同时将user保存到缓存中,通过比较文件和缓存中的值 发现,两者是一样的。 public class User implements
推荐度:
导读memcache怎么存储的对象:memchache 将对象序列化后保存 memcahce将值序列化成字节数组,然后存储到缓存中。 如下例,我们将user对象序列化到文件a.txt中,同时将user保存到缓存中,通过比较文件和缓存中的值 发现,两者是一样的。 public class User implements


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.java

protected 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;
 }

结论

值的存储,都是序列化成字节数组,然后保存

文档

memcache怎么存储的对象

memcache怎么存储的对象:memchache 将对象序列化后保存 memcahce将值序列化成字节数组,然后存储到缓存中。 如下例,我们将user对象序列化到文件a.txt中,同时将user保存到缓存中,通过比较文件和缓存中的值 发现,两者是一样的。 public class User implements
推荐度:
标签: 如何 存储 怎么
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top