引用:http://blog.csdn.net/einarzhang/article/details/6064092
Memcache的介绍有很多,这里给出如何在Java中应用Memcache的基本方法
1 安装Memcache服务器(windows)
下载windows版Memcache安装包,如memcached-1.2.6-win32-bin.zip,解压到指定位置,比如(D://memcache),打开dos命令行,输入以下两个命令即可启动Memcache服务。
D:/memcache/memcached.exe -d install
D:/memcache/memcached.exe -d start
2 下载Java版本的memcache客户端(以下列出常用的几种)。
spymemcached :
http://code.google.com/p/spymemcached/
gwhalin / Memcached-Java-Client
https://github.com/gwhalin/Memcached-Java-Client/downloads
Jcache
http://code.google.com/intl/zh-CN/appengine/docs/java/memcache/usingjcache.html
3 下面给出两种方式调用Memcache
gwhalin / Memcached-Java-Client调用方式如下:
- public class MemcacheManagerForGwhalin {
- // 构建缓存客户端
- private static MemCachedClient cachedClient;
- // 单例模式实现客户端管理类
- private static MemcacheManagerForGwhalin INSTANCE = new MemcacheManagerForGwhalin();
- private MemcacheManagerForGwhalin() {
- cachedClient = new MemCachedClient();
- //获取连接池实例
- SockIOPool pool = SockIOPool.getInstance();
- //设置缓存服务器地址,可以设置多个实现分布式缓存
- pool.setServers(new String[]{"127.0.0.1:11211"});
- //设置初始连接5
- pool.setInitConn(5);
- //设置最小连接5
- pool.setMinConn(5);
- //设置最大连接250
- pool.setMaxConn(250);
- //设置每个连接最大空闲时间3个小时
- pool.setMaxIdle(1000 * 60 * 60 * 3);
- pool.setMaintSleep(30);
- pool.setNagle(false);
- pool.setSocketTO(3000);
- pool.setSocketConnectTO(0);
- pool.initialize();
- }
- /**
- * 获取缓存管理器唯一实例
- * @return
- */
- public static MemcacheManagerForGwhalin getInstance() {
- return INSTANCE;
- }
- @Override
- public void add(String key, Object value) {
- cachedClient.set(key, value);
- }
- @Override
- public void add(String key, Object value, int milliseconds) {
- cachedClient.set(key, value, milliseconds);
- }
- @Override
- public void remove(String key) {
- cachedClient.delete(key);
- }
- @Override
- public void remove(String key, int milliseconds) {
- cachedClient.delete(key, milliseconds, new Date());
- }
- @Override
- public void update(String key, Object value, int milliseconds) {
- cachedClient.replace(key, value, milliseconds);
- }
- @Override
- public void update(String key, Object value) {
- cachedClient.replace(key, value);
- }
- @Override
- public Object get(String key) {
- return cachedClient.get(key);
- }
- }
Spy方式调用如下:
- public class MemcacheManagerForSpy implements IMemcacheManager {
- //缓存客户端
- private MemcachedClient memcacheCient;
- //Manager管理对象,单例模式
- private static MemcacheManagerForSpy INSTANCE = new MemcacheManagerForSpy();
- private MemcacheManagerForSpy() {
- try {
- memcacheCient = new MemcachedClient(new InetSocketAddress("127.0.0.1",11211));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static MemcacheManagerForSpy getInstance() {
- return INSTANCE;
- }
- @Override
- public void add(String key, Object value, int milliseconds) {
- memcacheCient.add(key, milliseconds, value);
- }
- @Override
- public void add(String key, Object value) {
- memcacheCient.add(key, 3600, value);
- }
- @Override
- public void remove(String key, int milliseconds) {
- memcacheCient.delete(key);
- }
- @Override
- public void remove(String key) {
- memcacheCient.delete(key);
- }
- @Override
- public void update(String key, Object value, int milliseconds) {
- memcacheCient.replace(key, milliseconds, value);
- }
- @Override
- public void update(String key, Object value) {
- memcacheCient.replace(key, 3600, value);
- }
- @Override
- public Object get(String key) {
- return memcacheCient.get(key);
- }
- }