开源项目学习-jeesite1.2.7-缓存相关

Shiro缓存

参考资料

Shiro之缓存管理

数据缓存

spring-context-shiro.xml定义了缓存管理器:

<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
   <property name="cacheManager" ref="cacheManager"/>
</bean>

该管理器在spring-context.xml进行了声明:

<!-- 缓存配置 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
   <property name="configLocation" value="classpath:${ehcache.configFile}" />
</bean>

ehcache.configFile对应的值为:

ehcache.configFile=cache/ehcache-local.xml

其内容为:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache">

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

   <!-- 默认缓存配置. 自动失效:最后一次访问时间间隔300秒失效,若没有访问过自创建时间600秒失效。-->
   <defaultCache maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
      overflowToDisk="true" statistics="true"/>
   
   <!-- 系统缓存 -->
   <cache name="sysCache" maxEntriesLocalHeap="1000" eternal="true" overflowToDisk="true" statistics="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" statistics="true"/>
   
    <!-- 简单页面缓存 -->
    <cache name="pageCachingFilter" maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="120"
       timeToLiveSeconds="120" overflowToDisk="true" memoryStoreEvictionPolicy="LFU" statistics="true"/>
   
   <!-- 系统活动会话缓存 -->
    <cache name="activeSessionsCache" maxEntriesLocalHeap="10000" eternal="true" overflowToDisk="true"
           diskPersistent="true" diskExpiryThreadIntervalSeconds="600" statistics="true"/>
       
</ehcache>

比如在登录过程中用到的CacheUtils就是借助ehcache做缓存管理:

public class CacheUtils {
	
	private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);
	private static CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class);
	
	private static final String SYS_CACHE = "sysCache";

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

}

Session缓存

spring-context-shiro.xml定义了缓存管理器:

<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    <property name="cacheManager" ref="cacheManager"/>
</bean>
<bean id="sessionDAO" class="com.thinkgem.jeesite.common.security.shiro.session.CacheSessionDAO">
    <property name="sessionIdGenerator" ref="idGen" />
    <property name="activeSessionsCacheName" value="activeSessionsCache" />
    <property name="cacheManager" ref="shiroCacheManager" />
</bean>

其中cacheManager在spring-context.xml中进行了定义:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:${ehcache.configFile}" />
</bean>	

idGen对应的是IdGen,是一个Service,启用了懒加载:

@Lazy注解是干啥的?

查看代码
/**
 * 封装各种生成唯一性ID算法的工具类.
 * @author ThinkGem
 * @version 2013-01-15
 */
@Service
@Lazy(false)
public class IdGen implements IdGenerator, SessionIdGenerator {

   private static SecureRandom random = new SecureRandom();
   
   /**
    * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
    */
   public static String uuid() {
      return UUID.randomUUID().toString().replaceAll("-", "");
   }
   
   /**
    * 使用SecureRandom随机生成Long. 
    */
   public static long randomLong() {
      return Math.abs(random.nextLong());
   }

   /**
    * 基于Base62编码的SecureRandom随机生成bytes.
    */
   public static String randomBase62(int length) {
      byte[] randomBytes = new byte[length];
      random.nextBytes(randomBytes);
      return Encodes.encodeBase62(randomBytes);
   }
   
   /**
    * Activiti ID 生成
    */
   @Override
   public String getNextId() {
      return IdGen.uuid();
   }

   @Override
   public Serializable generateId(Session session) {
      return IdGen.uuid();
   }
   
   public static void main(String[] args) {
      System.out.println(IdGen.uuid());
      System.out.println(IdGen.uuid().length());
      System.out.println(new IdGen().getNextId());
      for (int i=0; i<1000; i++){
         System.out.println(IdGen.randomLong() + "  " + IdGen.randomBase62(5));
      }
   }

}

 接下来看看CacheSessionDAO,该类继承了EnterpriseCacheSessionDAO,实现了了Session接口:

/**
 * 系统安全认证实现类
 * @author ThinkGem
 * @version 2014-7-24
 */
public class CacheSessionDAO extends EnterpriseCacheSessionDAO implements SessionDAO

SessionDao有啥用?

SessionDAO定义了从持久层操作session的标准。

怎么用?

跟我学Shiro-会话管理

Shiro笔记四(会话管理):SessionDao

posted @ 2022-06-17 16:52  EA2218764AB  阅读(156)  评论(0编辑  收藏  举报