- 浏览: 5062063 次
- 性别:
- 来自: 南京
-
文章分类
- 全部博客 (2844)
- java (1094)
- hadoop (37)
- jvm (39)
- hbase (11)
- sql (25)
- 异常 (83)
- div css (6)
- 数据库 (95)
- 有趣的code (15)
- struts2 (6)
- spring (124)
- js (44)
- 算法 (65)
- linux (36)
- hibernate (7)
- 中间件 (78)
- 设计模式 (2)
- 架构 (275)
- 操作系统 (91)
- maven (35)
- tapestry (1)
- mybatis (9)
- MQ (101)
- zookeeper (18)
- 搜索引擎,爬虫 (208)
- 分布式计算 (45)
- c# (7)
- 抓包 (28)
- 开源框架 (45)
- 虚拟化 (12)
- mongodb (15)
- 计算机网络 (2)
- 缓存 (97)
- memcached (6)
- 分布式存储 (13)
- scala (5)
- 分词器 (24)
- spark (104)
- 工具 (23)
- netty (5)
- Mahout (6)
- neo4j (6)
- dubbo (36)
- canal (3)
- Hive (10)
- Vert.x (3)
- docker (115)
- 分布式追踪 (2)
- spring boot (5)
- 微服务 (56)
- 淘客 (5)
- mesos (67)
- php (3)
- etcd (2)
- jenkins (4)
- nginx (7)
- 区块链 (1)
- Kubernetes (92)
- 驾照 (1)
- 深度学习 (15)
- JGroups (1)
- 安全 (5)
- 测试 (16)
- 股票 (1)
- Android (2)
- 房产 (1)
- 运维 (6)
- 网关 (3)
最新评论
-
明兜3号:
部署落地+业务迁移 玩转k8s进阶与企业级实践技能(又名:Ku ...
Kubernetes系统常见运维技巧 -
q328965539:
牛掰啊 资料收集的很全面
HDFS小文件处理解决方案总结+facebook(HayStack) + 淘宝(TFS) -
guichou:
fluent挂载了/var/lib/kubelet/pods目 ...
kubernetes上部署Fluentd+Elasticsearch+kibana日志收集系统 -
xu982604405:
System.setProperty("java.r ...
jmx rmi 穿越防火墙问题及jmxmp的替代方案 -
大漠小帆:
麻烦问下,“获取每个Item相似性最高的前N个Item”,这个 ...
协同过滤推荐算法在MapReduce与Spark上实现对比
最近项目刚好用的spring版本是Spring3.1.M1 ,好像 spring3就开始有了对缓存的整合。其实底层的实现也就是上篇文件一样。http://m635674608.iteye.com/admin/blogs/1678983
spring3.1.M1中负责cache的模块是org.springframework.context-3.1.0.M1.jar
与2.5时的modules模块类似,3.1的注解缓存也是在方法上声明注解,3.1同样提供了两个注解:
@Cacheable:负责将方法的返回值加入到缓存中
@CacheEvict:负责清除缓存
@Cacheable 支持如下几个参数:
value:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cache的name
key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL
condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL
1 //将缓存保存进andCache,并使用参数中的userId加上一个字符串(这里使用方法名称)作为缓存的key
2 @Cacheable(value="andCache",key="#userId + 'findById'")
3 public SystemUser findById(String userId) {
4 SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);
5 return user ;
6 }
7 //将缓存保存进andCache,并当参数userId的长度小于32时才保存进缓存,默认使用参数值及类型作为缓存的key
8 @Cacheable(value="andCache",condition="#userId.length < 32")
9 public boolean isReserved(String userId) {
10 System.out.println("hello andCache"+userId);
11 return false;
12 }
@CacheEvict 支持如下几个参数:
value:缓存位置名称,不能为空,同上
key:缓存的key,默认为空,同上
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false
//清除掉指定key的缓存
@CacheEvict(value="andCache",key="#user.userId + 'findById'")
public void EvictUserRole(SystemUser user) {
System.out.println("hello andCache delete"+user.getUserId());
}
//清除掉全部缓存
@CacheEvict(value="andCache",allEntries=true)
public final void EvictUsers(String[] reservedUsers) {
System.out.println("hello andCache deleteall");
}
Spring3集成了ehcache缓存。
第一步:配置Spring文件,声明支持缓存和支持注解
13 <?xml version="1.0" encoding="UTF-8"?>
14 <beans xmlns="http://www.springframework.org/schema/beans"
15 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
16 xmlns:cache="http://www.springframework.org/schema/cache"
17 xsi:schemaLocation="
18 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
19 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
20
21 <!-- 缓存注解声明,使用注解缓存 -->
22 <cache:annotation-driven cache-manager="cacheManager" />
23
24 <!-- 指定ehcache.xml的位置 -->
25 <bean id="cacheManagerFactory"
26 class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
27 p:configLocation="classpath:/ehcache.xml" />
28
29 <!-- 声明缓存Manager -->
30 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
31 p:cacheManager-ref="cacheManagerFactory" />
32
33 </beans>
第二步:配置Ehcache文件,定义缓存的具体策略。
34
35
36 <?xml version="1.0" encoding="UTF-8"?>
37
38
39
40
41
42 <ehcache>
43
44
45 <diskStore path="java.io.tmpdir" />
46
47
48 <defaultCache maxElementsInMemory="500" eternal="false"
49
50
51 timeToIdleSeconds="300" timeToLiveSeconds="1200" overflowToDisk="true" />
52
53
54 <cache name="DEFAULT_CACHE" maxElementsInMemory="5000" eternal="false"
55
56
57 timeToIdleSeconds="500" timeToLiveSeconds="500" overflowToDisk="true" />
58
59
60 </ehcache>
61
62
第三步:写一个使用缓存的测试方法:
63 import org.springframework.cache.annotation.Cacheable;
64 //将缓存保存进DEFAULT_CACHE,并使用参数中的userId加上一个字符串(这里使用方法名称)作为缓存的key
65 @Cacheable(value="DEFAULT_CACHE",key="#userId + 'findById'")
66 public SystemUser findById(String userId) {
67 SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);
68 return user ;
69 }
70
在这里,value = “”,对应了ehcache中的缓存名。
这种写法spring的缓存框架是根据传入进去的参数来判断查找缓存的,比如这里的param等于“admin”,缓存后,spring在从ehcache查“admin” 从缓存中取了。
如果你想更自主的控制缓存查找,可以使用key参数。
key使用SPEL语言(Spring开发的类似于jsp中的EL表达式的语言)。Key的默认生成如下:
1. 不提供参数,返回0
2. 只提供一个参数,直接返回该参数
3. 提供多个参数,则返回多个参数进行hash运算之后的结果。
总结:其实spring3的这个cache底层的实现就是和这里一样http://m635674608.iteye.com/admin/blogs/1678983
只是spring帮我们封装好了。当然spring封装的还是比较好的。
评论

发表评论
-
spring session序列化问题排查
2017-12-01 19:07 6302严重: Servlet.service() for ser ... -
spring mvc统一异常处理(@ControllerAdvice + @ExceptionHandler)
2017-12-01 17:09 3020spring 封装了非常强大的异常处理机制。本文选取@Co ... -
springboot注解
2017-12-01 09:44 1044@RestController和@RequestMappin ... -
Spring 4 xml 注解配置谅解
2017-12-01 09:39 1139《Spring in Action》4th E ... -
利用junit对springMVC的Controller进行测试
2017-11-30 16:26 1466平时对junit测试service/D ... -
spring cloud config实现datasource的热部署
2017-11-21 16:44 1643关于spring cloud config的基本使用,前面的 ... -
Spring+MyBatis实现数据库读写分离方案
2017-11-20 17:15 1125百度关键词:spring mybatis 多数据源 读写分离 ... -
spring session 退出登录 清理session
2017-11-10 09:26 6484/** * Allows creating an ... -
spring http session 监听 创建 失效session
2017-11-15 09:35 4892一.流程概述 主要是通过扩展ServletRequest ... -
spring session spring:session:sessions:expires 源码跟踪
2017-11-14 09:37 1531/** * Saves any attribut ... -
spring session 考虑问题解答
2017-11-09 09:50 668相关问题 2.Redis容量考虑,由于spring see ... -
spring 获取bean 测试
2017-11-09 09:51 688package com.jayway.springsess ... -
spring父子容器与读取properties文件
2017-11-09 09:51 837读取properties文件中的内容,可以使用@Value ... -
Spring在代码中获取bean的几种方式
2017-11-08 09:46 1244方法一:在初始化时保存ApplicationContext对 ... -
Spring中DispacherServlet、WebApplicationContext、ServletContext的关系
2017-11-08 09:41 833解释一: 要想很好理解这三个上下文的关系,需 ... -
Spring以及SPringmvc相关问题: ServletContext -父子容器
2017-11-08 09:41 630总结如下: 明确了Servlet规范中Servl ... -
DelegatingFilterProxy
2017-11-08 09:48 1228摘要: 配置过滤器代理类,通过spring配置的bean来 ... -
DelegatingFilterProxy-api
2017-11-09 09:51 574为什么用DelegatingFilterProxy ... -
spring的启动过程——spring和springMVC父子容器的原理
2017-11-15 09:29 734要想很好理解这三个上下文的关系,需要先熟悉spri ... -
spring中bean被多次实例化问题
2017-11-13 09:33 52481. 描述 spring中提供了两种主要方式实例化bea ...
相关推荐
通过上述步骤,我们可以成功地在Spring Boot项目中整合Spring Cache与Redis,实现了一个高效的分布式缓存解决方案。这种方式不仅可以提高系统的响应速度,还可以有效减轻数据库的压力,对于构建高并发、高可用的应用...
SpringBoot2.0在整合缓存管理方面有了很大的改进,特别是与Redis的结合,使得数据缓存更加高效。本文将详细介绍如何在SpringBoot2.0项目中整合SpringCache和Redis,以及如何利用Spring提供的缓存注解进行操作。 ...
> spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html <!--渲染效果--> ${Hello}">显示欢迎 <!--不渲染效果--> ${Hello}">显示欢迎 ``` ### 2. 替换属性 `...
SSM(Spring、Struts、MyBatis)整合是Java Web开发中常见的技术栈,其中涉及到多个组件和库的集成。在SSM整合过程中,jar包的选择和作用至关重要,它们构成了整个框架的基础。以下是对部分关键jar包的详细说明: 1...
struts2整合spring所需要的包 xwork-2.0.7.jar xwork核心包 antlr-2.7.6.jar 一个语言转换工具, Hibernate利用它实现 HQL 到 SQL 的转换模板相关操作需要包 javassist-3.9.0.GA.jar 代码生成工具 ...
- **Spring Cache**:Spring提供的抽象缓存层,支持多种缓存实现。 7. **分布式与集群**: - **Spring Cloud**:一套微服务解决方案,提供服务注册、服务发现、负载均衡等功能,适合大型游戏管理系统。 - **...
Spring Boot 自带的 `FreeMarkerAutoConfiguration` 类负责自动配置 FreeMarker。它使用 `FreeMarkerProperties` 类来获取配置信息,如模板路径、模板后缀等。默认情况下,模板位于 `classpath:/templates/` 目录下...
Hibernate可以使用不同cache缓存工具作为二级缓存。EHCache是缺省的cache缓存工具。如果没有其它的可选缓存工具,则为必需的。 hibernate3.jar hibernate3的核心类库。 itext.jar 是用于生成PDF文档的一个java...
**3. Abstract Base Class (ABC) 抽象基类** - **定义**:一种特殊的类,不能被实例化,通常包含一个或多个抽象方法,作为其他类的基础。 - **应用场景**:作为子类继承的基础类,强制子类实现特定的方法。 **4. ...