spring缓存cache的使用详解
作者:书生杨阳 发布时间:2023-03-28 11:36:14
标签:spring,缓存,cache
spring缓存cache的使用
在spring配置文件中添加schema和spring对缓存注解的支持:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.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"
default-autowire="byName">
<!--缓存配置-->
<cache:annotation-driven/>
在spring配置文件中加入缓存管理器:
<!-- generic cache manager -->
<bean id="cacheManager"
class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="hardwareCache"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="bannerCache"/>
</set>
</property>
</bean>
然后在代码的service的impl层加上如 * 解即可把数据缓存起来:
@Cacheable(value="bannerCache")
其中@Cacheable表示spring将缓存该方法获取到的数据,(缓存是基于key-value方式实现的),key为该方法的参数,value为返回的数据,当你连续访问该方法时你会发现只有第一次会访问数据库. 其他次数只是查询缓存.减轻了数据库的压力.
当更新了数据库的数据,需要让缓存失效时,使用下面的注解:
这个注解表示让appCache缓存的所有数据都失效。
@CacheEvict(value = "appCache", allEntries = true)
springcache配置缓存存活时间
Spring Cache @Cacheable本身不支持key expiration的设置,以下代码可自定义实现Spring Cache的expiration,针对Redis、SpringBoot2.0。
直接上代码:
@Service
@Configuration
public class CustomCacheMng{
private Logger logger = LoggerFactory.getLogger(this.getClass());
// 指明自定义cacheManager的bean name
@Cacheable(value = "test",key = "'obj1'",cacheManager = "customCacheManager")
public User cache1(){
User user = new User().setId(1);
logger.info("1");
return user;
}
@Cacheable(value = "test",key = "'obj2'")
public User cache2(){
User user = new User().setId(1);
logger.info("2");
return user;
}
// 自定义的cacheManager,实现存活2天
@Bean(name = "customCacheManager")
public CacheManager cacheManager(
RedisTemplate<?, ?> redisTemplate) {
RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory());
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(2));
return new RedisCacheManager(writer, config);
}
// 提供默认的cacheManager,应用于全局
@Bean
@Primary
public CacheManager defaultCacheManager(
RedisTemplate<?, ?> redisTemplate) {
RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory());
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
return new RedisCacheManager(writer, config);
}
}
来源:https://blog.csdn.net/Petershusheng/article/details/52397895


猜你喜欢
- 1.依赖的jar文件 jsch-0.1.53.jar2.登录方式有密码登录,和密匙登录 代码:主函数:import java.ut
- 查 看: File------>Project Structure-------
- /// 构造随机数 种子static int GetRandomSeed(){ &
- 一、什么是锁擦除锁擦除是指虚拟机即时编译器(JIT)在运行时,对一些代码上要求同步,但是被检测到不可能存在共享数据竞争的锁进行擦除。锁擦除的
- 本文以一个C#的SQL数据库字串操作函数为例,说明如何实现对SQL字符串过滤、检测SQL是否有危险字符、修正sql语句中的转义字符,确保SQ
- 今天给大家介绍一个仿微信的图片选择器:ImageSelector。ImageSelector支持图片的单选、限数量的多选和不限数量的多选。支
- 本文实例讲述了Java Socket实现传输压缩对象的方法。分享给大家供大家参考,具体如下:前面文章《Java Socket实现的传输对象功
- 一、依赖注入(DI)依赖注入听起来很高深的样子,其实白话就是:给属性赋值。一共有两种方法,第一是以构造器参数的形式,另外一种就是以setti
- 程序员讨厌写文档, 讨厌写注释, 而我还讨厌写日志, 输出一个 "Id=5, 姓名=王大锤
- C# Linq延迟查询在定义linq查询表达式时,查询是不会执行,查询会在迭代数据项时运行。它使用yield return 语句返回谓词为t
- import java.io.UnsupportedEncodingException;import java.security.Inval
- Unsafe类是啥?Java最初被设计为一种安全的受控环境。尽管如此,Java HotSpot还是包含了一个“后门”,提供了一些可以直接操控
- 在设置过webBrowser控件的ObjectForScripting属性后,还需要设置应用程序对com可见,不然会抛出一个异常(Objec
- @Scheduled不执行的原因1. 今天用@Schedule做了一个定时任务希望凌晨1点执行,代码如下@Servicepublic cla
- BufferedInputStream BufferedInputStream 是缓冲输入流。它继承于FilterInputSt
- DAO模式是接口的一个典型应用。1. StudenDaoListImpl.java与StudentDaoArrayImpl.java有何不同
- 首先声明本文是基于GitHub上"baoyongzhang"的SwipeMenuListView修改而来,该项目地址:h
- 前言这几天听朋友说JPA很好用,根本不用写sql。我在想一个程序员不写sql还能叫程序员?而且越高级的工具封装越多的工具,可拓展性和效率就非
- 指定创建派生类实例时应调用的基类构造函数;调用基类上已被其他方法重写的方法。注意:不能从静态方法中使用base关键字,base关键字只能在实
- 题意Description相信大家都做过"A+B Problem"了吧,这道题是它的加强版。输入两个整数 A , B ,