优化cachekit,增加ehcache和redis两种实现
This commit is contained in:
parent
95657aa301
commit
1091fd5bb3
|
|
@ -20,7 +20,7 @@ import com.smallchill.common.plugins.GlobalPlugin;
|
|||
import com.smallchill.core.constant.Cst;
|
||||
import com.smallchill.core.interfaces.IConfig;
|
||||
import com.smallchill.core.interfaces.IPluginFactroy;
|
||||
import com.smallchill.core.shiro.DefaultShiroFactroy;
|
||||
import com.smallchill.core.shiro.DefaultShiroFactory;
|
||||
import com.smallchill.core.toolbox.file.DefaultFileProxyFactory;
|
||||
import com.smallchill.core.toolbox.grid.JqGridFactory;
|
||||
import com.smallchill.core.toolbox.kit.DateKit;
|
||||
|
|
@ -57,7 +57,7 @@ public class WebConfig implements IConfig {
|
|||
me.setDefaultSelectFactory(new DefaultSelectFactory());
|
||||
|
||||
//设定shiro工厂类
|
||||
me.setDefaultShiroFactory(new DefaultShiroFactroy());
|
||||
me.setDefaultShiroFactory(new DefaultShiroFactory());
|
||||
|
||||
//设定文件代理工厂类
|
||||
me.setDefaultFileProxyFactory(new DefaultFileProxyFactory());
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import com.smallchill.core.intercept.CURDInterceptor;
|
|||
import com.smallchill.core.intercept.QueryInterceptor;
|
||||
import com.smallchill.core.intercept.SelectInterceptor;
|
||||
import com.smallchill.core.interfaces.ICURD;
|
||||
import com.smallchill.core.interfaces.ICache;
|
||||
import com.smallchill.core.interfaces.ICheck;
|
||||
import com.smallchill.core.interfaces.IGrid;
|
||||
import com.smallchill.core.interfaces.ILog;
|
||||
|
|
@ -26,7 +27,8 @@ import com.smallchill.core.interfaces.IQuery;
|
|||
import com.smallchill.core.interfaces.ISelect;
|
||||
import com.smallchill.core.interfaces.IShiro;
|
||||
import com.smallchill.core.listener.ConfigListener;
|
||||
import com.smallchill.core.shiro.DefaultShiroFactroy;
|
||||
import com.smallchill.core.shiro.DefaultShiroFactory;
|
||||
import com.smallchill.core.toolbox.cache.EhCacheFactory;
|
||||
import com.smallchill.core.toolbox.check.PermissionCheckFactory;
|
||||
import com.smallchill.core.toolbox.file.DefaultFileProxyFactory;
|
||||
import com.smallchill.core.toolbox.file.IFileProxy;
|
||||
|
|
@ -111,7 +113,12 @@ public class Cst {
|
|||
/**
|
||||
* 默认shirorealm工厂类
|
||||
*/
|
||||
private IShiro defaultShiroFactory = new DefaultShiroFactroy();
|
||||
private IShiro defaultShiroFactory = new DefaultShiroFactory();
|
||||
|
||||
/**
|
||||
* 默认缓存工厂类
|
||||
*/
|
||||
private ICache defaultCacheFactory = new EhCacheFactory();
|
||||
|
||||
/**
|
||||
* 默认文件上传转换工厂类
|
||||
|
|
@ -263,6 +270,14 @@ public class Cst {
|
|||
this.defaultShiroFactory = defaultShiroFactory;
|
||||
}
|
||||
|
||||
public ICache getDefaultCacheFactory() {
|
||||
return defaultCacheFactory;
|
||||
}
|
||||
|
||||
public void setDefaultCacheFactory(ICache defaultCacheFactory) {
|
||||
this.defaultCacheFactory = defaultCacheFactory;
|
||||
}
|
||||
|
||||
public IFileProxy getDefaultFileProxyFactory() {
|
||||
return defaultFileProxyFactory;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,41 +1,27 @@
|
|||
/**
|
||||
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.smallchill.core.interfaces;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.smallchill.core.interfaces.ILoader;
|
||||
|
||||
/**
|
||||
* 缓存用接口
|
||||
* 通用缓存接口
|
||||
*/
|
||||
public interface ICache {
|
||||
|
||||
Map<String, Object> findOne(String cacheName, String key, String sql);
|
||||
|
||||
Map<String, Object> findOne(String cacheName, String key, String sql, Object modelOrMap);
|
||||
|
||||
Map<String, Object> findOneBySqlId(String cacheName, String key, String sqlId);
|
||||
|
||||
Map<String, Object> findOneBySqlId(String cacheName, String key, String sqlId, Object modelOrMap);
|
||||
|
||||
List<Map<String, Object>> find(String cacheName, String key, String sql);
|
||||
|
||||
List<Map<String, Object>> find(String cacheName, String key, String sql, Object modelOrMap);
|
||||
|
||||
List<Map<String, Object>> findBySqlId(String cacheName, String key, String sqlId);
|
||||
|
||||
List<Map<String, Object>> findBySqlId(String cacheName, String key, String sqlId, Object modelOrMap);
|
||||
|
||||
public void put(String cacheName, Object key, Object value);
|
||||
|
||||
public <T> T get(String cacheName, Object key);
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public List getKeys(String cacheName);
|
||||
|
||||
public void remove(String cacheName, Object key);
|
||||
|
||||
public void removeAll(String cacheName);
|
||||
|
||||
public <T> T get(String cacheName, Object key, ILoader iLoader);
|
||||
|
||||
public <T> T get(String cacheName, Object key, Class<? extends ILoader> iLoaderClass);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ import com.smallchill.core.toolbox.Func;
|
|||
import com.smallchill.core.toolbox.Paras;
|
||||
import com.smallchill.system.model.User;
|
||||
|
||||
public class DefaultShiroFactroy implements IShiro{
|
||||
public class DefaultShiroFactory implements IShiro{
|
||||
|
||||
public User user(String account) {
|
||||
User user = Blade.create(User.class).findFirstBy("account = #{account}", Paras.create().set("account", account));
|
||||
101
src/main/java/com/smallchill/core/toolbox/cache/EhCacheFactory.java
vendored
Normal file
101
src/main/java/com/smallchill/core/toolbox/cache/EhCacheFactory.java
vendored
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package com.smallchill.core.toolbox.cache;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.ehcache.Cache;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
import net.sf.ehcache.Element;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.smallchill.core.interfaces.ICache;
|
||||
import com.smallchill.core.interfaces.ILoader;
|
||||
import com.smallchill.core.toolbox.kit.CacheKit;
|
||||
|
||||
/**
|
||||
* Ehcache缓存工厂
|
||||
*/
|
||||
public class EhCacheFactory implements ICache{
|
||||
|
||||
private static CacheManager cacheManager;
|
||||
private static volatile Object locker = new Object();
|
||||
private static final Logger log = LogManager.getLogger(CacheKit.class);
|
||||
|
||||
private static CacheManager getCacheManager() {
|
||||
if (cacheManager == null) {
|
||||
synchronized (CacheKit.class) {
|
||||
if (cacheManager == null) {
|
||||
cacheManager = CacheManager.create();
|
||||
}
|
||||
}
|
||||
}
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
static Cache getOrAddCache(String cacheName) {
|
||||
CacheManager cacheManager = getCacheManager();
|
||||
Cache cache = cacheManager.getCache(cacheName);
|
||||
if (cache == null) {
|
||||
synchronized(locker) {
|
||||
cache = cacheManager.getCache(cacheName);
|
||||
if (cache == null) {
|
||||
log.warn("Could not find cache config [" + cacheName + "], using default.");
|
||||
cacheManager.addCacheIfAbsent(cacheName);
|
||||
cache = cacheManager.getCache(cacheName);
|
||||
log.debug("Cache [" + cacheName + "] started.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
public void put(String cacheName, Object key, Object value) {
|
||||
getOrAddCache(cacheName).put(new Element(key, value));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(String cacheName, Object key) {
|
||||
Element element = getOrAddCache(cacheName).get(key);
|
||||
return element != null ? (T)element.getObjectValue() : null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public List getKeys(String cacheName) {
|
||||
return getOrAddCache(cacheName).getKeys();
|
||||
}
|
||||
|
||||
public void remove(String cacheName, Object key) {
|
||||
getOrAddCache(cacheName).remove(key);
|
||||
}
|
||||
|
||||
public void removeAll(String cacheName) {
|
||||
getOrAddCache(cacheName).removeAll();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(String cacheName, Object key, ILoader iLoader) {
|
||||
Object data = get(cacheName, key);
|
||||
if (data == null) {
|
||||
data = iLoader.load();
|
||||
put(cacheName, key, data);
|
||||
}
|
||||
return (T) data;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(String cacheName, Object key, Class<? extends ILoader> iLoaderClass) {
|
||||
Object data = get(cacheName, key);
|
||||
if (data == null) {
|
||||
try {
|
||||
ILoader dataLoader = iLoaderClass.newInstance();
|
||||
data = dataLoader.load();
|
||||
put(cacheName, key, data);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return (T) data;
|
||||
}
|
||||
|
||||
}
|
||||
84
src/main/java/com/smallchill/core/toolbox/cache/RedisCacheFactory.java
vendored
Normal file
84
src/main/java/com/smallchill/core/toolbox/cache/RedisCacheFactory.java
vendored
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
package com.smallchill.core.toolbox.cache;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.smallchill.core.interfaces.ICache;
|
||||
import com.smallchill.core.interfaces.ILoader;
|
||||
import com.smallchill.core.plugins.dao.Redis;
|
||||
import com.smallchill.core.toolbox.redis.IJedis;
|
||||
|
||||
/**
|
||||
* Redis缓存工厂
|
||||
*/
|
||||
public class RedisCacheFactory implements ICache {
|
||||
|
||||
/**
|
||||
* BladeConfig中注入的redis名称
|
||||
*/
|
||||
private String redisName;
|
||||
|
||||
public String getRedisName() {
|
||||
return redisName;
|
||||
}
|
||||
|
||||
public void setRedisName(String redisName) {
|
||||
this.redisName = redisName;
|
||||
}
|
||||
|
||||
public RedisCacheFactory(String redisName) {
|
||||
super();
|
||||
this.redisName = redisName;
|
||||
}
|
||||
|
||||
public IJedis getJedis() {
|
||||
return Redis.init(getRedisName());
|
||||
}
|
||||
|
||||
public void put(String cacheName, Object key, Object value) {
|
||||
getJedis().hset(cacheName, key, value);
|
||||
}
|
||||
|
||||
public <T> T get(String cacheName, Object key) {
|
||||
return getJedis().hget(cacheName, key);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public List getKeys(String cacheName) {
|
||||
return new ArrayList<>(getJedis().hkeys(cacheName));
|
||||
}
|
||||
|
||||
public void remove(String cacheName, Object key) {
|
||||
getJedis().hdel(cacheName, key);
|
||||
}
|
||||
|
||||
public void removeAll(String cacheName) {
|
||||
getJedis().del(cacheName);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(String cacheName, Object key, ILoader iLoader) {
|
||||
Object data = get(cacheName, key);
|
||||
if (data == null) {
|
||||
data = iLoader.load();
|
||||
put(cacheName, key, data);
|
||||
}
|
||||
return (T) data;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(String cacheName, Object key, Class<? extends ILoader> iLoaderClass) {
|
||||
Object data = get(cacheName, key);
|
||||
if (data == null) {
|
||||
try {
|
||||
ILoader dataLoader = iLoaderClass.newInstance();
|
||||
data = dataLoader.load();
|
||||
put(cacheName, key, data);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return (T) data;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (c) 2011-2016, James Zhan 詹波 (jfinal@126.com).
|
||||
* Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -18,99 +18,46 @@ package com.smallchill.core.toolbox.kit;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.ehcache.Cache;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
import net.sf.ehcache.Element;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.smallchill.core.constant.Cst;
|
||||
import com.smallchill.core.interfaces.ICache;
|
||||
import com.smallchill.core.interfaces.ILoader;
|
||||
|
||||
/**
|
||||
* CacheKit. Useful tool box for EhCache.
|
||||
* 缓存工具类
|
||||
*/
|
||||
public class CacheKit {
|
||||
|
||||
private static CacheManager cacheManager;
|
||||
private static volatile Object locker = new Object();
|
||||
private static final Logger log = LogManager.getLogger(CacheKit.class);
|
||||
|
||||
private static CacheManager getManager() {
|
||||
if (cacheManager == null) {
|
||||
synchronized (CacheKit.class) {
|
||||
if (cacheManager == null) {
|
||||
cacheManager = CacheManager.create();
|
||||
}
|
||||
}
|
||||
}
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
static Cache getOrAddCache(String cacheName) {
|
||||
CacheManager cacheManager = getManager();
|
||||
Cache cache = cacheManager.getCache(cacheName);
|
||||
if (cache == null) {
|
||||
synchronized(locker) {
|
||||
cache = cacheManager.getCache(cacheName);
|
||||
if (cache == null) {
|
||||
log.warn("Could not find cache config [" + cacheName + "], using default.");
|
||||
cacheManager.addCacheIfAbsent(cacheName);
|
||||
cache = cacheManager.getCache(cacheName);
|
||||
log.debug("Cache [" + cacheName + "] started.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
private static ICache defaultGridFactory = Cst.me().getDefaultCacheFactory();
|
||||
|
||||
public static void put(String cacheName, Object key, Object value) {
|
||||
getOrAddCache(cacheName).put(new Element(key, value));
|
||||
defaultGridFactory.put(cacheName, key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T get(String cacheName, Object key) {
|
||||
Element element = getOrAddCache(cacheName).get(key);
|
||||
return element != null ? (T)element.getObjectValue() : null;
|
||||
return defaultGridFactory.get(cacheName, key);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static List getKeys(String cacheName) {
|
||||
return getOrAddCache(cacheName).getKeys();
|
||||
return defaultGridFactory.getKeys(cacheName);
|
||||
}
|
||||
|
||||
public static void remove(String cacheName, Object key) {
|
||||
getOrAddCache(cacheName).remove(key);
|
||||
defaultGridFactory.remove(cacheName, key);
|
||||
}
|
||||
|
||||
public static void removeAll(String cacheName) {
|
||||
getOrAddCache(cacheName).removeAll();
|
||||
defaultGridFactory.removeAll(cacheName);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T get(String cacheName, Object key, ILoader iLoader) {
|
||||
Object data = get(cacheName, key);
|
||||
if (data == null) {
|
||||
data = iLoader.load();
|
||||
put(cacheName, key, data);
|
||||
}
|
||||
return (T)data;
|
||||
return defaultGridFactory.get(cacheName, key, iLoader);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T get(String cacheName, Object key, Class<? extends ILoader> iLoaderClass) {
|
||||
Object data = get(cacheName, key);
|
||||
if (data == null) {
|
||||
try {
|
||||
ILoader dataLoader = iLoaderClass.newInstance();
|
||||
data = dataLoader.load();
|
||||
put(cacheName, key, data);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return (T)data;
|
||||
return defaultGridFactory.get(cacheName, key, iLoaderClass);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user