<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
package com.cmft.aisp.common.redis;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.JedisPoolConfig;
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private String timeout;
@Value("${spring.redis.pool.max-idle}")
private String maxIdle;
@Value("${spring.redis.pool.min-idle}")
private String minIdle;
@Value("${spring.redis.pool.max-active}")
private String maxTotal;
@Value("${spring.redis.pool.max-wait}")
private String maxWaitMillis;
@Value("${spring.redis.database}")
private String database;
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(Integer.parseInt(maxIdle));
jedisPoolConfig.setMinIdle(Integer.parseInt(minIdle));
jedisPoolConfig.setMaxTotal(Integer.parseInt(maxTotal));
jedisPoolConfig.setMaxWaitMillis(Integer.parseInt(maxWaitMillis));
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(Integer.parseInt(port));
jedisConnectionFactory.setPassword(password);
jedisConnectionFactory.setTimeout(Integer.parseInt(timeout));
jedisConnectionFactory.setDatabase(Integer.parseInt(database));
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer);
@SuppressWarnings({ "rawtypes", "unchecked" })
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);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
public class RedisTest {
@Autowired
RedisTemplate<String, Object> redisTemplate;
public void getData() {
ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
redisTemplate.opsForValue().set("key","value");
redisTemplate.opsForValue().get("key");
redisTemplate.opsForValue().append("key", "appendValue");
opsForValue.get("key", 2, 5);
opsForValue.getAndSet("key", "value");
opsForValue.size("key");
System.out.println("==============================================================================");
redisTemplate.hasKey("key");
redisTemplate.delete("key");
}