因为项目需要,需要连接多个不同服务器的memcahe服务,并进行相关操作。
由于Jfinal并未提供memcahe相关的插件,所以这里模仿其提供的redis插件写了一个人memcahe插件,仅供参考,有不足或者错误的地方,欢迎指正。
- 1、RedisPlugin —–>MemCachePlugin
import com.danga.MemCached.SockIOPool;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.IPlugin;
/**
* @author daiwenjun
* 2017年12月11日
*/
public class MemCachePlugin implements IPlugin {
private String cacheName;
private String host;
private Integer port;
public MemCachePlugin(String cacheNameParam,String hostParam, Integer portParam) {
if (StrKit.isBlank(cacheNameParam))
throw new IllegalArgumentException("cacheNameParam can not be blank.");
if (StrKit.isBlank(hostParam))
throw new IllegalArgumentException("hostParam can not be blank.");
this.cacheName = cacheNameParam.trim();
this.host = hostParam;
this.port = portParam;
}
@Override
public boolean start() {
//服务器监听IP:端口
String [] addr ={host+":"+port};
Integer [] weights = {3};
SockIOPool pool = SockIOPool.getInstance(cacheName);
// 设置服务器信息
pool.setServers(addr);
pool.setWeights(weights);
// 设置初始连接数、最小和最大连接数以及最大处理时间
pool.setInitConn(50);
pool.setMinConn(50);
pool.setMaxConn(1000);
pool.setMaxIdle(1000 * 60 * 60 * 6);
// 设置主线程的睡眠时间
pool.setMaintSleep(30);
// 设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// 初始化连接池
pool.initialize();
MemCache.init(pool);
MemCache.addPool(pool,cacheName);
return true;
}
@Override
public boolean stop() {
return true;
}
}
- 2、Redis ——> MemCache
import java.util.concurrent.ConcurrentHashMap;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
/**
* @author daiwenjun
* 2017年12月11日
* TODO
*/
public class MemCache {
private static volatile SockIOPool pool;
private static final ConcurrentHashMap<String, SockIOPool> cacheMap = new ConcurrentHashMap<String, SockIOPool>();
static synchronized void addPool(SockIOPool cache, String cacheName) {
if (cache == null)
throw new IllegalArgumentException("cache can not be null");
if (cacheMap.containsKey(cacheName))
throw new IllegalArgumentException("cache already exists");
cacheMap.put(cacheName, cache);
}
static SockIOPool removeCache(String cacheName) {
return cacheMap.remove(cacheName);
}
public static SockIOPool use(String cacheName) {
return cacheMap.get(cacheName);
}
public static void init(SockIOPool poolParam) {
MemCache.pool = poolParam;
}
public static MemCachedClient getMemCachedClient(String cacheName) {
return new MemCachedClient(cacheName);
}
public static SockIOPool getSockIOPool() {
return pool;
}
}
- 3、封装工具类
import olo.qd.plugin.MemCache;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.jfinal.kit.StrKit;
public class MemCacheFactory {
/**
* 1分钟过期
* */
public static final int ONE_MINUTE = 60;
/**
* 3分钟过期
* */
public static final int THREE_MINUTE = 60 * 3;
/**
* 1小时过期
* */
public static final int ONE_HOUR = 60 * 60;
/**
* 1天过期
* */
public static final int ONE_DAY = 60 * 60 * 24;
/**
* 1周过期
* */
public static final int ONE_WEEK = 60 * 60 * 24 * 7;
/**
* 1月过期
* */
public static final int ONE_MONTH = 60 * 60 * 24 * 30; // 最大不能超过30天
/**
* 永不过期
* */
public static final int ALWAYS = 0;
public static final String SERVER1 = "server1";
public static final String SERVER2 = "server2";
private static MemCachedClient cacheClient = null;
private static SockIOPool pool = null;
//传参使用
public static MemCachedClient getInstance(String name) {
if (StrKit.isBlank(name)) {
name = SERVER1 ;
}
if(pool == null) {
pool = MemCache.use(name);
}
if (cacheClient == null) {
cacheClient = MemCache.getMemCachedClient(name);
}
return cacheClient;
}
public static void setMemCachedClient(MemCachedClient cachedClient) {
if (cachedClient != null) {
cacheClient = cachedClient;
}
}
/**
* 缓存对象
* */
public static void add(String name,String key, Object obj, int time) {
MemCachedClient memcache = getInstance(name);
if (memcache != null) {
memcache.add(key, obj, time);
}
memcache = null;
System.out.println("*******新增: serverName="+name+" key="+key+" value="+obj +" time="+time);
}
/**
* 获取对象
* */
public static Object get(String name,String key) {
MemCachedClient memcache = getInstance(name);
Object obj = null;
if (memcache != null) {
obj = memcache.get(key);
}
memcache = null;
System.out.println("*******根据key查询: serverName="+name+ " key="+key+" value="+obj);
return obj;
}
/**
* 删除对象
* */
public static void delete(String name,String key) {
MemCachedClient memcache = getInstance(name);
if (memcache != null) {
memcache.delete(key);
}
System.out.println("*******根据key删除: serverName="+name+ " key="+key);
}
/**
* 清空缓存
* */
public static void flushAll(String name) {
MemCachedClient memcache = getInstance(name);
if (memcache != null) {
memcache.flushAll();
}
System.out.println("*******清空memcahe serverName="+name);
}
}
- 4、在Jfinal的MainConfig中的configPlugin配置中添加插件配置:
MemCachePlugin SERVER1= new MemCachePlugin("server1", ip, port);
me.add(SERVER1);
MemCachePlugin SERVER2= new MemCachePlugin("server2", ip, port);
me.add(SERVER2);
- 5、代码中的具体使用示例:
//新增
MemCacheFactory.add(MemCacheFactory.ZHMOBIADMIN, DEMO_KEY, list, MemCacheFactory.ONE_DAY);
//取值
MemCacheFactory.get(MemCacheFactory.ZHMOBIADMIN, DEMO_KEY);
//删除
MemCacheFactory.delete(MemCacheFactory.ZHMOBIADMIN, DEMO_KEY);