spring+shiro+ehcache整合

1.导入jar包(pom.xml文件)

                 <!-- ehcache缓存框架 -->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.6.11</version>
		</dependency>

  Spring 整合 ehcache 包 spring-context-support 包

2.使用 ehcache ,导入 ehcache.xml 配置文件

解压 ehcache-core.jar 包 ,将 ehcache-failsafe.xml 复制 src/main/resources
改名 ehcache.xml

ehcache.xml文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <diskStore path="java.io.tmpdir"/>
	<!-- 默认缓存区 -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    <!-- 自定义缓存区 -->
    <cache name="bos"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
    <!-- 自定义缓存区 -->
    <cache name="standard"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>

</ehcache>name

  

diskStore 
指定数据存储位置,可指定磁盘中的文件夹位置
defaultCache 
默认的管理策略 
  name
Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)
maxElementsInMemory
在内存中缓存的element的最大数目
如果放入cache中的元素超过这个数值,有两种情况:
1、若overflowToDisk的属性值为true,会将cache中多出的元素放入磁盘文件中。
2、若overflowToDisk的属性值为false,会根据memoryStoreEvictionPolicy的策略替换cache中原有的元素。 
eternal
设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
  overflowToDisk
如果内存中数据超过内存限制,是否要缓存到磁盘上。
maxElementsOnDisk
在磁盘上缓存的element的最大数目,默认值为0,表示不限制。 
 timeToIdleSeconds
对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。以秒为单位。
timeToLiveSeconds 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。以秒为单位。 
diskPersistent 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。 
diskExpiryThreadIntervalSeconds
对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。以秒为单位。
iskSpoolBufferSizeMB
DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。 
memoryStoreEvictionPolicy
如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。 
缓存的3 种清空策略 :
FIFO ,first in first out (先进先出).
LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

3.applicationContext-ehcache.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/cache
		http://www.springframework.org/schema/cache/spring-cache.xsd ">

	 <!-- 缓存配置  -->
	<bean id="ehCacheManager"
		class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation" value="classpath:ehcache.xml" />
	</bean>

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

	<!-- spring 封装ehcache缓存管理器  -->
	<bean id="springCacheManager"
		class="org.springframework.cache.ehcache.EhCacheCacheManager">
		<property name="cacheManager" ref="ehCacheManager" />
	</bean>

	<!-- 激活spring 缓存注解 -->
	<cache:annotation-driven cache-manager="springCacheManager"/>

</beans>

  加载该配置文件

4.修改web.xml文件

<!-- spring配置文件位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>

 5.将cache管理器注入到安全管理器中 

6.对认证数据、授权数据 哪些进行缓存 ?

对应到ehcache.xml文件中的自定义的缓存缓存区

注意: 使需要缓存对象,实现 Serializable 接口

使用注解进行开发

第七步: 在被 spring 管理 bean 对象方法上 使用@Cacheable 、@CacheEvict
@Cacheable 应用缓存区,对方法返回结果进行缓存 ---- 用于查询方法
@CacheEvict 清除缓存区数据 --- 用于 增加、修改、删除 方法

原文地址:https://www.cnblogs.com/fjkgrbk/p/ehcache_spring_shiro.html

时间: 2024-08-28 11:58:07

spring+shiro+ehcache整合的相关文章

Spring+Shiro+CAS整合配置笔记

一.前言 Apache Shiro与Spring Security一样是Java的一个安全框架.那为什么与Spring整合却用Shiro?不要问为什么,任性!开个玩笑:D 其实我个人是认为Spring Security太过于笨重,要写太多的过滤器.我是个怕麻烦的人,Shiro的配置简单这就是我选择的理由,何况Spring官方自己都推荐使用Shiro.而Shiro权限控制与CAS单点登录的组合更是堪称Java安全中的***~( ̄_, ̄ )--但本文只介绍它们三者的整合配置(说白了就是给自己留个学习

010-shiro与spring web项目整合【四】缓存Ehcache、Redis

一.Ehcache shiro每次授权都会通过realm获取权限信息,为了提高访问速度需要添加缓存,第一次从realm中读取权限数据,之后不再读取,这里Shiro和Ehcache整合. 1.添加Ehcache的jar包 <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.5.3</versi

Ehcache 整合Spring 使用页面、对象缓存

Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的简单,Spring对Ehcache的支持也非常好.EHCache支持内存和磁盘的缓存,支持LRU.LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件.同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩提高响应速度.

Ehcache 整合Spring 使用页面、对象缓存(转载)

Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的简单,Spring对Ehcache的支持也非常好.EHCache支持内存和磁盘的缓存,支持LRU.LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件.同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩提高响应速度.

ehcache整合spring本地接口方式

一.简介 ehcache整合spring,可以通过使用echache的本地接口,从而达到定制的目的.在方法中根据业务逻辑进行判断,从缓存中获取数据或将数据保存到缓存.这样让程序变得更加灵活. 本例子使用maven构建,需要的依赖如下: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2

Ehcache 整合Spring 使用

本文参考:http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的简单,Spring对Ehcache的支持也非常好.EHCache支持内存和磁盘的缓存,支持LRU.LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件.同时它也能提供基于Filter的Cache,该Filter

项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据

1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据         2 菜单数据添加 2.1 使用combotree父菜单项数据     1. 页面:menu_add.jsp 2. 修改组件样式:easyui-combotree,修改url  树型表格treeGrid跟下来数combotree要求数据格式基本一致. Combotree通过text属性展示文本.   3. 使用treegrid组件的

Spring Boot:整合Shiro权限框架

综合概述 Shiro是Apache旗下的一个开源项目,它是一个非常易用的安全框架,提供了包括认证.授权.加密.会话管理等功能,与Spring Security一样属基于权限的安全框架,但是与Spring Security 相比,Shiro使用了比较简单易懂易于使用的授权方式.Shiro属于轻量级框架,相对于Spring Security简单很多,并没有security那么复杂. 优势特点 它是一个功能强大.灵活的.优秀的.开源的安全框架. 它可以胜任身份验证.授权.企业会话管理和加密等工作. 它

ehcache整合spring注解方式

一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件配置进去即可.在spring中使用ehcache有两种方式,一种是使用spring提供的封装,使用注解的方式配置在某个方法上面,第一次调用该方法的时候,将该方法执行返回的数据缓存,当再次执行的时候如果时间没有变,就直接冲缓存获取,该方法不执行:另一种方式是获取到ehcache本地接口,直接使用ehc