redis学习笔记(二)JedisCluster + redis 3.2.5集群


redis系列文章目录

  • Redis 利用Hash存储节约内存
  • 使用spring-data-redis实现incr自增

  • Redis学习笔记(九)redis实现时时直播列表缓存,支持分页[热点数据存储]
  • Redis学习笔记(八)redis之lua脚本学习
  • Redis学习笔记(七)jedis超时重试机制注意事项
  • Redis学习笔记(六)redis实现分布式锁
  • Redis学习笔记(五)jedis(JedisCluster)操作Redis集群 redis-cluster
  • redis学习笔记(四)缓存与数据库一致性问题
  • redis学习笔记(三)数据淘汰策略
  • redis学习笔记(二)JedisCluster + redis 3.2.5集群
  • redis学习笔记(一)redis3.2.5集群安装与测试

开发环境

  • jedis2.9.0
  • redis3.2.5
  • springboot 1.3.6.RELEASE

注意事项:本文搭建的redis配置了密码,所以jedis客户端要使用比较新的版本,老版本不支持密码,需要修改源码比较麻烦

环境搭建

  1. 引入jedis
     
        
    redis.clients
    jedis
    2.9.0
  2. 注入JedisClusterbean
 
  
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<HostAndPort> 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;
}
}
  1. 创建JedisTemplate模板
    ```java
    package com.le.luffi.media.redis;

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简单测试
```java
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到此为止

你可能感兴趣的:(redis,redis学习教程)