有个项目使用的缓存是EhCache, 这个客户的访问量单机抗不住, 就用了多部署, 发现有些梗, 是缓存导致的, 没有同步... 于是用上了Redis . 业务代码尽量不能动... 就写了个插件, EhCache与Redis自由切换 , 下面分享一下
这个分享适合老项目微改造,不想太多改动的。集群项目推荐直接使用j2cache这个开源中国红薯大佬的工具。
Redis 安装 配置
菜鸟教程网:
http://www.runoob.com/redis/redis-install.html
http://blog.csdn.net/libaineu2004/article/details/49004195
JFinal 中配置
看手册: 第八 章 RedisPlugin
数据库ActiveRecordPlugin 默认使用的是
EhCache
com.jfinal.plugin.activerecord.cache;
在这里得自己模仿写一个 可切换的Cache工具了.
MyCache.java
public class MyCache implements ICache {
private static String getCacheType() {
return PropKit.get("cache_type", "ehcache");
}
public static boolean isEhCache() {
return "ehcache".equals(getCacheType());
}
public static boolean isRedis() {
return "redis".equals(getCacheType());
}
@Override
public <T> T get(String cacheName, Object key) {
if (isEhCache()) {
return CacheKit.get(cacheName, key);
} else if (isRedis()) {
return Redis.use(cacheName).get(key);
}
return null;
}
@Override
public void put(String cacheName, Object key, Object value) {
if (isEhCache()) {
CacheKit.put(cacheName, key, value);
} else if (isRedis()) {
Redis.use(cacheName).set(key, value);
}
}
@Override
public void remove(String cacheName, Object key) {
if (isEhCache()) {
CacheKit.remove(cacheName, key);
} else if (isRedis()) {
Redis.use(cacheName).del(key);
}
}
@Override
public void removeAll(String cacheName) {
if (isEhCache()) {
CacheKit.removeAll(cacheName);
} else if (isRedis()) {
Cache cache = Redis.use(cacheName);
cache.del(cache.keys("*").toArray());
}
}
}放进数据库连接插件 ActiveRecordPlugin中
JFinalConfig中
/**
* 配置插件
*/
public void configPlugin(Plugins me) {
// 配置DruidPlugin数据库连接池插件
DruidPlugin druidPlugin = createDruidPlugin();
me.add(druidPlugin);
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(DbKit.MAIN_CONFIG_NAME, druidPlugin);
me.add(arp);
arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));// 大小写不敏感
// 缓存可切换使用
arp.setCache(new MyCache());
// 所有主数据元 配置在 MappingKit 中
_MappingKit.mapping(arp);
// 缓存设置
if(MyCache.isRedis()){
//根据ehcache.xml 中来 栗子~
String[] cacheName = {"cacheName_1","cacheName_2",
"cacheName_3","cacheName_4","cacheName_5","cacheName_6",
"cacheName_7","cacheName_8","cacheName_9"};
for (int i = 0; i <= 9; i++) {
RedisPlugin newsRedis =
new RedisPlugin( cacheName[i], "192.168.1.240", 6379, 0, "JFinal2011", i);
me.add(newsRedis);
}
}else if (MyCache.isEhCache()) {
me.add(new EhCachePlugin());
}
}好了, 最后导包
写个测试类:
public class RedisTest {
public static void main(String[] args) {
for (int i = 0; i <= 9; i++) {
testRedis("cacheName_" + i, "192.168.1.240", 6379, 0 , "JFinal2011", i);
System.out.println("------------------------");
}
}
public static void testRedis(final String cacheName, String host, int port) {
System.out.println("cacheName="+ cacheName + " host="+ host + " port="+ port);
RedisPlugin rpMy = new RedisPlugin( cacheName, host, port, timeout, password, database);
rpMy.start();
new Thread(){
public void run() {
for (int i = 0; i < 10000; i++) {
new Thread(){
public void run() {
String name = getName();
Cache use = Redis.use(cacheName);
use.set("k" + name, "value_key_" + cacheName + "_"+name);
Object v = use.get("k" + name);
System.out.println(v);
use.del("k" + name);
}
}.start();
}
}
}.start();
}
}@JFinal 非常感谢老大的指导!!! 已经做修改了~ 爽!