继续上篇,这篇介绍服务层缓存,基于注解的方式使用ehcache

注解的标签主要有4个:@Cacheable、@CacheEvict、@CachePut、@Caching,他们的用法是:

@Cacheable:调用方法时会先从缓存中取,如果没有就执行方法,然后将结果存入缓存
@CacheEvict:方法执行后会清空缓存
@CachePut:无论有没有缓存都会执行方法,然后将结果存入缓存
@Caching:组合多个cache注解使用

一、修改配置文件

1、修改spring-context-ehcache.xml文件,加入:

<!-- 开启缓存注解 -->
<cache:annotation-driven cache-manager="cacheManager" /> <!-- spring的ehcache缓存配置 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"></property>
</bean>

如果ehcache的bean的id就叫"cacheManager",cache-manager可以不加,因为默认值就是这个

2、修改ehcache-context.xml文件,加入:

<cache name="testDao"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="100000"
overflowToDisk="true"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
/>

"testDao"是接下来要用到的缓存名称,一定要加好,不然注解使用缓存时会提示找不到

二、在方法中加入cache注解

修改testDao.java类:

@SuppressWarnings("unchecked")
@Cacheable(value="testDao", key="'list'")
public List<testInfo> getList() { String hql = "from testInfo";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setCacheable(true);
return query.list();
} @Cacheable(value="testDao", key="'view' + #id")
public testInfo getInfo(String id) { return (testInfo) sessionFactory.getCurrentSession().get(testInfo.class, id);
} @Caching(
put={@CachePut(value="testDao", key="'view' + #testInfo.id")},
evict={@CacheEvict(value="testDao", key="'list'")}
)
public testInfo update(testInfo testInfo) {
testInfo.setName("789");
//update
return testInfo;
} @Caching(
evict={
@CacheEvict(value="testDao", key="'view' + #id"),
@CacheEvict(value="testDao", key="'list'")}
)
public void delete(String id) {
//delete
} @CacheEvict(value="testDao", allEntries=true)
public void deleteAll() {
//deleteAll
}

查询方法使用@Cacheable注解,value属性一定要加,更新方法使用@CachePut注解,还需要清除相关的list缓存,删除方法使用@CacheEvict注解,"allEntries=true"表示清空所有缓存。

Controller的方法也可以使用缓存注解。

三、运行测试

1、修改HelloController.java类,添加更新和删除的方法:

@RequestMapping("update/{id}")
public String update(@PathVariable("id") String id, HttpServletRequest request) { testInfo testInfo = new testInfo();
testInfo.setId(id);
testDao.update(testInfo); return "redirect:/hello/list2";
} @RequestMapping("delete/{id}")
public String delete(@PathVariable("id") String id, HttpServletRequest request) { testDao.delete(id); return "redirect:/hello/list2";
}

2、修改list.jsp页面,修改table的内容为:

<table border="1" width="150px">
<tr>
<th>列1</th>
<th>列2</th>
</tr>
<c:forEach items="${testList}" var="item">
<tr>
<td>${item.id}</td>
<td>
<a href="${path}/hello/view/${item.id}" target="_blank">${item.name}</a>
<a href="${path}/hello/update/${item.id}">更新</a>
<a href="${path}/hello/delete/${item.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>

3、测试

在这两个地方设置断点

第一次访问list和view的时候会命中断点,第二次就不会了

点击更新后,list的断点重新命中,再点击"233",没有命中断点,内容变成了"789",因为更新操作结束后更新了缓存

点击删除后,list和view的断点都会重新命中,因为删除操作后清空了缓存

实例代码地址:https://github.com/ctxsdhy/cnblogs-example

spring中基于注解使用ehcache的更多相关文章

  1. spring中基于注解使用AOP

    本文内容:spring中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...

  2. Spring中基于注解的IOC(二):案例与总结

    2.Spring的IOC案例 创建maven项目 导入依赖 pom.xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...

  3. Spring中基于注解的IOC(一):基础介绍

    1. Spring中的常用注解 注解配置和xml配置要实现的功能都是一样的,都要降低程序的耦合,只是配置的形式不一样 xml中配置示例: 注解分类: 1.用于创建对象的注解 它们的作用就和在xml中编 ...

  4. Spring中基于注解方式管理bean

    操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...

  5. spring中基于aop使用ehcache

    我就是对着这个博客看的 http://www.cnblogs.com/ctxsdhy/p/6421016.html

  6. Spring 中基于 AOP 的 @AspectJ注解实例

    @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格.通过在你的基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的 ...

  7. Spring:基于注解的Spring MVC

    什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...

  8. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  9. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

随机推荐

  1. 在win10中安装python3.6.6

    文章目录: 一.登录到官网下载指定python版本                二.在win10中安装python3.6.6并验证安装结果                三.运行python的三种方 ...

  2. ajax方法请求成功后,没有执行success的方法

    $.ajax( { type: "POST", url: "AddSupplier.aspx", dataType:"text", data ...

  3. 弹性布局(display:flex;)属性详解

    Flexbox 是 flexible box 的简称(注:意思是“灵活的盒子容器”),是 CSS3 引入的新的布局模式.它决定了元素如何在页面上排列,使它们能在不同的屏幕尺寸和设备下可预测地展现出来. ...

  4. MinorGC和FullGC的触发条件

    前言 无论是日常工作,还是企业面试,我们都会经常接触到GC.我们都知道GC是java中的垃圾回收策略.GC帮我们省去了很多事.在GC中,我经常听到的就属于MinorGC和FullGC了.那么在什么情况 ...

  5. vue中组件通信

    组件的通信 1. 父子组件通信 案例:   //父子组件通信思路 // 1 将父组件的数据传给子组件 在子组件上自定义单项数据绑定 // 2 子组件用props 接受自定义的那个:号属性 Vue.co ...

  6. Android P不能使用http

    三种方法解决Android P(安卓9.0)联网问题: 1.最简单的方法就是改用https,但很多的http接口都要一一改(非全局接口可以忽略方法1). 2.target降低至27,target27之 ...

  7. 安排:《蚂蚁花呗1234面:Redis+分布式架构+MySQL+linux+红黑树》

    前言: 大厂面试机会难得,为了提高面试通关率,建议朋友们在面试前先复盘自己的知识栈,依据掌握程度划分重要.优先级,系统地去学习!如果不准备充分就去参加面试,既会失去进入大厂的机会,更是对自己的不负责. ...

  8. WPF 浏览PDF 文件

    添加成功后会在工具箱里看到下图所示的控件.打开VS2010,新建项目(WpfPDFReader),右键项目添加User Control(用户控件).因为Adobe PDF Reader COM 组件是 ...

  9. abp(net core)+easyui+efcore实现仓储管理系统——菜单 (十六)

    系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+easyui+efcore实现仓储管理系统——解决方案介绍(二) ...

  10. Oracle - View

    Oracle View的创建 Create Or Replace View ViewName As Select * From Tables/View Where 条件;