使用spring-data-redis实现incr自增
注意事项:本文搭建的redis配置了密码,所以jedis客户端要使用比较新的版本,老版本不支持密码,需要修改源码比较麻烦
1. 引入jedis
redis.clients
jedis
2.9.0
2. 注入JedisCluster
bean
import com.le.luffi.media.common.Constant;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
import java.util.Set;
@Configuration
public class JedisClusterConfig {
@Bean
public JedisCluster getJedisCluster() {
String[] serverArray = Constant.REDIS_CACHE_CLUSTER_NODES.split(",");
Set nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
JedisCluster jedisCluster = new JedisCluster(nodes, Constant.REDIS_CACHE_COMMANDTIMEOUT,
Constant.REDIS_CACHE_SOTIMEOUT, Constant.REDIS_CACHE_MAXATTEMPTS, Constant.REDIS_CACHE_CLUSTER_PASSWORD, new GenericObjectPoolConfig());
return jedisCluster;
}
}
3. 创建JedisTemplate
模板
import com.le.luffi.media.common.Constant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;
@Component
public class JedisTemplate {
private static final Logger LOGGER = LoggerFactory.getLogger(JedisTemplate.class);
@Autowired
private JedisCluster jedisCluster;
private static final String KEY_SPLIT = ":"; //用于隔开缓存前缀与缓存键值
/**
* 设置缓存
*
* @param prefix 缓存前缀(用于区分缓存,防止缓存键值重复)
* @param key 缓存key
* @param value 缓存value
*/
public void set(String prefix, String key, String value) {
if (StringUtils.isBlank(prefix)) throw new IllegalArgumentException("prefix must not null!");
if (StringUtils.isBlank(key)) throw new IllegalArgumentException("key must not null!");
jedisCluster.set(prefix + KEY_SPLIT + key, value);
LOGGER.debug("RedisUtil:set cache key={},value={}", prefix + KEY_SPLIT + key, value);
}
/**
* 设置缓存,并且自己指定过期时间
*
* @param prefix
* @param key
* @param value
* @param expireTime 过期时间
*/
public void setWithExpireTime(String prefix, String key, String value, int expireTime) {
if (StringUtils.isBlank(prefix)) throw new IllegalArgumentException("prefix must not null!");
if (StringUtils.isBlank(key)) throw new IllegalArgumentException("key must not null!");
jedisCluster.setex(prefix + KEY_SPLIT + key, expireTime, value);
LOGGER.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", prefix + KEY_SPLIT + key, value,
expireTime);
}
/**
* 设置缓存,并且由配置文件指定过期时间
*
* @param prefix
* @param key
* @param value
*/
public void setWithExpireTime(String prefix, String key, String value) {
if (StringUtils.isBlank(prefix)) throw new IllegalArgumentException("prefix must not null!");
if (StringUtils.isBlank(key)) throw new IllegalArgumentException("key must not null!");
// int EXPIRE_SECONDS = redisProperties.getExpireSeconds();
jedisCluster.setex(prefix + KEY_SPLIT + key, Constant.REDIS_CACHE_EXPIRESECONDS, value);
LOGGER.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", prefix + KEY_SPLIT + key, value, Constant.REDIS_CACHE_EXPIRESECONDS);
}
/**
* 获取指定key的缓存
*
* @param prefix
* @param key
*/
public String get(String prefix, String key) {
if (StringUtils.isBlank(prefix)) throw new IllegalArgumentException("prefix must not null!");
if (StringUtils.isBlank(key)) throw new IllegalArgumentException("key must not null!");
String value = jedisCluster.get(prefix + KEY_SPLIT + key);
LOGGER.debug("RedisUtil:get cache key={},value={}", prefix + KEY_SPLIT + key, value);
return value;
}
/**
* 删除指定key的缓存
*
* @param prefix
* @param key
*/
public void deleteWithPrefix(String prefix, String key) {
if (StringUtils.isBlank(prefix)) throw new IllegalArgumentException("prefix must not null!");
if (StringUtils.isBlank(key)) throw new IllegalArgumentException("key must not null!");
jedisCluster.del(prefix + KEY_SPLIT + key);
LOGGER.debug("RedisUtil:delete cache key={}", prefix + KEY_SPLIT + key);
}
public void delete(String key) {
if (StringUtils.isBlank(key)) throw new IllegalArgumentException("key must not null!");
jedisCluster.del(key);
LOGGER.debug("RedisUtil:delete cache key={}", key);
}
}
4. controller简单测试
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class RedisController {
@Autowired
private JedisTemplate jedisTemplate;
@RequestMapping("/testJedisCluster")
public void testJedisCluster() {
String username = "username";
String value = jedisTemplate.get(RedisConstants.LUFFI_LIVE_PREFIX, username);
if (StringUtils.isBlank(value)) {
jedisTemplate.set(RedisConstants.LUFFI_LIVE_PREFIX, username, "lbl");
}
value = jedisTemplate.get(RedisConstants.LUFFI_LIVE_PREFIX, username);
System.out.println("value: " + value);
}
@RequestMapping("/deleteWithPrefix")
public void deleteWithPrefix() {
String username = "username";
jedisTemplate.deleteWithPrefix(RedisConstants.LUFFI_LIVE_PREFIX, username);
}
}
demo到此为止