Jfinal---模拟redis插件写一个连接多个memcahe的插件
来源:互联网 发布:steam mac 中文游戏 编辑:程序博客网 时间:2024/06/07 06:15
因为项目需要,需要连接多个不同服务器的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);
阅读全文
0 0
- Jfinal---模拟redis插件写一个连接多个memcahe的插件
- JFinal redis cluster集群插件
- 写的一个qfDialog插件
- MT4使用MQL连接Redis的插件
- 自己写了一个perl脚本检测redis(nagios插件)
- 我写的第一个Mootools插件
- 写个自己的Xcode4插件
- JFinal所集成的插件们
- JFinal的ActiveRecord插件常用方法
- Opticks的插件创建--写一个插件类
- 写了一个简单的FireFox插件
- 用Jquery写的一个分页插件
- 自己写的一个遮罩层插件
- 如何写一个简单的jquery插件
- 如何写一个Jquery 的Plugin插件
- 写一个简单的jq插件
- 如何写一个yii2的插件
- 写一个返回顶部的chrome插件
- 初学PX4之环境搭建
- LeetCode416
- LeetCode | 743. Network Delay Time | 中等难度 图论 单源最短路径题
- mac 上配置nginx过程中的问题记录
- 手机的100种玩法,程序员你是怎么“善待”你的手机的?
- Jfinal---模拟redis插件写一个连接多个memcahe的插件
- 扫码领红包 无需消费即可轻松提现 附教程
- Bootstrap 3.x 打印问题 打印无法显示背景、字体变黑
- HTML5移动开发的10大移动APP开发框架
- 结构体与byte数组转换/结构体内定长数组
- linux 僵尸进程
- NKOJ 2844 (APIO 2014)回文串(Manacher+后缀自动机+倍增/回文树)
- 从标准输入获取字符串并将换行符去掉-c语言实现
- Python checkio "Long Non Repeat"解决方案