2017-12-29 16:18:40 +08:00
|
|
|
|
package com.nis.util;
|
|
|
|
|
|
|
2018-12-16 11:04:25 +08:00
|
|
|
|
|
|
|
|
|
|
import org.apache.shiro.cache.Cache;
|
|
|
|
|
|
import org.crazycake.shiro.RedisCacheManager;
|
|
|
|
|
|
|
2017-12-29 16:18:40 +08:00
|
|
|
|
import com.nis.web.service.SpringContextHolder;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Cache工具类
|
|
|
|
|
|
* @author darnell
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class CacheUtils {
|
|
|
|
|
|
|
2018-12-16 11:04:25 +08:00
|
|
|
|
private static RedisCacheManager cacheManager = (RedisCacheManager)SpringContextHolder.getBean("shiroCacheManager");
|
2017-12-29 16:18:40 +08:00
|
|
|
|
|
|
|
|
|
|
private static final String SYS_CACHE = "sysCache";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取SYS_CACHE缓存
|
|
|
|
|
|
* @param key
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static Object get(String key) {
|
|
|
|
|
|
return get(SYS_CACHE, key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 写入SYS_CACHE缓存
|
|
|
|
|
|
* @param key
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static void put(String key, Object value) {
|
|
|
|
|
|
put(SYS_CACHE, key, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从SYS_CACHE缓存中移除
|
|
|
|
|
|
* @param key
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static void remove(String key) {
|
|
|
|
|
|
remove(SYS_CACHE, key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取缓存
|
|
|
|
|
|
* @param cacheName
|
|
|
|
|
|
* @param key
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static Object get(String cacheName, String key) {
|
2018-12-16 11:04:25 +08:00
|
|
|
|
return getCache(cacheName).get(key);
|
2017-12-29 16:18:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 写入缓存
|
|
|
|
|
|
* @param cacheName
|
|
|
|
|
|
* @param key
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static void put(String cacheName, String key, Object value) {
|
2018-12-16 11:04:25 +08:00
|
|
|
|
Cache cache=cacheManager.getCache(cacheName);
|
|
|
|
|
|
cache.put(key, value);
|
2017-12-29 16:18:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从缓存中移除
|
|
|
|
|
|
* @param cacheName
|
|
|
|
|
|
* @param key
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static void remove(String cacheName, String key) {
|
|
|
|
|
|
getCache(cacheName).remove(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获得一个Cache,没有则创建一个。
|
|
|
|
|
|
* @param cacheName
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2018-12-16 11:04:25 +08:00
|
|
|
|
/*private static Cache getCache(String cacheName){
|
2017-12-29 16:18:40 +08:00
|
|
|
|
Cache cache = cacheManager.getCache(cacheName);
|
|
|
|
|
|
if (cache == null){
|
|
|
|
|
|
cacheManager.addCache(cacheName);
|
|
|
|
|
|
cache = cacheManager.getCache(cacheName);
|
|
|
|
|
|
cache.getCacheConfiguration().setEternal(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
return cache;
|
2018-12-16 11:04:25 +08:00
|
|
|
|
}*/
|
|
|
|
|
|
private static Cache getCache(String cacheName){
|
|
|
|
|
|
Cache cache = cacheManager.getCache(cacheName);
|
|
|
|
|
|
return cache;
|
2017-12-29 16:18:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-16 11:04:25 +08:00
|
|
|
|
public static RedisCacheManager getCacheManager() {
|
2017-12-29 16:18:40 +08:00
|
|
|
|
return cacheManager;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|