zl程序教程

您现在的位置是:首页 >  后端

当前栏目

SpringBoot缓存技术

2023-09-11 14:22:18 时间

一、SpringBoot整合Ehhcache

添加maven依赖

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-cache</artifactId>
4 </dependency>
5 <dependency>
6     <groupId>net.sf.ehcache</groupId>
7     <artifactId>ehcache</artifactId>
8 </dependency>

创建Ehcache配置文件src/main/resources/ehcache.xml

 1 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
 2     <diskStore path="java.io.tmpdir"/>
 3     <!--defaultCache:echcache 的默认缓存策略-->
 4     <defaultCache 
 5         maxElementsInMemory="10000" 
 6         eternal="false"
 7         timeToIdleSeconds="120" 
 8         timeToLiveSeconds="120" 
 9         maxElementsOnDisk="10000000"
10         diskExpiryThreadIntervalSeconds="120"
11         memoryStoreEvictionPolicy="LRU">
12         <persistence strategy="localTempSwap"/>
13     </defaultCache>
14     <!-- 自定义缓存策略-->
15     <cache name="user"
16         maxElementsInMemory="10000"
17         eternal="false"
18         timeToIdleSeconds="120"
19         timeToLiveSeconds="120"
20         maxElementsOnDisk="10000000"
21         diskExpiryThreadIntervalSeconds="120"
22         memoryStoreEvictionPolicy="LRU">
23         <persistence strategy="localTempSwap"/>
24     </cache>
25 </ehcache>

在application.properties中添加

spring.cache.ehcache.config=ehcache.xml

在启动类上加注解

@EnableCaching

在业务层可以使用注解缓存数据

1 @Override
2 @Cacheable(value="user")
3 public User findUserById(Integer id) {
4     return this.userMapper.findUserById(id);
5 }

@Cacheable的作用就是把方法的返回值添加到ehcache缓存中,value属性为ehcache配置文件中的缓存策略,不设定使用默认。

要求实体类需要实现Serializable接口。

@CacheEvict为清除缓存

1 @Override
2 @CacheEvict(value="user", allEntries=true)
3 public void saveUser(User user) {
4     this.userMapper.save(user);
5 }

二、SpringBoot整合Redis

SpringDataRedis是属于SpringData下的一个模块,作用是简化redis的操作。添加mvaen依赖:

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-data-redis</artifactId>
4 </dependency>

编写SpringDataRedis配置类

 1 @Configuration
 2 public class RedisConfig {
 3     @Bean
 4     public JedisPoolConfig jedisPoolConfig() {
 5         JedisPoolConfig config = new JedisPoolConfig();
 6         config.setMaxIdle(10);
 7         config.setMinIdle(5);
 8         config.setMaxTotal(20);
 9         return config;
10     }
11     @Bean
12     public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
13         JedisConnectionFactory factory = new JedisConnectionFactory();
14         factory.setPoolConfig(config);
15         factory.setHostName("127.0.0.1");
16         factory.setPort(6379);
17         return factory;
18     }
19     @Bean
20     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
21         RedisTemplate<String, Object> template = new RedisTemplate<>();
22         template.setConnectionFactory(factory);
23         template.setKeySerializer(new StringRedisSerializer());
24         template.setValueSerializer(new StringRedisSerializer());
25         return template;
26     }
27 }

也可以把参数独立出来

1 spring.redis.pool.max-idle=10
2 spring.redis.pool.min-idle=5
3 spring.redis.pool.max-total=20
4 spring.redis.hostname=127.0.0.1
5 spring.redis.port=6379
 1 @Configuration
 2 public class RedisConfig {
 3     @Bean
 4     @ConfigurationPropeties(prefix="spring.redis.pool")
 5     public JedisPoolConfig jedisPoolConfig() {
 6         JedisPoolConfig config = new JedisPoolConfig();
 7         //其他参数已经自动注入
 8         return config;
 9     }
10     @Bean
11     @ConfigurationProperties(prefix="spring.redis")
12     public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
13         JedisConnectionFactory factory = new JedisConnectionFactory();
14         factory.setPoolConfig(config);
15         //其他参数已经自动注入
16         return factory;
17     }
18     @Bean
19     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
20         RedisTemplate<String, Object> template = new RedisTemplate<>();
21         template.setConnectionFactory(factory);
22         template.setKeySerializer(new StringRedisSerializer());
23         template.setValueSerializer(new StringRedisSerializer());
24         return template;
25     }
26 }

使用springboot中的redis非常简单

1 this.redisTemplate.opsForValue().set("user", user);
2 (User)this.redisTemplate.opsForValue().get("user");