SpringCache
官方文档:https://docs.spring.io/spring/docs/4.3.13.RELEASE/spring-framework-reference/htmlsingle/#cache
对应的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"> <cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="default"/>
</bean>
</set>
</property>
</bean>
<bean id="cacheManager2" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
</bean> </beans>
对应的实现:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import java.util.Date; /**
* Created by wangzhilei3 on 2017/12/27.
*/
@Service
@CacheConfig(cacheNames = "default", cacheManager = "cacheManager2")
public class CacheableService {
private static final Logger log = LoggerFactory.getLogger(CacheableService.class); /**
* 启用缓存
*
* @param l
* @return
*/
@Cacheable()
public Date getDate(Long l) {
log.info("缓存未生效");
return new Date();
} /**
* 更新缓存
*
* @param l
* @return
*/
@CachePut()
public Date putDate(Long l) {
log.info("更新");
return new Date();
} /**
* 删除缓存
*
* @param l
*/
@CacheEvict()
public void deleteDate(Long l) {
log.info("删除");
} }
增加 ”cacheNames“
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; /**
* Created by wangzhilei3 on 2017/12/27.
*/
@Service
@CacheConfig(cacheNames = "default,sting",cacheManager = "cacheManager2")
public class CacheableServiceOfString {
private static Logger logger = LoggerFactory.getLogger(CacheableServiceOfString.class);
@Cacheable("sting")
public String getDate(Long l){
logger.info("缓存未生效");
return "hello";
}
}
需要注意的是不同返回类型的方法共享同一个缓存(@Cacheable("sting")注解相同),当存在键冲突时,会产生类型转换异常。
当@CacheConfig(cacheNames = "default,sting",cacheManager = "cacheManager2")中的cacheNames 为多个,而方法注解@Cacheable()中的参数缺省时,两个缓存队列会同时生效,所以不同的返回类型要尽量避免采用相同的cacheName。
SpringCache的更多相关文章
- SpringCache缓存初探
body,table tr { background-color: #fff } table tr td,table tr th { border: 1px solid #ccc; text-alig ...
- SpringCache与redis集成,优雅的缓存解决方案
缓存可以说是加速服务响应速度的一种非常有效并且简单的方式.在缓存领域,有很多知名的框架,如EhCache .Guava.HazelCast等.Redis作为key-value型数据库,由于他的这一特性 ...
- SpringCache @Cacheable 在同一个类中调用方法,导致缓存不生效的问题及解决办法
由于项目需要使用SpringCache来做一点缓存,但自己之前没有使用过(其实是没有听过)SpringCache,于是,必须先学习之. 在网上找到一篇文章,比较好,就先学习了,地址是: https:/ ...
- SpringCache学习之操作redis
一.redis快速入门 1.redis简介 在java领域,常见的四大缓存分别是ehcache,memcached,redis,guava-cache,其中redis与其他类型缓存相比,有着得天独厚的 ...
- SpringBoot基础系列-SpringCache使用
原创文章,转载请标注出处:<SpringBoot基础系列-SpringCache使用> 一.概述 SpringCache本身是一个缓存体系的抽象实现,并没有具体的缓存能力,要使用Sprin ...
- spring-boot的spring-cache中的扩展redis缓存的ttl和key名
原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...
- SpringCache实战遇坑
1. SpringCache实战遇坑 1.1. pom 主要是以下两个 <dependency> <groupId>org.springframework.boot</g ...
- SpringCache学习实践
1. SpringCache学习实践 1.1. 引用 <dependency> <groupId>org.springframework.boot</groupId> ...
- SpringBoot2.X + SpringCache + redis解决乱码问题
环境:SpringBoot2.X + SpringCache + Redis Spring boot默认使用的是SimpleCacheConfiguration,使用ConcurrentMapCach ...
- AOP方法增强自身内部方法调用无效 SpringCache 例子
开启注解@EnableCaChing,配置CacheManager,结合注解@Cacheable,@CacheEvit,@CachePut对数据进行缓存操作 缺点:内部调用,非Public方法上使用注 ...
随机推荐
- EXCHANGE上冒充任意用户--Exchange Server权限提升漏洞(CVE-2018-8581)分析
0x00 前言 这是我们2018年Top 5趣案系列中的第三个案例.这些漏洞都有一些因素使它们从今年发布的大约1,400个报告中脱颖而出.今天我们将分析一个Exchange漏洞,它允许任何经过身份验证 ...
- RedHat 6 安装 MySQL 5.5 流程记录及相关问题解决方案
目录 1. RedHat 中 使用 yum 方式安装 MySQL 2. 安装过程中遇到的问题 1. RedHat 中 使用 yum 方式安装 MySQL 1.1 删除系统自带的 MySQL 5.1 r ...
- go test benchmark
Benchtest的简单使用 一个简单的benchtest用例 // 以BenchmarkXXX类似命名,并传入b *testing.B 参数 func BenchmarkLoopSum(b *tes ...
- 【Spring Cloud】Spring Cloud之整合Spring Cloud Bus以及最佳实践
一.整合步骤 1)加入Maven坐标 <!-- actuator监控模块 --> <dependency> <groupId>org.springframework ...
- zookeeper,及k8s基础概念
1.描述zookeeper集群中leader,follower,observer几种角色 Zookeeper: 分布式系统:是一个硬件或软件组件分布在网络中的不同的计算机之上,彼此间仅通过消息传递进行 ...
- elasticsearch使用ansj分词器
目前elasticsearch的版本已经更新到7.0以上了,不过由于客户需要5.2.2版本的elasticsearch,所以还是需要安装的,并且安装上ansj分词器.在部署ES的时候,采用容器的方式进 ...
- XSS简单练习
xss平台: https://xss.haozi.me题解: https://blog.csdn.net/AlexYoung28/article/details/82315538 对在xss.haoz ...
- 后台返回的base64的图片格式,前端如何转为普通的图片格式
在上一篇的博客当中,写了前端如何将普通的图片格式转为base64的图片,今天开发的时候遇到了后台返回的图片格式是base64的,我这边需要把base64的图片格式转为普通的,搜了一下js的方法,感觉很 ...
- Fiddler抓websocket协议的包,用jmeter做并发测试
1.Fiddler: 左边为ws请求url.右边为请求数据,响应数据 jmeter:
- httprunner学习14-完整的项目结构设计
前言 一个完整的接口自动化测试项目到底该如何设计?httprunner框架的知识点其实并不多,前面基本上把一些重要的概念都介绍完了. 本篇就是一个总结性的,可以用于实际工作中设计一个接口自动化测试项目 ...