本文章适用于测试开发环境是单机redis,生产环境是集群redis
使用到的jar包
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.redisson/redisson-spring-boot-starter -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.13.6</version>
<exclusions>
<exclusion>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-23</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-21</artifactId>
<version>3.13.6</version>
</dependency>
公共使用到的配置类 -JedisProperties
package com.property.framework.config.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* @className: RedisProperties
* @program: property-new-admin
* @description: redis配置
* @author: buliu
* @create: 2022-03-11 13:48
*/
@Data
@Configuration
@Component
@ConfigurationProperties(prefix = "spring.redis.jedis.pool")
public class JedisProperties {
private Integer maxIdle;
private Integer maxWait;
private Integer minIdle;
private Integer maxActive;
}
公共使用到的配置类 -RedisProperties
package com.property.framework.config.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @className: RedisProperties
* @program: property-new-admin
* @description: redis配置
* @author: buliu
* @create: 2022-03-11 13:48
*/
@Data
@Configuration
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
private String host;
private Integer port;
private String password;
private Integer database; // 这里要注意 貌似是说redis集群环境中不支持指定数据库名 这个不知道是不是这样
private int timeout;
private Cluster cluster;
public static class Cluster {
private List<String> nodes;
private Integer maxRedirects;
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
public Integer getMaxRedirects() {
return maxRedirects;
}
public void setMaxRedirects(Integer maxRedirects) {
this.maxRedirects = maxRedirects;
}
}
}
yml 文件片段(截取)
- 1、dev 环境-使用的是 jedis
# 当前为 dev 环境
# 数据源配置
# 单机版
spring:
redis:
database: 0
host: XX.XX.XX.XX
port: 6379
password: XXXXXXXX # 密码(默认为空)
timeout: 6000 # 连接超时时长(毫秒)
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
2、 prod 环境-使用的是 jedis
# 当前为 dev 环境
# 数据源配置
# 集群版
spring:
redis:
database: 0
password: XXXXXXXX# 密码(默认为空)
timeout: 6000 # 连接超时时长(毫秒)
cluster:
nodes: XXXXXXXX:7001,XXXXXXXX:7002,XXXXXXXX:7003,XXXXXXXX:7004,XXXXXXXX:7005,XXXXXXXX:7006
maxRedirects: 2 # 获取失败 最大重定向次数
jedis:
pool:
maxActive: 1000 # 连接池最大连接数(使用负值表示没有限制)
maxWait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
maxIdle: 10 # 连接池中的最大空闲连接
minIdle: 5 # 连接池中的最小空闲连接
redis 的配置类
package com.property.framework.config;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.property.framework.config.properties.JedisProperties;
import com.property.framework.config.properties.RedisProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.Serializable;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Autowired
private RedisProperties redisProperties;
@Autowired
private JedisProperties jedisProperties;
@Profile("dev") // 指的是在dev环境下注入bean
@Bean
public JedisPool redisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(jedisProperties.getMaxIdle());
jedisPoolConfig.setMaxWaitMillis(jedisProperties.getMaxWait());
if (StringUtils.isNotBlank(redisProperties.getPassword())) {
return new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort(), redisProperties.getTimeout(), redisProperties.getPassword(), redisProperties.getDatabase());
} else {
return new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort(), redisProperties.getTimeout(), null, redisProperties.getDatabase());
}
}
@Profile("dev") // 指的是在dev环境下注入bean
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(redisProperties.getHost());
redisStandaloneConfiguration.setPort(redisProperties.getPort());
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
jedisClientConfiguration.connectTimeout(Duration.ofMillis(redisProperties.getTimeout()));
jedisClientConfiguration.usePooling();
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build());
}
@Profile("prod") // 指的是在prod 环境下注入bean
@Bean
public JedisCluster getJedisCluster() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(jedisProperties.getMaxIdle());
poolConfig.setMaxWaitMillis(jedisProperties.getMaxWait());
poolConfig.setMinIdle(jedisProperties.getMinIdle());
poolConfig.setMaxTotal(jedisProperties.getMaxActive());
// 截取集群节点
String[] cluster = redisProperties.getCluster().getNodes().toArray(new String[0]);
// 创建set集合
Set<HostAndPort> nodes = new HashSet<>();
// 循环数组把集群节点添加到set集合中
for (String node : cluster) {
String[] host = node.split(":");
//添加集群节点
nodes.add(new HostAndPort(host[0], Integer.parseInt(host[1])));
}
//需要密码连接的创建对象方式
return new JedisCluster(nodes, redisProperties.getTimeout(), 1000, redisProperties.getCluster().getMaxRedirects(), redisProperties.getPassword(), poolConfig);
}
@Profile("prod") // 指的是在prod 环境下注入bean
@Bean
public RedisClusterConfiguration getClusterConfig() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());
redisClusterConfiguration.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());
redisClusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
return redisClusterConfiguration;
}
@Profile("prod") // 指的是在prod 环境下注入bean
@Bean
public JedisConnectionFactory redisConnectionFactory(RedisClusterConfiguration cluster) {
return new JedisConnectionFactory(cluster);
}
@Bean(name = "redisTemplate")
@SuppressWarnings({"rawtypes"})
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//使用 fastjson 序列化
JacksonRedisSerializer jacksonRedisSerializer = new JacksonRedisSerializer<>(Object.class);
// value 值的序列化采用 fastJsonRedisSerializer
template.setValueSerializer(jacksonRedisSerializer);
template.setHashValueSerializer(jacksonRedisSerializer);
// key 的序列化采用 StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
//缓存管理器
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory);
return builder.build();
}
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
public KeyGenerator wiselyKeyGenerator() {
return (target, method, params) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
Arrays.stream(params).map(Object::toString).forEach(sb::append);
return sb.toString();
};
}
@Bean
public RedisTemplate<String, Serializable> limitRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
class JacksonRedisSerializer<T> implements RedisSerializer<T> {
private Class<T> clazz;
private ObjectMapper mapper;
JacksonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
this.mapper = new ObjectMapper();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
}
@Override
public byte[] serialize(T t) throws SerializationException {
try {
return mapper.writeValueAsBytes(t);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes.length <= 0) {
return null;
}
try {
return mapper.readValue(bytes, clazz);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
redisson 的一些配置(基础版的配置) // 如有更好的配置可以自行调整与修改
package com.property.framework.config;
import com.property.framework.config.properties.RedisProperties;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.util.ArrayList;
import java.util.List;
/**
* @className: RedissonConfig
* @program: property-new-admin
* @description: redisson配置
* @author: buliu
* @create: 2022-03-11 13:54
*/
@Configuration
public class RedissonConfig {
@Autowired
private RedisProperties redisProperties;
@Profile("prod") // 指的是在prod 环境下注入bean
@Bean
public Redisson redisson() {
//redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加
List<String> clusterNodes = new ArrayList<>();
for (int i = 0; i < redisProperties.getCluster().getNodes().size(); i++) {
clusterNodes.add("redis://" + redisProperties.getCluster().getNodes().get(i));
}
Config config = new Config();
ClusterServersConfig clusterServersConfig = config.useClusterServers()
.addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
//设置密码
clusterServersConfig.setPassword(redisProperties.getPassword());
return (Redisson) Redisson.create(config);
}
@Profile("dev") // 指的是在 dev 环境下注入bean
@Bean
public RedissonClient getRedisson() {
Config config = new Config();
config.useSingleServer().setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort()).setPassword(redisProperties.getPassword());
//添加主从配置
return Redisson.create(config);
}
}
redis的操作service类 -这里我为了方便把抛出的Exception 给去掉了 如果 有需要可以百度
package com.property.common.core.redis;
import com.property.common.core.domain.RedisInfo;
import com.property.common.exception.RedisConnectException;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface RedisService {
/**
* 获取 redis 的详细信息
*
* @return List
*/
List<RedisInfo> getRedisInfo() throws RedisConnectException;
/**
* 获取 redis key 数量
*
* @return Map
*/
Map<String, Object> getKeysSize();
/**
* 获取 redis 内存信息
*
* @return Map
*/
Map<String, Object> getMemoryInfo();
/**
* 获取 key
*
* @param pattern 正则
* @return Set
*/
Set<String> getKeys(String pattern);
/**
* get命令
*
* @param key key
* @return String
*/
String get(String key);
/**
* set命令
*
* @param key key
* @param value value
* @return String
*/
String set(String key, String value);
/**
* set 命令
*
* @param key key
* @param value value
* @param milliscends 毫秒
* @return String
*/
String set(String key, String value, Long milliscends);
/**
* del命令
*
* @param key key
* @return Long
*/
Long del(String... key);
/**
* exists命令
*
* @param key key
* @return Boolean
*/
Boolean exists(String key);
/**
* pttl命令
*
* @param key key
* @return Long
*/
Long pttl(String key);
/**
* pexpire命令
*
* @param key key
* @param milliscends 毫秒
* @return Long
*/
Long pexpire(String key, Long milliscends);
/**
* zadd 命令
*
* @param key key
* @param score score
* @param member value
*/
Long zadd(String key, Double score, String member);
/**
* zrangeByScore 命令
*
* @param key key
* @param min min
* @param max max
* @return Set<String>
*/
Set<String> zrangeByScore(String key, String min, String max) throws RedisConnectException;
/**
* zremrangeByScore 命令
*
* @param key key
* @param start start
* @param end end
* @return Long
*/
Long zremrangeByScore(String key, String start, String end);
/**
* zrem 命令
*
* @param key key
* @param members members
* @return Long
*/
Long zrem(String key, String... members);
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @return
*/
public <T> String setCacheObject(final String key, final T value);
/**
* 获得缓存的基本对象。
*
* @param key 缓存键值
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(final String key);
void lget(String key, String value);
/**
* 自增1
*
* @param cacheKey
* @param i
* @return
*/
Long incr(String cacheKey, int i);
}
dev 环境下的实现类
package com.property.common.core.redis;
import cn.hutool.core.util.SerializeUtil;
import com.property.common.core.domain.RedisInfo;
import com.property.common.exception.RedisConnectException;
import com.property.common.function.JedisExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Client;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.*;
/**
* Redis 工具类,只封装了几个常用的 redis 命令,
* 可根据实际需要按类似的方式扩展即可。
*
* @author buliu
*/
@Profile("dev")
@Service("redisService")
public class RedisDevServiceImpl implements RedisService {
@Autowired
JedisPool jedisPool;
private static final String separator = System.getProperty("line.separator");
/**
* 处理 jedis请求
*
* @param j 处理逻辑,通过 lambda行为参数化
* @return 处理结果
*/
private <T> T excuteByJedis(JedisExecutor<Jedis, T> j) {
try (Jedis jedis = jedisPool.getResource()) {
return j.excute(jedis);
} catch (Exception e) {
return null;
}
}
@Override
public List<RedisInfo> getRedisInfo() {
String info = this.excuteByJedis(
j -> {
Client client = j.getClient();
client.info();
return client.getBulkReply();
}
);
List<RedisInfo> infoList = new ArrayList<>();
String[] strs = Objects.requireNonNull(info).split(separator);
RedisInfo redisInfo;
if (strs.length > 0) {
for (String str1 : strs) {
redisInfo = new RedisInfo();
String[] str = str1.split(":");
if (str.length > 1) {
String key = str[0];
String value = str[1];
redisInfo.setKey(key);
redisInfo.setValue(value);
infoList.add(redisInfo);
}
}
}
return infoList;
}
@Override
public Map<String, Object> getKeysSize() {
Long dbSize = this.excuteByJedis(
j -> {
Client client = j.getClient();
client.dbSize();
return client.getIntegerReply();
}
);
Map<String, Object> map = new HashMap<>();
map.put("create_time", System.currentTimeMillis());
map.put("dbSize", dbSize);
return map;
}
@Override
public Map<String, Object> getMemoryInfo() {
String info = this.excuteByJedis(
j -> {
Client client = j.getClient();
client.info();
return client.getBulkReply();
}
);
String[] strs = Objects.requireNonNull(info).split(separator);
Map<String, Object> map = null;
for (String s : strs) {
String[] detail = s.split(":");
if ("used_memory".equals(detail[0])) {
map = new HashMap<>();
map.put("used_memory", detail[1].substring(0, detail[1].length() - 1));
map.put("create_time", System.currentTimeMillis());
break;
}
}
return map;
}
@Override
public Set<String> getKeys(String pattern) {
return this.excuteByJedis(j -> j.keys(pattern));
}
@Override
public String get(String key) {
return this.excuteByJedis(j -> j.get(key.toLowerCase()));
}
@Override
public String set(String key, String value) {
return this.excuteByJedis(j -> j.set(key.toLowerCase(), value));
}
@Override
public String set(String key, String value, Long milliscends) {
String result = this.set(key.toLowerCase(), value);
this.pexpire(key, milliscends);
return result;
}
@Override
public Long del(String... key) {
return this.excuteByJedis(j -> j.del(key));
}
@Override
public Boolean exists(String key) {
return this.excuteByJedis(j -> j.exists(key));
}
@Override
public Long pttl(String key) {
return this.excuteByJedis(j -> j.pttl(key));
}
@Override
public Long pexpire(String key, Long milliseconds) {
return this.excuteByJedis(j -> j.pexpire(key, milliseconds));
}
@Override
public Long zadd(String key, Double score, String member) {
return this.excuteByJedis(j -> j.zadd(key, score, member));
}
@Override
public Set<String> zrangeByScore(String key, String min, String max) throws RedisConnectException {
return this.excuteByJedis(j -> j.zrangeByScore(key, min, max));
}
@Override
public Long zremrangeByScore(String key, String start, String end) {
return this.excuteByJedis(j -> j.zremrangeByScore(key, start, end));
}
@Override
public Long zrem(String key, String... members) {
return this.excuteByJedis(j -> j.zrem(key, members));
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @return
*/
@Override
public <T> String setCacheObject(String key, T value) {
return this.excuteByJedis(j -> j.set(key.getBytes(), SerializeUtil.serialize(value)));
}
/**
* 获得缓存的基本对象。
*
* @param key 缓存键值
* @return 缓存键值对应的数据
*/
@Override
public <T> T getCacheObject(String key) {
return (T) this.excuteByJedis(j -> j.get(key.getBytes()));
}
@Override
public void lget(String key, String value) {
// return this.excuteByJedis(j -> j.(key, members));
}
/**
* 自增1
*
* @param cacheKey
* @param i
* @return
*/
@Override
public Long incr(String cacheKey, int i) {
return this.excuteByJedis(j -> j.incrBy(cacheKey, i));
}
}
prod 实现类
package com.property.common.core.redis;
import cn.hutool.core.util.SerializeUtil;
import com.property.common.core.domain.RedisInfo;
import com.property.common.exception.RedisConnectException;
import com.property.common.function.JedisExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;
import java.util.*;
/**
* Redis 工具类,只封装了几个常用的 redis 命令,
* 可根据实际需要按类似的方式扩展即可。
*
* @author buliu
*/
@Profile("prod")
@Service("redisService")
public class RedisServiceImpl implements RedisService {
@Autowired
JedisCluster jedisPool;
private static final String separator = System.getProperty("line.separator");
/**
* 处理 jedis请求
*
* @param j 处理逻辑,通过 lambda行为参数化
* @return 处理结果
*/
private <T> T excuteByJedis(JedisExecutor<JedisCluster, T> j) {
try {
return j.excute(jedisPool);
} catch (Exception e) {
return null;
}
}
@Override
public List<RedisInfo> getRedisInfo() {
// String info = this.excuteByJedis(
// j -> {
// Client client = j.getClient();
// client.info();
// return client.getBulkReply();
// }
// );
// String[] strs = Objects.requireNonNull(info).split(separator);
// RedisInfo redisInfo;
// if (strs.length > 0) {
// for (String str1 : strs) {
// redisInfo = new RedisInfo();
// String[] str = str1.split(":");
// if (str.length > 1) {
// String key = str[0];
// String value = str[1];
// redisInfo.setKey(key);
// redisInfo.setValue(value);
// infoList.add(redisInfo);
// }
// }
// }
return new ArrayList<>();
}
@Override
public Map<String, Object> getKeysSize() {
// Long dbSize = this.excuteByJedis(
// j -> {
// Client client = j.getClient();
// client.dbSize();
// return client.getIntegerReply();
// }
// );
Map<String, Object> map = new HashMap<>();
// map.put("create_time", System.currentTimeMillis());
// map.put("dbSize", dbSize);
return map;
}
@Override
public Map<String, Object> getMemoryInfo() {
// String info = this.excuteByJedis(
// j -> {
// Client client = j.getClient();
// client.info();
// return client.getBulkReply();
// }
// );
// String[] strs = Objects.requireNonNull(info).split(separator);
// for (String s : strs) {
// String[] detail = s.split(":");
// if ("used_memory".equals(detail[0])) {
// map = new HashMap<>();
// map.put("used_memory", detail[1].substring(0, detail[1].length() - 1));
// map.put("create_time", System.currentTimeMillis());
// break;
// }
// }
return null;
}
@Override
public Set<String> getKeys(String pattern) {
return this.excuteByJedis(j -> j.keys(pattern));
}
@Override
public String get(String key) {
return this.excuteByJedis(j -> j.get(key.toLowerCase()));
}
@Override
public String set(String key, String value) {
return this.excuteByJedis(j -> j.set(key.toLowerCase(), value));
}
@Override
public String set(String key, String value, Long milliscends) {
String result = this.set(key.toLowerCase(), value);
this.pexpire(key, milliscends);
return result;
}
@Override
public Long del(String... key) {
return this.excuteByJedis(j -> j.del(key));
}
@Override
public Boolean exists(String key) {
return this.excuteByJedis(j -> j.exists(key));
}
@Override
public Long pttl(String key) {
return this.excuteByJedis(j -> j.pttl(key));
}
@Override
public Long pexpire(String key, Long milliseconds) {
return this.excuteByJedis(j -> j.pexpire(key, milliseconds));
}
@Override
public Long zadd(String key, Double score, String member) {
return this.excuteByJedis(j -> j.zadd(key, score, member));
}
@Override
public Set<String> zrangeByScore(String key, String min, String max) throws RedisConnectException {
return this.excuteByJedis(j -> j.zrangeByScore(key, min, max));
}
@Override
public Long zremrangeByScore(String key, String start, String end) {
return this.excuteByJedis(j -> j.zremrangeByScore(key, start, end));
}
@Override
public Long zrem(String key, String... members) {
return this.excuteByJedis(j -> j.zrem(key, members));
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @return
*/
@Override
public <T> String setCacheObject(String key, T value) {
return this.excuteByJedis(j -> j.set(key.getBytes(), SerializeUtil.serialize(value)));
}
/**
* 获得缓存的基本对象。
*
* @param key 缓存键值
* @return 缓存键值对应的数据
*/
@Override
public <T> T getCacheObject(String key) {
return (T) this.excuteByJedis(j -> j.get(key.getBytes()));
}
@Override
public void lget(String key, String value) {
// return this.excuteByJedis(j -> j.(key, members));
}
/**
* 自增1
*
* @param cacheKey
* @param i
* @return
*/
@Override
public Long incr(String cacheKey, int i) {
return this.excuteByJedis(j -> j.incrBy(cacheKey, i));
}
}