Spring Boot cache浅谈
SpringBoot与缓存JSR107Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry和Expiry。
CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可以在运行期访问多个CachingProvider。
CacheManager定义了创建、配置、获取、管理和控制多个唯一命名的Cache,这些Cache存在于CacheManager的上下文中。一个CacheManager仅被一个CachingProvider所拥有。
Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个Cache仅被一个CacheManager所拥有。
Entry是一个存储在Cache中的key-value对。
Expiry每一个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期的状态。一旦过期,条目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。
<ignore_js_op>![]()
二、Spring缓存抽象
Spring从3.1开始定义了org.springframework.cache.Cache
和org.springframework.cache.CacheManager接口来统一不同的缓存技术;
并支持使用JCache(JSR-107)注解简化我们开发;
•Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
•Cache接口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache等;
•每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。
•使用Spring缓存抽象时我们需要关注以下两点;
1、确定方法需要被缓存以及他们的缓存策略
2、从缓存中读取之前缓存存储的数据
<ignore_js_op>![]()
使用JSR107需要导入如下包
[XML] 纯文本查看 复制代码
|
1
2
3
4
|
<dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId></dependency> |
切换到redis缓存
先加依赖
[AppleScript] 纯文本查看 复制代码
|
1
2
3
4
5
6
7
8
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache </artifactId> </dependency> |
加了redis依赖之后spring会创建RedisCacheManager 默认配置,但是默认的RedisCacheManager使用的序列化器是jdk自带的
可以使用如下方式切换
[Java] 纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package cn.qw.springcache.config;import cn.qw.springcache.pojo.Employee;import org.springframework.cache.CacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ResourceLoader;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.cache.RedisCacheWriter;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.RedisSerializer;import java.net.UnknownHostException;import java.time.Duration;import java.util.LinkedHashSet;import java.util.List;@Configurationpublic class MyRedisConfig { /** * 缓存管理器 */ @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { //初始化一个RedisCacheWriter RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); //设置CacheManager的值序列化方式为json序列化 RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer(); RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair .fromSerializer(jsonSerializer); RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig() .serializeValuesWith(pair); //设置默认超过期时间是30秒 defaultCacheConfig.entryTtl(Duration.ofSeconds(30)); defaultCacheConfig.usePrefix(); //初始化RedisCacheManager RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig); return redisCacheManager; }} |
在service 配注解就可以使用
[AppleScript] 纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package cn.qw.springcache.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import javax.annotation.Resource;import cn.qw.springcache.pojo.Department;import cn.qw.springcache.dao.DepartmentMapper;@Service//@CacheConfig(cacheNames = "emp",cacheManager = )public class DepartmentService{ @Autowired private DepartmentMapper departmentMapper; public int deleteByPrimaryKey(Integer id) { return departmentMapper.deleteByPrimaryKey(id); } public int insert(Department record) { return departmentMapper.insert(record); } public int insertSelective(Department record) { return departmentMapper.insertSelective(record); } @Cacheable(cacheNames = "dep",key = "#id" ,condition = "#id>0") public Department selectByPrimaryKey(Integer id) { return departmentMapper.selectByPrimaryKey(id); } public int updateByPrimaryKeySelective(Department record) { return departmentMapper.updateByPrimaryKeySelective(record); } public int updateByPrimaryKey(Department record) { return departmentMapper.updateByPrimaryKey(record); }}
|
更多免费技术资料可关注:annalin1203

浙公网安备 33010602011771号