All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.jblv.ehcache.util.EhCacheUtils Maven / Gradle / Ivy
/**
* Copyright © 2012-2014 JeeSite All rights reserved.
*/
package com.jblv.ehcache.util;
import com.jblv.common.utils.spring.SpringUtils;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import java.util.Iterator;
import java.util.List;
/**
* Cache工具类
* @author yangtao
*/
public class EhCacheUtils {
private static CacheManager cacheManager =SpringUtils.getBean("ehCacheManager");
public static final String SYS_CACHE = "sysCache";
public static final String USER_CACHE = "userCache";
public static final String DEFAULT_CACHE = "defaultCache";
/**
* 根据缓存键获取系统缓存的缓存值
* @param key
* @return
*/
public static Object getSysInfo(String key) {
return get(SYS_CACHE, key);
}
public static Object getDefaultInfo(String key) {
return get(DEFAULT_CACHE, key);
}
public static Object getSysInfo(String prefix,String key) {
return get(SYS_CACHE, prefix+"_"+key);
}
public static Object getUserInfo(String key,String yhid) {
return get(USER_CACHE, key+"_"+yhid);
}
/**
* 写入SYS_CACHE缓存
* @param key
* @return
*/
public static void putSysInfo(String key, Object value) {
put(SYS_CACHE, key, value);
}
public static void putDefaultInfo(String key, Object value) {
put(DEFAULT_CACHE, key, value);
}
public static void putSysInfo(String prefix,String key, Object value) {
put(SYS_CACHE, prefix+"_"+key, value);
}
public static void putUserInfo(String key,String yhid, Object value) {
put(USER_CACHE, key+"_"+yhid, value);
}
/**
* 从SYS_CACHE缓存中移除
* @param key
* @return
*/
public static void removeSysInfo(String key) {
remove(SYS_CACHE, key);
}
public static void removeSysInfo(String prefix,String key) {
remove(SYS_CACHE, prefix+"_"+key);
}
public static void removeUserInfo(String key,String yhid) {
remove(USER_CACHE, key+"_"+yhid);
}
public static void clearUserInfo(String yhid){
Cache cache=getCache(USER_CACHE);
List list = cache.getKeys();
for (Iterator localIterator = list.iterator(); localIterator.hasNext();) {
Object key = localIterator.next();
if(key.toString().indexOf("_"+yhid)>=0){
cache.remove(key);
}
}
}
/**
* 清空缓存
* @param cacheName
*/
public static void clear(String cacheName){
Cache cache=getCache(cacheName);
List list = cache.getKeys();
for (Iterator localIterator = list.iterator(); localIterator.hasNext();) {
Object key = localIterator.next();
cache.remove(key);
}
}
/**
* 获取缓存值
* @param cacheName 缓存的名称
* @param key 缓存的键
* @return
*/
private static Object get(String cacheName, String key) {
Element element = getCache(cacheName).get(key);
return element==null?null:element.getObjectValue();
}
/**
* 写入缓存
* @param cacheName
* @param key
* @param value
*/
private static void put(String cacheName, String key, Object value) {
Element element = new Element(key, value);
getCache(cacheName).put(element);
}
/**
* 从缓存中移除
* @param cacheName
* @param key
*/
private static void remove(String cacheName, String key) {
getCache(cacheName).remove(key);
}
/**
* 获得一个Cache,没有则创建一个。
* @param cacheName
* @return
*/
private static Cache getCache(String cacheName){
Cache cache = cacheManager.getCache(cacheName);
if (cache == null){
cacheManager.addCache(cacheName);
cache = cacheManager.getCache(cacheName);
cache.getCacheConfiguration().setEternal(true);
}
return cache;
}
public static CacheManager getCacheManager() {
return cacheManager;
}
}