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
0
投稿
猜你喜欢
- SpringMvc中普通类注入Service为null场景:使用Quartz定时器时,普通的java类需要注入spring的service类
- WebService是一种跨编程语言和跨操作系统平台的远程调用技术所谓远程调用,就是一台计算机a上的一个程序可以调用到另外一台计算机b上的一
- 前言在工作中遇到这样一个问题:开发过程中将数据库的账号、密码等信息配置在了一个单独的properties配置文件中(使用明文)。但运维人员要
- 前言Springboot应用在启动的时候分为两步:首先生成 SpringApplication 对象 ,运行 SpringApplicati
- 本文主要探究的是关于Bean的作用域、生命周期的相关内容,具体如下。Bean的作用域Spring 3中为Bean定义了5中作用域,分别为si
- 方法重写(Override)和方法重载(Overload)都是面向对象编程中,多态特性的不同体现,但二者本身并无关联,它们的区别犹如马德华之
- 用java实现循环队列的方法:1、添加一个属性size用来记录眼下的元素个数。目的是当head=rear的时候。通过size=0还是size
- 在ibatis的xml文件里,我们去写sql语句,对应mapper类的方法,这些sql语句与控制台上没什么两样,但在有些功能上需要注意,如w
- 本文实例讲述了Java使用Preference类保存上一次记录的方法。分享给大家供大家参考。具体分析如下:在使用java中JFileChoo
- 在IntelliJ IDEA中一不小心将你本地代码给覆盖了,这个时候,你 ctrl + z
- 初次接触spring-boot的时候,我们经常会看到这样的文章:“
- 前言作为一个写java的使用最多的轻量级框架莫过于spring,不管是老项目用到的springmvc,还是现在流行的springboot,都
- protected 来谈谈protected访问权限问题。看下面示例1:Test.javaclass MyObject {}public c
- 关闭 IDEA 的自动检查更新(截图idea 2020 2.x)idea 右下角会有这样的更新提示2. 关闭 idea 自动检查更新取消勾选
- 前言最近新整了个博客网站,同事在gitee上找的,还不错,gitee上的地址在这里:拾壹博客管理系统。别人的业务,再好也有不满足自己的地方,
- 1.打开官网稍微学习一下,了解一下spring cloud是个什么东西,大概有哪些组件等https://spring.io/projects
- MyBatis 是一款常用的持久层框架,使得程序能够以调用方法的方式执行某个指定的SQL,将执行SQL的底层逻辑进行封装。多数与Spring
- 前言最近被问到了这个问题,第一次回答的也是很不好,在此参考网上答案进行整理记录。供大家学习参考。Synchronized修饰非静态方法Syn
- 写在前面从Java 1.0开始,引入java.io包;到Java 1.4再扩展了java.nio包;再到java 1.7又添加了新的流类,使
- 本文实例为大家分享了java仿windows记事本小程序的具体代码,供大家参考,具体内容如下import java.awt.Checkbox