Spring Boot使用redis

0

添加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

redis配置:

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0

如果我们不添加redis配置,默认提供的是一个RedisTemplate<Object, Object>工具,我们可以自己实现一个配置:

package com.api.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * redis 配置
 */
@Configuration
public class RedisConfig {

	private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class);
	
	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
		LOGGER.info("初始化RedisTemplate");
		final RedisTemplate<String, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(factory);
		serializer(template);
		template.afterPropertiesSet();
		return template;
	}

	/**
	 * 设置序列化方式
	 */
	private void serializer(RedisTemplate<String, Object> template) {
		final Jackson2JsonRedisSerializer<?> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
		final ObjectMapper mapper = new ObjectMapper();
		// KEY
		template.setKeySerializer(new StringRedisSerializer());
		// VALUE
		mapper.setSerializationInclusion(Include.NON_NULL);
		mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		serializer.setObjectMapper(mapper);
		template.setValueSerializer(serializer);
	}

}

就可以使用了,下面是一个redis的工具:

package com.api.core.service;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * redis 工具
 */
@Service
public class RedisService {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	public Object get(String key) {
		return redisTemplate.opsForValue().get(key);
	}
	
	public void set(String key, Object value) {
		redisTemplate.opsForValue().set(key, value);
	}
	
	public void set(String key, Object value, long timeout, TimeUnit unit) {
		redisTemplate.opsForValue().set(key, value, timeout, unit);
	}
	
	public void del(String key) {
		redisTemplate.delete(key);
	}
	
}