首页 > 代码库 > Java MemCached 缓存实现

Java MemCached 缓存实现


首先创建MemCached 缓存管理类,此代码测试需要添加 java_memcached-release_2.6.3.jar,commons-pool-1.5.6.jar,slf4j-api-1.6.1.jar,slf4j-simple-1.6.1.jar 这几个jar包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.util.Date;
 
import org.apache.commons.net.telnet.TelnetClient;
 
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
 
/**
 * 缓存管理类
 *
 * @author  @ 2014-4-24
 */
public class MemCachedManager {
 
    /**
     * 记录日志
     */
    private static LoggerUtil log = LoggerUtil.getInstance(MemCachedManager.class);
 
    /**
     * 创建缓存客户端
     */
    public static MemCachedClient mcc = new MemCachedClient();
 
    /**
     * 缓存管理对象
     */
    private static MemCachedManager memCached = null;
 
    private static boolean isSuccess = true;
    // 设置与缓存服务器的连接池
    static {
         
        //判断缓存是否服务是否开启
        TelnetClient telnet = new TelnetClient();
        try {
            String[] sers="192.168.15.1:11211".split(":");
            telnet.connect(sers[0],Integer.parseInt(sers[1]));
        } catch (Exception e) {
            log.error("缓存初始化错误", e);
            isSuccess = false;
        }
 
        // 服务器列表和其权重
        String[] servers = {"192.168.15.1:11211"};
        Integer[] weights = {3};
 
        // 获取socke连接池的实例对象
        SockIOPool pool = SockIOPool.getInstance();
 
        // 设置服务器信息
        pool.setServers(servers);
        pool.setWeights(weights);
 
        // 设置初始连接数、最小和最大连接数以及最大处理时间
        pool.setInitConn(5);
        pool.setMinConn(5);
        pool.setMaxConn(250);
        pool.setMaxIdle(1000 * 60 * 60 * 6);
 
        // 设置主线程的睡眠时间
        pool.setMaintSleep(30);
 
        // 设置TCP的参数,连接超时等
        pool.setNagle(false);
        pool.setSocketTO(1000);
        pool.setSocketConnectTO(0);
 
        // 初始化连接池
        pool.initialize();
 
    }
 
    /**
     * 私有构造方法,不允许实例化!
     *
     */
    private MemCachedManager() {
    }
 
    /**
     * 获取唯一实例.
     *
     * @return 缓存管理对象
     */
    public static MemCachedManager getInstance() {
        if (memCached == null) {
            memCached = new MemCachedManager();
        }
 
        return memCached;
 
    }
 
    /**
     * 添加一个指定的值到缓存中.
     *
     * @param key
     *            主键
     * @param value
     *            值
     * @return 是否成功
     */
    public boolean add(String key, Object value) {
        if (isSuccess) {
            return mcc.add(key, value);
        } else {
            return false;
        }
    }
 
    /**
     * 添加一个指定的值到缓存中.
     *
     * @param key
     *            主键
     * @param value
     *            值
     * @param expiry
     *            失效时间
     * @return 是否成功
     */
    public boolean add(String key, Object value, Date expiry) {
        if (isSuccess) {
            return mcc.add(key, value, expiry);
        } else {
            return false;
        }
    }
 
    /**
     * 替换缓存
     *
     * @param key
     *            主键
     * @param value
     *            值
     * @return 是否成功
     */
    public boolean replace(String key, Object value) {
        if (isSuccess) {
            return mcc.replace(key, value);
        } else {
            return false;
        }
    }
 
    /**
     * 替换缓存
     *
     * @param key
     *            主键
     * @param value
     *            值
     * @param expiry
     *            失效时间
     * @return 是否成功
     */
    public boolean replace(String key, Object value, Date expiry) {
        if (isSuccess) {
            return mcc.replace(key, value, expiry);
        } else {
            return false;
        }
    }
 
    /**
     * 根据指定的关键字获取对象.
     *
     * @param key
     *            主键
     * @return 缓存对象的值
     */
    public Object get(String key) {
        if (isSuccess) {
            return mcc.get(key);
        } else {
            return null;
        }
    }
     
    /**删除缓存
     * @param key 主键
     * @return 是否删除成功
     */
    public boolean delete(String key)
    {
        if (isSuccess) {
            return mcc.delete(key);
        } else {
            return false;
        }
         
    }
 
   /**判断缓存是否存在<br>     * @param key 主键<br>     * @return 是否存在<br>     */    <br>    public boolean keyExists(String key) {
        if (isSuccess) {
            return mcc.keyExists(key);
        } else {
            return false;
        }
    }
 
}

  测试:

  

    MemCachedManager cache = MemCachedManager.getInstance();
        long startDate = System.currentTimeMillis();

              
        //cache.delete("test2");
      //设置失效时间15s, 默认为0,永不失效,此时间最大不能超过30天 
//
cache.add("test2", "ffff",new Date(15000)); long endDate = System.currentTimeMillis(); long nowDate = (endDate - startDate); System.out.println(nowDate); System.out.print(" get value : " + cache.get("test2"));