Skip to content

Instantly share code, notes, and snippets.

@0xffan
Last active September 21, 2018 08:56
Show Gist options
  • Save 0xffan/64aca48adee8bbf5b779ccec16b69047 to your computer and use it in GitHub Desktop.
Save 0xffan/64aca48adee8bbf5b779ccec16b69047 to your computer and use it in GitHub Desktop.
Spring Data Redis (2.0.10.RELEASE): Lettuce pooling Java configuration.
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class RedisClient {
@SuppressWarnings("unused")
private static final Logger LOGGER = LoggerFactory.getLogger(RedisClient.class);
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* <p>说明:存储</p>
* <p>时间:2018年9月21日 下午3:18:32</p>
* @param key 键
* @param value 值
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* <p>说明:存储</p>
* <p>时间:2018年9月21日 下午3:18:29</p>
* @param key 键
* @param value 值
* @param expire 生存时间,单位为秒
*/
public void set(String key, Object value, int expire) {
redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
}
/**
* <p>说明:存储</p>
* <p>时间:2018年9月21日 下午3:18:29</p>
* @param key 键
* @param value 值
* @param expire 生存时间,单位为秒
*/
public Boolean setIfAbsent(String key, Object value, int expire) {
return redisTemplate.opsForValue().setIfAbsent(key, value);
}
/**
* <p>说明:获取key的值</p>
* <p>时间:2018年9月21日 下午3:20:04</p>
* @param key 键
* @return
*/
@SuppressWarnings("unchecked")
public <T> T get(String key) {
return (T) redisTemplate.opsForValue().get(key);
}
/**
* <p>说明:删除key</p>
* <p>时间:2018年9月21日 下午4:12:18</p>
* @param key
* @return true - 成功删除key
*/
public Boolean delete(String key) {
return redisTemplate.delete(key);
}
/**
* <p>说明:为指定的key设置生存时间,单位为秒</p>
* <p>时间:2018年9月21日 下午3:14:27</p>
* @param key
* @param expire 生存时间,单位为秒
* @return
*/
public Boolean expire(String key, int expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
/**
* <p>说明:批量存储</p>
* <p>时间:2018年9月21日 下午3:18:29</p>
* @param pairs 键/值对
*/
public void multiSet(Map<? extends String, ? extends Object> pairs) {
redisTemplate.opsForValue().multiSet(pairs);
}
/**
* <p>说明:批量获取</p>
* <p>时间:2018年9月21日 下午3:31:24</p>
* @param keys
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> multiGet(Collection<String> keys) {
return (List<T>) redisTemplate.opsForValue().multiGet(keys);
}
/**
* <p>说明:向哈希表存入键值对</p>
* <p>时间:2018年9月21日 下午3:33:33</p>
* @param key
* @param hashKey 哈希表中的key
* @param value 值
*/
public void hashPut(String key, String hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* <p>说明:向哈希表存入键值对,同时为哈希表设置生存时间</p>
* <p>时间:2018年9月21日 下午3:43:37</p>
* @param key
* @param map 要存入的键值对
* @param expire 生存时间,单位为秒
*/
public void hashPut(String key, String hashKey, Object value, int expire) {
redisTemplate.opsForHash().put(key, hashKey, value);
expire(key, expire);
}
/**
* <p>说明:批量向哈希表存入键值对</p>
* <p>时间:2018年9月21日 下午3:43:37</p>
* @param key
* @param map 要存入的键值对
*/
public void hashPutAll(String key, Map<? extends Object, ? extends Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* <p>说明:批量向哈希表存入键值对,同时为哈希表设置生存时间</p>
* <p>时间:2018年9月21日 下午3:43:37</p>
* @param key
* @param map 要存入的键值对
* @param expire 生存时间,单位为秒
*/
public void hashPutAll(String key, Map<? extends Object, ? extends Object> map, int expire) {
redisTemplate.opsForHash().putAll(key, map);
expire(key, expire);
}
/**
* <p>说明:批量向哈希表存入键值对,同时为哈希表设置生存时间</p>
* <p>时间:2018年9月21日 下午3:43:37</p>
* @param key
* @param map 要存入的键值对
* @param expire 生存时间,单位为秒
*/
public void batchHashPutAll(String key, Map<String, Map<? extends Object, ? extends Object>> maps) {
maps.forEach((k, m) -> {
hashPutAll(k, m);
});
}
/**
* <p>说明:获取哈希表中 hashKey 的值</p>
* <p>时间:2018年9月21日 下午3:33:33</p>
* @param key
* @param hashKey 哈希表中的key
*/
@SuppressWarnings("unchecked")
public <T> T hashGet(String key, String hashKey) {
return (T) redisTemplate.opsForHash().get(key, hashKey);
}
/**
* <p>说明:获取哈希表中 多个 hashKey 的值</p>
* <p>时间:2018年9月21日 下午3:33:33</p>
* @param key
* @param hashKeys 哈希表中的多个key
*/
@SuppressWarnings("unchecked")
public <T> T hashGet(String key, Collection<Object> hashKeys) {
return (T) redisTemplate.opsForHash().multiGet(key, hashKeys);
}
/**
* <p>说明:获取哈希表中所有的键值对</p>
* <p>时间:2018年9月21日 下午4:04:57</p>
* @param key
* @return
*/
public Map<Object, Object> hashGetAll(String key) {
return redisTemplate.opsForHash().entries(key);
}
}
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@Configuration
@PropertySource("classpath:redis.properties")
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.ssl}")
private boolean ssl;
@Value("${spring.redis.pool.maxActive}")
private Integer maxActive;
@Value("${spring.redis.pool.maxIdle}")
private Integer maxIdle;
@Value("${spring.redis.pool.minIdle}")
private Integer minIdle;
@Value("${spring.redis.pool.maxWait}")
private Long maxWait;
@Bean
public RedisTemplate<String,Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
};
@Bean
public StringRedisTemplate stringRedisTemplate() {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory());
return stringRedisTemplate;
};
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration standalone = new RedisStandaloneConfiguration(host, port);
standalone.setPassword(RedisPassword.of(password));
LettuceConnectionFactory redisConnectionFactory = new LettuceConnectionFactory(standalone, redisClientConfiguration());
return redisConnectionFactory;
}
@Bean
public LettuceClientConfiguration redisClientConfiguration() {
LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
builder.poolConfig(redisPoolConfig());
return builder.build();
}
@Bean
public GenericObjectPoolConfig redisPoolConfig() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
if (maxWait != null) {
config.setMaxWaitMillis(maxWait);
}
return config;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment