zhouerla 发表于 2015-8-31 13:13:54

memcached demo 应用例子

前面已发布了linux memcache的安装,这写了一个小例子,这在不再详细介绍。
安装说明:http://sunney2010.javaeye.com/blog/656905
   Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据 库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon)是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。但是它并不提供冗余(例如,复制其hashmap条目); 当某个服务器S停止运行或崩溃了,所有存放在S上的键/值对都将丢失。
  Memcached由DangaInteractive开发,用于提升LiveJournal.com访问速度的。LJ每秒动态页面访问量几千次,用户700万。Memcached将数 据库负载大幅度降低,更好的分配资源,更快速访问。


1 package com.ctoall.core;
2
3 import java.io.Serializable;
4
5 public class StudentDO implements Serializable {
6
7   /**
8      *
9      */
10   private static final long serialVersionUID = 3312721104787630488L;
11   private int Id;
12   private String Name;
13   private int age;
14   private String address;
15   public int getId() {
16         return Id;
17   }
18   public void setId(int id) {
19         Id = id;
20   }
21   public String getName() {
22         return Name;
23   }
24   public void setName(String name) {
25         Name = name;
26   }
27   public int getAge() {
28         return age;
29   }
30   public void setAge(int age) {
31         this.age = age;
32   }
33   public String getAddress() {
34         return address;
35   }
36   public void setAddress(String address) {
37         this.address = address;
38   }
39   
40 }  
  1 package com.ctoall.core;


2
3 import java.util.Date;
4
5 import com.danga.MemCached.MemCachedClient;
6 import com.danga.MemCached.SockIOPool;
7
8
9 public class MemCached {
10      // 创建全局的唯一实例
11   protected static MemCachedClient mcc = new MemCachedClient();
12   
13   protected static MemCached memCached = new MemCached();
14   // 设置与缓存服务器的连接池
15   static {
16         // 服务器列表和其权重
17         String[] servers = {"192.168.186.128:11211"};
18         Integer[] weights = {3};
19
20         // 获取socke连接池的实例对象
21         SockIOPool pool = SockIOPool.getInstance();
22
23         // 设置服务器信息
24         pool.setServers( servers );
25         pool.setWeights( weights );
26
27         // 设置初始连接数、最小和最大连接数以及最大处理时间
28         pool.setInitConn( 5 );
29         pool.setMinConn( 5 );
30         pool.setMaxConn( 250 );
31         pool.setMaxIdle( 1000 * 60 * 60 * 6 );
32
33         // 设置主线程的睡眠时间
34         pool.setMaintSleep( 30 );
35
36         // 设置TCP的参数,连接超时等
37         pool.setNagle( false );
38         pool.setSocketTO( 3000 );
39         pool.setSocketConnectTO( 0 );
40
41         // 初始化连接池
42         pool.initialize();
43
44         // 压缩设置,超过指定大小(单位为K)的数据都会被压缩
45         mcc.setCompressEnable( true );
46         mcc.setCompressThreshold( 64 * 1024 );
47   }
48   /**
49      * 保护型构造方法,不允许实例化!
50      *
51      */
52   protected MemCached()
53   {
54         
55   }
56   
57   /**
58      * 获取唯一实例.
59      * @return
60      */
61   public static MemCached getInstance()
62   {
63         return memCached;
64   }
65   
66   /**
67      * 添加一个指定的值到缓存中.
68      * @param key
69      * @param value
70      * @return
71      */
72   public boolean add(String key, Object value)
73   {
74         return mcc.add(key, value);
75   }
76   
77   public boolean add(String key, Object value, Date expiry)
78   {
79         return mcc.add(key, value, expiry);
80   }
81   
82   public boolean replace(String key, Object value)
83   {
84         return mcc.replace(key, value);
85   }
86   
87   public boolean replace(String key, Object value, Date expiry)
88   {
89         return mcc.replace(key, value, expiry);
90   }
91   
92   /**
93      * 根据指定的关键字获取对象.
94      * @param key
95      * @return
96      */
97   public Object get(String key)
98   {
99         return mcc.get(key);
100   }
101   
102   public static void main(String[] args)
103   {
104         MemCached cache = MemCached.getInstance();
105      StudentDO stuDO=new StudentDO();
106      stuDO.setId(10);
107      stuDO.setAge(28);
108      stuDO.setName("sunney");
109      stuDO.setAddress("广东省深圳市福田区梅华路105号多丽科技楼9楼");
110      cache.add("stu", stuDO);
111      StudentDO stuDO1=(StudentDO)cache.get("stu");
112      System.out.println("id:"+stuDO1.getId());
113      System.out.println("name:"+stuDO1.getName());
114      System.out.println("age:"+stuDO1.getAge());
115      System.out.println("adress:"+stuDO1.getAddress());
116      System.out.println("+++++++++++++++++++++++ok++++++++++++++++++++++++");
117            
118   }
119 }  
  memcached demo下载地址:http://sunney2010.javaeye.com/admin/blogs/695106
  
  
页: [1]
查看完整版本: memcached demo 应用例子