9 Redis Spring Boot集成

一、创建Spring Boot项目并添加依赖

compile('org.springframework.boot:spring-boot-starter-data-redis')

二、添加配置类
RedisConfig

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;
 
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
@Component
@Configuration
public class RedisConfig {
   @Value("${spring.redis.host}")
   private String host;
   @Value("${spring.redis.port}")
   private int port;
   @Value("${spring.redis.timeout}")
   private int timeout;
   @Value("${spring.redis.password}")
   private String password;
 
   private StringRedisTemplate redisTemplate;
 
   @Autowired
   public RedisConfig(StringRedisTemplate redisTemplate) {
      setSerializer(redisTemplate);
      this.redisTemplate = redisTemplate;
   }
 
   @Bean
   public JedisPoolConfig getRedisConfig() {
      JedisPoolConfig config = new JedisPoolConfig();
      return config;
   }
 
   @Bean
   public JedisPool getJedisPool() {
      JedisPoolConfig config = getRedisConfig();
      JedisPool pool = new JedisPool(config, host, port, timeout, password);
      return pool;
   }
 
 
   //缓存管理器
   @Bean
   public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
      RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
      //设置缓存过期时间
      cacheManager.setDefaultExpiration(10000);
      return cacheManager;
   }
 
   private void setSerializer(RedisTemplate template) {
      Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
      ObjectMapper om = new ObjectMapper();
      om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
      om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
      jackson2JsonRedisSerializer.setObjectMapper(om);
      template.setValueSerializer(jackson2JsonRedisSerializer);
   }
 
}

三、使用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
 
@RestController
public class RedisController {
   @Autowired
   private JedisPool jedisPool;
 
   @Autowired
   private RedisTemplate redisTemplate;
 
   /**
    * 使用Jedis
    */
   @GetMapping("/testRedis")
   public String testRedis() {
      String key = "Hello Redis";
      Jedis jedis = jedisPool.getResource();
      jedis.set(key, "Welcome to learn Redis!");
      return jedis.get(key);
   }
 
   /**
    * 使用Spring提供的RedisTemplate
    */
   @GetMapping("/testRedisT")
   public String testRedisTemplate() {
      String key = "Hello Redis";
      ValueOperations ops = redisTemplate.opsForValue();
      ops.set(key, "Welcome to learn Redis integrate with Spring Boot!");
      return (String) ops.get(key);
   }
}

你可能感兴趣的:(9 Redis Spring Boot集成)