Skip to content

Commit

Permalink
系统缓存CacheUtils使用shiro缓存,系统缓存支持redis,方便分布式的应用。由于是移植过来的功能还待详细测试,若有问题可通过…
Browse files Browse the repository at this point in the history
…qq群找到我或发邮件[email protected]
  • Loading branch information
think-gem committed Jul 15, 2016
1 parent 4bd4bdf commit 0856a3a
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
*/
package com.thinkgem.jeesite.common.filter;

import com.thinkgem.jeesite.common.utils.CacheUtils;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;

import com.thinkgem.jeesite.common.utils.SpringContextHolder;

/**
* 页面高速缓存过滤器
* @author ThinkGem
* @version 2013-8-5
*/
public class PageCachingFilter extends SimplePageCachingFilter {

private CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class);

@Override
protected CacheManager getCacheManager() {
return CacheUtils.getCacheManager();
this.cacheName = "pageCachingFilter";
return cacheManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ public Set<K> keys() {
jedis = JedisUtils.getResource();
Set<byte[]> set = jedis.hkeys(JedisUtils.getBytesKey(cacheKeyName));
for(byte[] key : set){
keys.add((K)key);
Object obj = (K)JedisUtils.getObjectKey(key);
if (obj != null){
keys.add((K) obj);
}
}
logger.debug("keys {} {} ", cacheKeyName, keys);
return keys;
Expand All @@ -196,7 +199,10 @@ public Collection<V> values() {
jedis = JedisUtils.getResource();
Collection<byte[]> col = jedis.hvals(JedisUtils.getBytesKey(cacheKeyName));
for(byte[] val : col){
vals.add((V)val);
Object obj = JedisUtils.toObject(val);
if (obj != null){
vals.add((V) obj);
}
}
logger.debug("values {} {} ", cacheKeyName, vals);
return vals;
Expand Down
88 changes: 67 additions & 21 deletions src/main/java/com/thinkgem/jeesite/common/utils/CacheUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
*/
package com.thinkgem.jeesite.common.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import java.util.Iterator;
import java.util.Set;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Cache工具类
Expand All @@ -14,8 +18,9 @@
*/
public class CacheUtils {

private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));

private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);
private static CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class);

private static final String SYS_CACHE = "sysCache";

/**
Expand All @@ -27,6 +32,17 @@ public static Object get(String key) {
return get(SYS_CACHE, key);
}

/**
* 获取SYS_CACHE缓存
* @param key
* @param defaultValue
* @return
*/
public static Object get(String key, Object defaultValue) {
Object value = get(key);
return value != null ? value : defaultValue;
}

/**
* 写入SYS_CACHE缓存
* @param key
Expand All @@ -52,19 +68,29 @@ public static void remove(String key) {
* @return
*/
public static Object get(String cacheName, String key) {
Element element = getCache(cacheName).get(key);
return element==null?null:element.getObjectValue();
return getCache(cacheName).get(getKey(key));
}


/**
* 获取缓存
* @param cacheName
* @param key
* @param defaultValue
* @return
*/
public static Object get(String cacheName, String key, Object defaultValue) {
Object value = get(cacheName, getKey(key));
return value != null ? value : defaultValue;
}

/**
* 写入缓存
* @param cacheName
* @param key
* @param value
*/
public static void put(String cacheName, String key, Object value) {
Element element = new Element(key, value);
getCache(cacheName).put(element);
getCache(cacheName).put(getKey(key), value);
}

/**
Expand All @@ -73,26 +99,46 @@ public static void put(String cacheName, String key, Object value) {
* @param key
*/
public static void remove(String cacheName, String key) {
getCache(cacheName).remove(key);
getCache(cacheName).remove(getKey(key));
}

/**
* 从缓存中移除所有
* @param cacheName
*/
public static void removeAll(String cacheName) {
Cache<String, Object> cache = getCache(cacheName);
Set<String> keys = cache.keys();
for (Iterator<String> it = keys.iterator(); it.hasNext();){
cache.remove(it.next());
}
logger.info("清理缓存: {} => {}", cacheName, keys);
}

/**
* 获得一个Cache,没有则创建一个。
* 获取缓存键名,多数据源下增加数据源名称前缀
* @param key
* @return
*/
private static String getKey(String key){
// String dsName = DataSourceHolder.getDataSourceName();
// if (StringUtils.isNotBlank(dsName)){
// return dsName + "_" + key;
// }
return key;
}

/**
* 获得一个Cache,没有则显示日志。
* @param cacheName
* @return
*/
private static Cache getCache(String cacheName){
Cache cache = cacheManager.getCache(cacheName);
private static Cache<String, Object> getCache(String cacheName){
Cache<String, Object> cache = cacheManager.getCache(cacheName);
if (cache == null){
cacheManager.addCache(cacheName);
cache = cacheManager.getCache(cacheName);
cache.getCacheConfiguration().setEternal(true);
throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。");

This comment has been minimized.

Copy link
@javyuan

javyuan Mar 30, 2017

个人感觉这样改不如原来的实现好用,规范是规范了,每次用一个缓存必须要在配置文件中配置这个缓存,但不如原来那样方便了。如果说已经有人用原来的版本很久了,这边这么一变动,代码改造量还真是不小,需要查看系统中所有用到的缓存然后把配置文件中没有的都配置上。

}
return cache;
}

public static CacheManager getCacheManager() {
return cacheManager;
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/thinkgem/jeesite/common/utils/JedisUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,24 @@ public static byte[] getBytesKey(Object object){
}
}

/**
* 获取byte[]类型Key
* @param key
* @return
*/
public static Object getObjectKey(byte[] key){
try{
return StringUtils.toString(key);
}catch(UnsupportedOperationException uoe){
try{
return JedisUtils.toObject(key);
}catch(UnsupportedOperationException uoe2){
uoe2.printStackTrace();
}
}
return null;
}

/**
* Object转换byte[]类型
* @param key
Expand Down
32 changes: 17 additions & 15 deletions src/main/resources/cache/ehcache-local.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@

<diskStore path="../temp/jeesite/ehcache" />

<!-- 默认缓存配置. -->
<defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true" maxEntriesLocalDisk="100000" />
<!-- 默认缓存配置. 自动失效:最后一次访问时间间隔300秒失效,若没有访问过自创建时间600秒失效。-->
<defaultCache maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true" statistics="true"/>

<!-- 系统缓存 -->
<cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
<cache name="sysCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true"/>

<!-- 用户缓存 -->
<cache name="userCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
<cache name="userCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true"/>

<!-- 集团缓存 -->
<cache name="corpCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true"/>

<!-- 内容管理模块缓存 -->
<cache name="cmsCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true"/>

<!-- 工作流模块缓存 -->
<cache name="actCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
<cache name="actCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true" statistics="true"/>

<!-- 内容管理模块缓存
<cache name="cmsCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/> -->
<!-- 简单页面缓存 -->
<cache name="pageCachingFilter" maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="120"
timeToLiveSeconds="120" overflowToDisk="true" memoryStoreEvictionPolicy="LFU" statistics="true"/>

<!-- 系统活动会话缓存 -->
<cache name="activeSessionsCache" maxEntriesLocalHeap="10000" overflowToDisk="true"
eternal="true" timeToLiveSeconds="0" timeToIdleSeconds="0"
diskPersistent="true" diskExpiryThreadIntervalSeconds="600"/>

<!-- 简单页面缓存
<cache name="SimplePageCachingFilter" maxEntriesLocalHeap="100" eternal="false" overflowToDisk="true"
timeToIdleSeconds="120" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LFU"/> -->
<cache name="activeSessionsCache" maxEntriesLocalHeap="10000" eternal="true" overflowToDisk="true"
diskPersistent="true" diskExpiryThreadIntervalSeconds="600" statistics="true"/>

</ehcache>
56 changes: 30 additions & 26 deletions src/main/resources/cache/ehcache-rmi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,51 @@
<diskStore path="../temp/jeesite/ehcache" />

<!-- 默认缓存配置. -->
<defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true" maxEntriesLocalDisk="100000" >
<defaultCache maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true" statistics="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicatePuts=false,replicateUpdatesViaCopy=false"/>
</defaultCache>
</defaultCache>

<!-- 系统缓存 -->
<cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true">
<cache name="sysCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>
<!-- 用户缓存 -->
<cache name="userCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true">
</cache>
<!-- 用户缓存 -->
<cache name="userCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>
</cache>

<!-- 用户缓存 -->
<cache name="corpCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>

<!-- 内容管理模块缓存 -->
<cache name="cmsCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true" statistics="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>

<!-- 工作流模块缓存 -->
<cache name="actCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>

<!-- 内容管理模块缓存
<cache name="cmsCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache> -->

<!-- 系统活动会话缓存 -->
<cache name="activeSessionsCache" maxEntriesLocalHeap="10000" overflowToDisk="true"
eternal="true" timeToLiveSeconds="0" timeToIdleSeconds="0"
diskPersistent="true" diskExpiryThreadIntervalSeconds="600">

<!-- 简单页面缓存 -->
<cache name="pageCachingFilter" maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="120"
timeToLiveSeconds="120" overflowToDisk="true" memoryStoreEvictionPolicy="LFU" statistics="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,
replicateUpdatesViaCopy=false, replicateRemovals=true "/>
</cache>

<!-- 简单页面缓存
<cache name="SimplePageCachingFilter" maxEntriesLocalHeap="100" eternal="false" overflowToDisk="true"
timeToIdleSeconds="120" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LFU">
</cache>
<!-- 系统活动会话缓存 -->
<cache name="activeSessionsCache" maxEntriesLocalHeap="10000" eternal="true" overflowToDisk="true"
diskPersistent="true" diskExpiryThreadIntervalSeconds="600" statistics="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,
replicateUpdatesViaCopy=false, replicateRemovals=true "/>
</cache> -->
</cache>

</ehcache>
4 changes: 4 additions & 0 deletions src/main/resources/spring-context-jedis.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.host}" />
<constructor-arg index="2" value="${redis.port}" type="int" />
<!-- <constructor-arg index="3" value="${redis.timeout}" type="int" />
<constructor-arg index="4" value="${redis.password}"/>
<constructor-arg index="5" value="${redis.database}" type="int" />
<constructor-arg index="6" value="${redis.clientName}"/> -->
</bean>

</beans>
6 changes: 4 additions & 2 deletions src/main/resources/spring-context-shiro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
<property name="cacheManager" ref="shiroCacheManager" />
</bean>

<!-- 定义授权缓存管理器 -->
<!-- <bean id="shiroCacheManager" class="com.thinkgem.jeesite.common.security.shiro.cache.SessionCacheManager" /> -->
<!-- 自定义系统缓存管理器-->
<!-- <bean id="shiroCacheManager" class="com.thinkgem.jeesite.common.security.shiro.cache.JedisCacheManager"> -->
<!-- <property name="cacheKeyPrefix" value="${redis.keyPrefix}_cache_" /> -->
<!-- </bean> -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="cacheManager"/>
</bean>
Expand Down

0 comments on commit 0856a3a

Please sign in to comment.