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>

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 清除缓存区数据 --- 用于 增加、修改、删除 方法

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

  1. Spring和EhCache整合(针对使用了Shiro)

    https://blog.csdn.net/a243293719/article/details/78277895

  2. spring和ehcache整合,实现基于注解的缓存实现

    要实现基于注解的缓存实现,要求Spring的版本在3.1或以上版本. 首先需要在spring的配置文件中添加对缓存注解的实现: <?xml version="1.0" enc ...

  3. Ehcache学习总结(2)--Ehcache整合spring配置

    首先需要的maven依赖为: [html] view plain copy <!--ehcache--> <dependency> <groupId>com.goo ...

  4. Shiro【授权过滤器、与ehcache整合、验证码、记住我】

    前言 本文主要讲解的知识点有以下: Shiro授权过滤器使用 Shiro缓存 与Ehcache整合 Shiro应用->实现验证码功能 记住我功能 一.授权过滤器测试 我们的授权过滤器使用的是pe ...

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

    一.Ehcache shiro每次授权都会通过realm获取权限信息,为了提高访问速度需要添加缓存,第一次从realm中读取权限数据,之后不再读取,这里Shiro和Ehcache整合. 1.添加Ehc ...

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

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...

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

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  8. ehcache整合spring本地接口方式

    一.简介 ehcache整合spring,可以通过使用echache的本地接口,从而达到定制的目的.在方法中根据业务逻辑进行判断,从缓存中获取数据或将数据保存到缓存.这样让程序变得更加灵活. 本例子使 ...

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

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

随机推荐

  1. Git分支merge和rebase的区别

    Git merge是用来合并两个分支的. git merge b # 将b分支合并到当前分支 同样 git rebase b,也是把 b分支合并到当前分支 原理 如下: 假设你现在基于远程分支&quo ...

  2. day29单例模式的4种实现模式

    单例模式的四种实现模式单例模式实现方式一: import settings class MySQL:  __instance=None  def __init__(self, ip, port):   ...

  3. matlab-画一个圆

    我们可以用 李萨如图形 的思路去画一个圆,或者一个椭圆. x,y是圆心所在坐标,r是半径,nseg是边缘段数(越高,边缘越顺滑,建议100以上),S是plot的样式设置字符 function Draw ...

  4. java中Class.getMethod方法

    Method Class.getMethod(String name, Class<?>... parameterTypes)的作用是获得对象所声明的公开方法 该方法的第一个参数name是 ...

  5. el-tabs添加滚动条

    element-ui的el-tabs默认是没有滚动条的,可在 el-tab-pane上添加: <el-tab-pane style="height:90%;overflow-y:aut ...

  6. Sybase采用load table加载文本数据中的部分字段数据

    LOAD TABLE语句实现从数据文件中装载部分列数据的方法.转载自:https://www.cnblogs.com/lizm166/p/8116475.html(有修改) LOAD TABLE:从数 ...

  7. LevelDB源码分析-MemTable

    MemTable(db/memtable.h db/memtable.cc db/skiplist.h) LevelDB中存储在内存中的那部分KV数据都存储在memtable中,而memtable中的 ...

  8. Vue.js组件之间的调用

    index.html: <div id="app"></div> 运行完index.html之后自动寻找运行main.js文件 main.js: impor ...

  9. resize2fs

    VPS是15G的..但是dh和fdisk显示不一样..求解 df:Filesystem Size Used Avail Use% Mounted onudev 236M 0 236M 0% /devt ...

  10. 配置Java运行环境

    变量名:JAVA_HOME    变量值:D:\Program Files\Java\jdk1.8.0_151(java安装目录) 变量名:Path                  变量值:.;%J ...