SpringBoot使用Redis缓存

(1)、添加依赖

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

(2)、配置Redis

1 spring.redis.host=192.168.205.128

(3)、使用

配置好Redis后,缓存注解就会把数据缓存到Redis中了,如果我们想要手动添加缓存可以使用

  • StringRedisTemplate  仅仅操作字符串
  • RedisTemplate  操作Key-Value对象

**Redis默认使用Jdk序列化机制(将对象序列化到Redis中必须要实现Serializable接口且缓存的数据是二进制),若要将数据以Json的形式序列化,可采用以下两种方式

  1.使用第三方jar包将对象转化为json

  2.更改redisTemplate默认的序列化规则

 1 package cn.coreqi.config;
 2 
 3 import cn.coreqi.entities.User;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.context.annotation.Primary;
 7 import org.springframework.data.redis.cache.RedisCacheConfiguration;
 8 import org.springframework.data.redis.cache.RedisCacheManager;
 9 import org.springframework.data.redis.cache.RedisCacheWriter;
10 import org.springframework.data.redis.connection.RedisConnectionFactory;
11 import org.springframework.data.redis.core.RedisTemplate;
12 import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
13 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
14 import org.springframework.data.redis.serializer.RedisSerializationContext;
15 import org.springframework.data.redis.serializer.RedisSerializer;
16 
17 import java.time.Duration;
18 
19 @Configuration
20 public class MyRedisConfig {
21     @Bean
22     public RedisTemplate<Object, User> userRedisTemplate(RedisConnectionFactory redisConnectionFactory){
23         RedisTemplate<Object, User> template = new RedisTemplate<>();
24         template.setConnectionFactory(redisConnectionFactory);
25         template.setDefaultSerializer(new Jackson2JsonRedisSerializer<User>(User.class));
26         return template;
27     }
28 
29     @Primary
30     @Bean
31     public RedisCacheManager userCacheManager(RedisConnectionFactory redisConnectionFactory) {
32         //初始化一个RedisCacheWriter
33         RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
34         //设置CacheManager的值序列化方式为json序列化
35         RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
36         RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
37                 .fromSerializer(jsonSerializer);
38         RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
39                 .serializeValuesWith(pair);
40         //设置默认超过期时间是30秒
41         defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
42         //初始化RedisCacheManager
43         return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
44     }
45 }

 

1 @Service
2 @CacheConfig(cacheNames = "user",cacheManager = "userCacheManager")   //抽取缓存的公共配置
3 //可以使用@Caching注解在方法上运用多个缓存注解
4 public class UserService {
5   .......      
6 }

*如何在代码中获取缓存

1     @Qualifier("userCacheManager")
2     @Autowired
3     private RedisCacheManager userCacheManager;
4 
5     public User cache(){
6         Cache user = userCacheManager.getCache("user");
7         user.put("2",new User("gaoxing","fanqisoft",1));
8         return user.get("1",User.class);
9     }

 

posted @ 2019-02-03 21:09  SpringCore  阅读(459)  评论(0编辑  收藏  举报