一:Spring和Ehcache缓存集成

业务问题:如果仓库不经常变动,大量进出库,总是需要查询仓库列表 (列表重复) ,使用缓存优化 !

阅读spring规范29章节

第一步: 导入ehcache的jar 和 ehcache.xml

ehcache-core-2.6.6.jar

需要导入spring-contextsupport 的jar

第二步: 配置自定义缓存区 <cache name=”” >

<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="zidingyi"

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

<persistence strategy="localTempSwap"/>

</cache>

</ehcache>

第三步: 配置Sprin的applicationContext.xml

引用cache 名称空间

<?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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

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/aop

   http://www.springframework.org/schema/aop/spring-aop.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context.xsd

   http://www.springframework.org/schema/tx

   http://www.springframework.org/schema/tx/spring-tx.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>

<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">

<property name="cacheManager" ref="ehCacheManager" />

</bean>

<cache:annotation-driven cache-manager="springCacheManager"/>

</beans>

第四步:程序中使用 @Cacheable 和 @CacheEvict 管理缓存

@Override

@CacheEvict(allEntries = true, value = "storemanager")

// 清空缓存区

public void saveStore(Store store) {

storeDAO.save(store);

}

@Override

@Cacheable("storemanager")

// 这里的storemanager 自定义缓存区的name

public List<Store> findAllStores() {

return storeDAO.findAll(Store.class);

}

这两个动作都是在service层进行。

二:hibernate中Ehcache的的基本配置

第一步:导入ehcache的jar包

ehcache依赖 backport-util-concurrent 和 commons-logging

第二步:配置ehcache默认的核心配置文件ehcache.xml

ehcache.xml(名字固定)(放在类路径下)

解压 ehcache的jar ,将根目录 ehcache-failsafe.xml 改名为 ehcache.xml 复制 src

配置后如下:

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

<!-- <diskStore path="java.io.tmpdir"/> -->

<!-- 缓存的路径改为z盘 -->

<diskStore path="z:"/>

<defaultCache

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

overflowToDisk="true"

maxElementsOnDisk="10000000"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU"

/>

<!-- 自定义缓存配置 -->

<!-- book在开启二级缓存的时候,就不走默认的缓存配置了,走自己的配置策略 -->

<cache name="cn.itcast.a_isolation.Book"

maxElementsInMemory="1"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

overflowToDisk="true"

maxElementsOnDisk="10000000"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU"

/>

</ehcache>

第三步:配置Hibernate启用二级缓存

在hibernate.cfg.xml中配置

<!-- 开启“基本”二级缓存 策略-->

<property name="hibernate.cache.use_second_level_cache">true</property>

第四步:配置二级缓存提供商

<!-- 指定二级缓存产品的提供商 -->

<property  name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

第五步:配置要缓存的数据(类)和并发策略

你要缓存哪些数据-对象,这里可以有两种方法配置:

方法一: 在hibernate.cfg.xml 进行配置

<!-- 配置缓存的类- -->

<class-cache usage="read-write" class="cn.awjw.domain.qp.Customer"/>

方法二: 在XXX.hbm.xml 进行配置

<!—对类的数据进行二级缓存—>

<cache usage=”read-write”/>

三:Ehcache在shiro中的使用(maven方式)

问题:当自定义Realm实现授权方法后, 每次调用需要权限控制页面,都要调用Realm的授权方法 (Realm授权方法, 根据用户查询 角色和权限信息, 每次相同用户查询数据都是一样的 ) ----------------- 浪费性能

解决:使用缓存优化,当第一次授权,将授权数据 AuthorizationInfo 放入缓存 ,第二次以后查询,直接从缓存中取出 AuthorizationInfo 数据,不需要查询数据库

第一步: 通过坐标 导入ehcache 的jar包

<ehcache.version>2.6.10</ehcache.version>

<dependency>

<groupId>net.sf.ehcache</groupId>

<artifactId>ehcache-core</artifactId>

<version>${ehcache.version}</version>

</dependency>

第二步: 在src/main/resources 提供 ehcache.xml 配置文件

先从ehcachee的jar包copy出默认的配置,然后修改后如下:

<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"

overflowToDisk="true"

maxElementsOnDisk="10000000"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU"

/>

 <!--自己配置的缓存区 -->

<cache name="bosCache"

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

overflowToDisk="true"

maxElementsOnDisk="10000000"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

</cache>

</ehcache>

第三步: 配置applicationContext.xml

首先要添加spring对Ehcache的支持,引入坐标

<spring-support.version>3.2.0.RELEASE</spring-support.version>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring-support.version}</version>

</dependency>

配置ehcache缓存管理器:

<!-- ehcache 缓存管理器  -->

<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

<property name="configLocation" value="classpath:ehcache.xml"></property>

</bean>

第四步: shiro整合ehcache

<!-- shiro 缓存管理器 -->

<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">

<property name="cacheManager" ref="ehcacheManager"></property>

</bean>

第五步: 将shiro缓存管理器 注入安全管理器

<!-- 安全管理器 -->

<bean id="securityManager"

class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

<!-- 在安全管理器,应该注入 Realm 连接安全数据  -->

<property name="realm" ref="myRealm"></property>

<property name="cacheManager" ref="shiroCacheManager"></property>

</bean>

第六步: 在realm指定 使用缓存区名称

<bean id="myRealm" class="cn.awjw.bos.realm. myRealm" >

<!-- 在realm指定使用缓存名称-->

<property name="authorizationCacheName" value="myCache"/>

</bean>

重启项目,即可在shiro中使用缓存技术了。

二级缓存EhCache在几种应用技术的配置方法和步骤总结的更多相关文章

  1. hibernate二级缓存ehcache

    与Session相对的是,SessionFactory也提供了相应的缓存机制.SessionFactory缓存可以依据功能和目的的不同而划分为内置缓存和外置缓存. SessionFactory的内置缓 ...

  2. Hibernate4.1.4配置二级缓存EHCache步骤

    1.当然首先引入EHCache相关的jar包 这些包不需要另外下载,在Hibernate官方网站下载Hibernate4.1.7的压缩包(如:hibernate-release-4.1.7.Final ...

  3. js相关(easyUI),触发器,ant,jbpm,hibernate二级缓存ehcache,Javamail,Lucene,jqplot,WebService,regex,struts2,oracle表空间

    *********************************************js相关********************************************* // 在指 ...

  4. hibernate二级缓存ehcache hibernate配置详解

    <!-----------------hibernate二级缓存ehcache------------------------->hibernate配置 <prop key=&quo ...

  5. Hibernate-ORM:16.Hibernate中的二级缓存Ehcache的配置

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的二级缓存的配置,作者将使用的是ehcache缓存 一,目录 1.二级缓存的具 ...

  6. 四)mybatis 二级缓存 ehcache 常见问题

    1. java.io.NotSerializableException 94359 [EH_CACHE Async Replication Thread] ERROR n.s.e.d.jgroups. ...

  7. FTP服务—三种登录类型的配置方法

    目录 一.部署 二.配置vsftpd 1.匿名用户登录配置 2.系统用户登录配置 3.虚拟用户登录配置 1. 创建虚拟用户名单文件 2. 生成虚拟用户口令认证文件 3. 创建FTP根目录及虚拟用户映射 ...

  8. phpunit 生成三种日志文件的配置方法

    #目录结构 windows bin目录下 ├── phpunit.phar ├── phpunit.cmd ├── phpunit.xml ├── build.xml ├── ArrTest.php ...

  9. 5种JVM调优配置方法概览!!!

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

随机推荐

  1. 封装JedisClient.提供API实现对redis的操作

    需要导包,jedis-2.8.1.jar和博主的序列化工具类SerializeUtils package com.demo.redis; import java.util.ArrayList; imp ...

  2. swift - 听云监测(Testin - Bugout) - 集成

    听云的SDK集成放的太难找了,官方demo到现在也没找到.. 我找了半天没找到在哪,看下面俩链接吧, 切记:添加的三方库 以文档为主,视频里的三方库 不一定正确 iOS SDK 集成:https:// ...

  3. FOR ALL ENTRIES的使用

    使用FOR ALL ENTRIES时注意: 1.一定要确定要有是否为空的判断 2.一定要注明两个表之间数据的关系 eg: IF GT_TJ30T[] IS NOT INITIAL.      SELE ...

  4. rbac集成 权限分配。之角色管理

    权限分配功能拆分: a. 角色管理 b. 用户管理 c. 菜单和权限的管理 d. 批量的权限操作 e. 分配权限 先实现 角色管理: 无非也就是,增删改查: 定义路由, 编写视图. 1.查看角色页面: ...

  5. linux C程序中获取shell脚本输出(如获取system命令输出)

    转载自 http://blog.csdn.net/hjxhjh/article/details/7909518 1. 前言 Unix 界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些 ...

  6. python3.6.5 路径处理与规范化

    在Linux和Mac平台上,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为饭斜杠. >>> os.path.normcase('c: ...

  7. Linux驱动之建立一个hello模块

    目标:在开发板上执行insmod hello.ko能在控制台打印出hello init:接着执行rmmod会在控制台打印出hello exit 建立一个hello模块的步骤如下: 1.建立一个hell ...

  8. C#做一个写txt文件流的测试,为什么配置低的机器写入的还快

    测试机:笔记本i7 8G 固态硬盘 由于采取读码写入txt方式, 读码频率挺高,文件名为日期格式,当前采用每次读码打开文件写入的方式, 为什么没用sb,因为怕断电情况的数据丢失.所以采取每条存入的方式 ...

  9. php伪静态与重定向

    什么是伪静态 伪静态是相对于真静态来讲的,伪静态只是改变了URL格式,实际还是动态页面,有真静态一样的SEO,真静态访问一个静态页面,服务器(apache,nginx)直接读取磁盘静态文件,伪静态是动 ...

  10. Ionic3--数据存储

    1.使用sqlite cordova plugin add cordova-sqlite-storage --save npm install --save @ionic/storage (本地存储) ...