SpringMVC使用Redis作为缓存提供者

(1)pom添加依赖项

 1     <dependency>
 2       <groupId>org.springframework.data</groupId>
 3       <artifactId>spring-data-redis</artifactId>
 4       <version>1.8.18.RELEASE</version>
 5     </dependency>
 6     <dependency>
 7       <groupId>org.apache.commons</groupId>
 8       <artifactId>commons-pool2</artifactId>
 9       <version>2.6.0</version>
10     </dependency>
11     <dependency>
12         <groupId>redis.clients</groupId>
13         <artifactId>jedis</artifactId>
14         <version>2.9.0</version>
15     </dependency>
16     <dependency>
17       <groupId>com.fasterxml.jackson.core</groupId>
18       <artifactId>jackson-databind</artifactId>
19       <version>2.9.8</version>
20     </dependency>

(2)设置相应的bean,添加@EnableCaching 注解启用缓存功能

 1 package cn.coreqi.config;
 2 
 3 import cn.coreqi.entities.User;
 4 import org.springframework.cache.CacheManager;
 5 import org.springframework.cache.annotation.EnableCaching;
 6 import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
 7 import org.springframework.cache.support.CompositeCacheManager;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.data.redis.cache.RedisCacheManager;
11 import org.springframework.data.redis.connection.RedisConnectionFactory;
12 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
13 import org.springframework.data.redis.core.RedisTemplate;
14 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
15 import org.springframework.data.redis.serializer.StringRedisSerializer;
16 
17 import java.util.ArrayList;
18 import java.util.List;
19 
20 @Configuration
21 @EnableCaching  //启用缓存 <cache:annotation-driven />
22 public class RedisConfig {
23 //    @Bean
24 //    public RedisConnectionFactory redis(){
25 //        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
26 //        redisStandaloneConfiguration.setHostName("192.168.205.128");
27 //        redisStandaloneConfiguration.setPort(6379);
28 //        redisStandaloneConfiguration.setDatabase(0);
29 //        //redisStandaloneConfiguration.setPassword(RedisPassword.of("123456"));
30 //
31 //        JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
32 //        jedisClientConfiguration.connectTimeout(Duration.ofMillis(6000));//  connection timeout
33 //
34 //        JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build());
35 //        return factory;
36 //    }
37 
38     //Redis连接工厂
39     @Bean
40     public RedisConnectionFactory redis(){
41         JedisConnectionFactory factory = new JedisConnectionFactory();
42         factory.setHostName("192.168.205.128");
43         factory.setPort(6379);
44         return factory;
45     }
46 
47     //RedisTemplate
48     @Bean
49     public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory redis){
50         RedisTemplate<String, User> redisTemplate = new RedisTemplate<>();
51         redisTemplate.setConnectionFactory(redis);
52         redisTemplate.setKeySerializer(new StringRedisSerializer());
53         redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<User>(User.class));
54         return redisTemplate;
55     }
56 
57 //    @Bean
58 //    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redis){
59 //        return new StringRedisTemplate(redis);
60 //    }
61 
62     //Redis缓存管理器
63     @Bean
64     public CacheManager cacheManager(RedisTemplate redisTemplate){
65         return new RedisCacheManager(redisTemplate);
66     }
67 
68     //注册多个缓存管理器(迭代)
69 //    @Bean
70 //    public CacheManager cacheManager(RedisTemplate redisTemplate){
71 //        CompositeCacheManager cacheManager = new CompositeCacheManager();
72 //        List<CacheManager> managers = new ArrayList<>();
73 //        managers.add(new ConcurrentMapCacheManager());
74 //        managers.add(new RedisCacheManager(redisTemplate));
75 //        cacheManager.setCacheManagers(managers);
76 //        return cacheManager;
77 //    }
78 }

(3)在dao相应的方法上添加缓存相关注解(可以在dao接口或dao实现类上添加)

此处仅作实例代码。

 1 package cn.coreqi.dao.redis;
 2 
 3 import cn.coreqi.entities.User;
 4 import org.springframework.cache.annotation.CacheEvict;
 5 import org.springframework.cache.annotation.CachePut;
 6 import org.springframework.cache.annotation.Cacheable;
 7 import org.springframework.stereotype.Repository;
 8 
 9 import java.util.List;
10 
11 @Repository
12 public class UserRedis {
13     @Cacheable("Users")
14     public List<User> getAll(){
15         return null;
16     }
17     @Cacheable("Users")
18     public User getById(int Id){
19         return null;
20     }
21     @CachePut(value = "Users",key = "#result.Id")
22     public User modify(User user){
23         return null;
24     }
25     @CacheEvict("Users")
26     public void delById(int Id){
27     }
28 }

 

posted @ 2019-01-16 20:15  SpringCore  阅读(2308)  评论(0编辑  收藏  举报