This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/util/CacheUtils.java

102 lines
1.9 KiB
Java
Raw Normal View History

2017-12-29 16:18:40 +08:00
package com.nis.util;
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 {
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) {
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) {
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
*/
/*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;
}*/
private static Cache getCache(String cacheName){
Cache cache = cacheManager.getCache(cacheName);
return cache;
2017-12-29 16:18:40 +08:00
}
public static RedisCacheManager getCacheManager() {
2017-12-29 16:18:40 +08:00
return cacheManager;
}
}