Skip to content

Latest commit

 

History

History
105 lines (68 loc) · 3.89 KB

54.md

File metadata and controls

105 lines (68 loc) · 3.89 KB

[TOC]

BOS物流项目54———使用ehcache缓存权限数据

一、说明

ehcache是专门缓存插件,可以缓存Java对象,提高系统性能。

之前,我们学习hibernate的二级缓存的时候,也用到了这个插件。

SSH与SSM学习之hibernate11——hibernate的二级缓存


二、在pom.xml文件中引入ehcache的依赖

        <!-- 引入ehcache的依赖 -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.6</version>
        </dependency>

三、在项目中提供ehcache的配置文件

在bos-web的中创建文件 ehcache.xml文件,内容如下

<ehcache>
    <diskStore path="java.io.tmpdir/shiro-ehcache"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
    />
</ehcache>

配置说明

配置项 说明
<diskStore> 当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口)。
<diskStore path=""> 用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index
name 缓存名称。
maxElementsInMemory 缓存最大个数。
eternal 对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds 设置对象在失效前的允许闲置时间(单位
timeToLiveSeconds 设置对象在失效前允许存活时间(单位
overflowToDisk 当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk 硬盘最大缓存个数。
diskPersistent 是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual
diskExpiryThreadIntervalSeconds 磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy 当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush 内存数量最大时是否清除。

四、在spring配置文件中配置缓存管理器对象,并注入给安全管理器对象

    <!--配置Shiro的安全管理器-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="bosRealm"/>
        <property name="cacheManager" ref="cacheManager"/>
    </bean>

    <!--注册缓存管理器-->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <!--注入ehcache的配置文件-->
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
    </bean>

五、源码下载

https://github.com/wimingxxx/bos-parent