项目中会常用到redis,但JedisCluster的使用api还是比较多,经常可能会记不太清楚,故这里将大部分JedisCluster的api贴出来,供大家参考。
一、redis在工作是一个常见的工具,这里对redis和springboot形成集群的使用。
(1)引入对应redis集群所需要maven文件
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
(2) 在配置文件配置 redis.properties 配置redis集群必要参数
#客户端超时时间单位是毫秒 默认是2000
redis.timeout=10000
#最大空闲数
redis.maxIdle=300
#连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal
#redis.maxActive=600
#控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性
redis.maxTotal=2000
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWaitMillis=1000
redis.nodes=192.168.25.128:7000,192.168.25.128:7001,192.168.25.128:7002,192.168.25.128:7003,192.168.25.128:7004,192.168.25.128:7005,192.168.25.128:7006,192.168.25.128:7007
(3)初始化JedisCluster
@Configuration
@PropertySource("classpath:conf/redis.properties")
public class RedisConfig {
@Value("${redis.maxIdle}")
private Integer maxIdle;
@Value("${redis.timeout}")
private Integer timeout;
@Value("${redis.maxTotal}")
private Integer maxTotal;
@Value("${redis.maxWaitMillis}")
private Integer maxWaitMillis;
@Value("${redis.nodes}")
private String clusterNodes;
@Bean
public JedisCluster getJedisCluster(){
String[] cNodes = clusterNodes.split(",");
HashSet<HostAndPort> nodes = new HashSet<>();
//分割集群节点
for (String node : cNodes) {
String[] hp = node.split(":");
nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
}
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setMaxTotal(maxTotal);
//创建集群对象
JedisCluster jedisCluster = new JedisCluster(nodes, timeout, jedisPoolConfig);
return jedisCluster;
}
}
(4)引入JedisCluster进行api测试
01、测试整数和浮点数
@Autowired
JedisCluster jedisCluster;
/***
* 整数和浮点数
*/
@Test
public void testNumber(){
jedisCluster.set("numkey1", "1");
jedisCluster.set("numkey2", "2");
jedisCluster.set("numkey3", "0.56");
System.out.println("获取numkey1的值:" + jedisCluster.get("numkey1"));
System.out.println("让numkey1的值加1:" + jedisCluster.incr("numkey1"));
System.out.println("获取numkey1的值:" + jedisCluster.get("numkey1"));
System.out.println("让numkey1的值减1:" + jedisCluster.decr("numkey1"));
System.out.println("让numkey1的值加5:" + jedisCluster.incrBy("numkey1", 5));
System.out.println("获取numkey1的值:" + jedisCluster.get("numkey1"));
System.out.println("获取numkey1的值减4:" + jedisCluster.decrBy("numkey1", 4));
System.out.println("获取numkey1的值:" + jedisCluster.get("numkey1"));
System.out.println("-------------------------------------");
System.out.println("获取numkey3的值:" + jedisCluster.get("numkey3"));
//小数无法进行加减法运算
}
测试参数的值
获取numkey1的值:1
让numkey1的值加1:2
获取numkey1的值:2
让numkey1的值减1:1
让numkey1的值加5:6
获取numkey1的值:6
获取numkey1的值减4:2
获取numkey1的值:2
-------------------------------------
获取numkey3的值:0.56
02、测试对字符串进行hash操作
由于mset设置一批的值,由于jedis集群获取值通过key配置slot的地址,故这里将mset批量设置的key的前缀{aaa}
public static int getSlot(String key) {
int s = key.indexOf("{");
if (s > -1) {
int e = key.indexOf("}", s + 1);
if (e > -1 && e != s + 1) {
key = key.substring(s + 1, e);
}
}
// optimization with modulo operator with power of 2
// equivalent to getCRC16(key) % 16384
return getCRC16(key) & (16384 - 1);
}
集群对字符串的操作
@Autowired
JedisCluster jedisCluster;
@Test
public void testString() throws InterruptedException {
System.out.println("对keyString1设置字符串:"+jedisCluster.set("keyString1", "valueString1"));
System.out.println("获取keyString1设置的值:"+jedisCluster.get("keyString1"));
System.out.println("对keyString1的值增加值:"+jedisCluster.append("keyString1","appendValue"));
System.out.println("获取keyString1设置的值:"+jedisCluster.get("keyString1"));
System.out.println("删除keyString1实在的值:"+jedisCluster.del("keyString1"));
System.out.println("获取keyString1设置的值:"+jedisCluster.get("keyString1"));
System.out.println("----------------------------对多个键值对的操作----------------------");
/**
* 因此,暂且不能在RedisCluster模式下,通过增加中间层来对批量请求进行分组,并处理到对应的slot中,理想很好,但是不能够实现,因为服务端会进行一定的限制。
*
* 只能通过HASH_TAG来实现cluster模式下的mget/mset批量操作,我们可以在命令行中通过cluster keyslot ${key}来查看某个key对应的slot,可以从Jedis客户端的源码查看对应的key slot算法:
*/
System.out.println("设置多个键值对:"+jedisCluster.mset("{aaa}keyString01",
"valueString01","{aaa}keyString02","valueString02","{aaa}keyString03","valueString03"));
System.out.println("获取多个键值对:"+jedisCluster.mget("{aaa}keyString1","{aaa}keyString01","{aaa}keyString02","{aaa}keyString03"));
System.out.println("设删除多个键值对:"+jedisCluster.del(new String[]{"{aaa}keyString01","{aaa}keyString02"}));
System.out.println("获取多个键值对:"+jedisCluster.mget("{aaa}keyString01","{aaa}keyString02","{aaa}keyString03"));
/**
* jedisCluster.set(prefix + key, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, tt1);
*
*
* EX seconds : 将键的过期时间设置为 seconds 秒。 执行 SET key value EX seconds 的效果等同于执行 SETEX key seconds value 。
* PX milliseconds : 将键的过期时间设置为 milliseconds 毫秒。 执行 SET key value PX milliseconds 的效果等同于执行 PSETEX key milliseconds value 。
* NX : 只在键不存在时, 才对键进行设置操作。 执行 SET key value NX 的效果等同于执行 SETNX key value 。
* XX : 只在键已经存在时, 才对键进行设置操作。
*/
System.out.println("-----------------------set命令----------------------------------");
System.out.println("获取设置nx,px1:" + jedisCluster.set("setKey", "setValue1", "NX", "PX", 10000));
TimeUnit.SECONDS.sleep(1);
System.out.println("获取setKey保留时间1:" + jedisCluster.ttl("setKey"));
TimeUnit.SECONDS.sleep(1);
System.out.println("获取setKey保留时间2:" + jedisCluster.ttl("setKey"));
System.out.println("获取设置nx,px2:" + jedisCluster.set("setKey", "setValue2", "NX", "PX", 10000));
System.out.println("获取setKey的value值2:"+jedisCluster.get("setKey"));
System.out.println("设置nx,xx1:" + jedisCluster.set("setKey", "setValue3", "NX", "PX", 10000));
System.out.println("获取setKey的value值3:"+jedisCluster.get("setKey"));
System.out.println("---------------------新增键值对防止覆盖原先值----------------");
System.out.println("key1 setnx:"+jedisCluster.setnx("key1", "value1"));
System.out.println("key2 setnx: "+jedisCluster.setnx("key2", "value2"));
System.out.println("key2 setnx: "+jedisCluster.setnx("key2", "value2-new"));
System.out.println("获取 key1的值:"+jedisCluster.get("key1"));
System.out.println("获取 key2的值"+jedisCluster.get("key2"));
System.out.println("------------新增键值对并设置有效时间------------");
System.out.println("set keyex:"+jedisCluster.setex("key3", 2, "value3"));
System.out.println("key3 :"+jedisCluster.get("key3"));
TimeUnit.SECONDS.sleep(3);
System.out.println("key3 休息3s:"+jedisCluster.get("key3"));
//GETSET is an atomic set this value and return the old value command.
System.out.println("------------------获取原值,更新为新值-----------");
System.out.println("getSet 值"+jedisCluster.getSet("key2", "key2GetSet"));
System.out.println("获取key2::"+jedisCluster.get("key2"));
System.out.println("获得key2的值的字串:"+jedisCluster.getrange("key2", 2, 4));
}
操作的字符串的的值
对keyString1设置字符串:OK
获取keyString1设置的值:valueString1
对keyString1的值增加值:23
获取keyString1设置的值:valueString1appendValue
删除keyString1实在的值:1
获取keyString1设置的值:null
----------------------------对多个键值对的操作----------------------
设置多个键值对:OK
获取多个键值对:[null, valueString01, valueString02, valueString03]
设删除多个键值对:2
获取多个键值对:[null, null, valueString03]
-----------------------set命令----------------------------------
获取设置nx,px1:OK
获取setKey保留时间1:9
获取setKey保留时间2:8
获取设置nx,px2:null
获取setKey的value值2:setValue1
设置nx,xx1:null
获取setKey的value值3:setValue1
---------------------新增键值对防止覆盖原先值----------------
key1 setnx:0
key2 setnx: 0
key2 setnx: 0
获取 key1的值:value1
获取 key2的值key2GetSet
------------新增键值对并设置有效时间------------
set keyex:OK
key3 :value3
key3 休息3s:null
------------------获取原值,更新为新值-----------
getSet 值key2GetSet
获取key2::key2GetSet
获得key2的值的字串:y2G
03、集群对有序集合api的操作
/**
* 有序集合
*/
@Test
public void testSortedSet(){
System.out.println("设置zset集合:"+jedisCluster.zadd("zset", 3,"key01"));
System.out.println("设置zset集合:"+jedisCluster.zadd("zset", 1,"key02"));
System.out.println("zset中的所有元素:"+jedisCluster.zrangeWithScores("zset", 0, -1));
System.out.println("zset中的score中元素2-4:"+jedisCluster.zrangeWithScores("zset", 2, 4));
Map<String,Double> map = new HashMap<String,Double>();
map.put("key1",1.0);
map.put("key2",1.2);
map.put("key3",4.0);
map.put("key4",5.0);
map.put("key5",0.2);
System.out.println("zet 添加map"+jedisCluster.zadd("zset",map));
System.out.println("zset中的所有元素(zrangeWithScores):"+jedisCluster.zrangeWithScores("zset", 0, -1));
System.out.println("-------------------------------------------------------");
System.out.println("zset中的所有zrangeByScore元素(1,3):"+jedisCluster.zrangeByScore("zset", 1,3));
System.out.println("zset中的所有zrangeByScoreWithScores元素(1,3):"+jedisCluster.zrangeByScoreWithScores("zset", 1,3));
System.out.println("zset中key2的分值:"+jedisCluster.zscore("zset", "key2"));
System.out.println("zset中key2的排名:"+jedisCluster.zrank("zset", "key2"));
System.out.println("删除zset中的元素key3:"+jedisCluster.zrem("zset", "key3"));
System.out.println("zset中的所有元素:"+jedisCluster.zrange("zset", 0, -1));
System.out.println("zset中元素的个数:"+jedisCluster.zcard("zset"));
System.out.println("zset中分值在1-4之间的元素的个数:"+jedisCluster.zcount("zset", 1, 4));
System.out.println("key2的分值加上5:"+jedisCluster.zincrby("zset", 5, "key2"));
System.out.println("key3的分值加上4:"+jedisCluster.zincrby("zset", 4, "key3"));
System.out.println("zset中的所有元素:"+jedisCluster.zrange("zset", 0, -1));
}
对有序集合的操作获取的值
设置zset集合:1
设置zset集合:1
zset中的所有元素:[[[107, 101, 121, 48, 50],1.0], [[107, 101, 121, 48, 49],3.0]]
zset中的score中元素2-4:[]
zet 添加map5
zset中的所有元素(zrangeWithScores):[[[107, 101, 121, 53],0.2], [[107, 101, 121, 48, 50],1.0], [[107, 101, 121, 49],1.0], [[107, 101, 121, 50],1.2], [[107, 101, 121, 48, 49],3.0], [[107, 101, 121, 51],4.0], [[107, 101, 121, 52],5.0]]
-------------------------------------------------------
zset中的所有zrangeByScore元素(1,3):[key02, key1, key2, key01]
zset中的所有zrangeByScoreWithScores元素(1,3):[[[107, 101, 121, 48, 50],1.0], [[107, 101, 121, 49],1.0], [[107, 101, 121, 50],1.2], [[107, 101, 121, 48, 49],3.0]]
zset中key2的分值:1.2
zset中key2的排名:3
删除zset中的元素key3:1
zset中的所有元素:[key5, key02, key1, key2, key01, key4]
zset中元素的个数:6
zset中分值在1-4之间的元素的个数:4
key2的分值加上5:6.2
key3的分值加上4:4.0
zset中的所有元素:[key5, key02, key1, key01, key3, key4, key2]
04、对hash操作的操作
/**
* test Hash
*/
@Test
public void testHash(){
Map<String,String> map = new HashMap<String,String>();
map.put("hashkey1","hashvalue1");
map.put("hashkey2","hashvalue2");
map.put("hashkey3","hashvalue3");
map.put("hashkey4","hashvalue4");
jedisCluster.hmset("hash",map);
jedisCluster.hset("hash", "hashkey5", "hashvalue5");
System.out.println("散列hash的所有键值对为:"+jedisCluster.hgetAll("hash"));
System.out.println("散列hash的所有键为:"+jedisCluster.hkeys("hash"));
System.out.println("散列hash的所有值为:"+jedisCluster.hvals("hash"));
System.out.println("将key6保存的值加上一个整数,如果hashkey6不存在则添加hashkey6:"+jedisCluster.hincrBy("hash", "hashkey6", 6));
System.out.println("散列hash的所有键值对为:"+jedisCluster.hgetAll("hash"));
System.out.println("将key6保存的值加上一个整数,如果hashkey6不存在则添加hashkey6:"+jedisCluster.hincrBy("hash", "hashkey6", 3));
System.out.println("散列hash的所有键值对为:"+jedisCluster.hgetAll("hash"));
System.out.println("删除一个或者多个键值对:"+jedisCluster.hdel("hash", "hashkey2"));
System.out.println("散列hash的所有键值对为:"+jedisCluster.hgetAll("hash"));
System.out.println("散列hash中键值对的个数:"+jedisCluster.hlen("hash"));
System.out.println("判断hash中是否存在hashkey2:"+jedisCluster.hexists("hash","hashkey2"));
System.out.println("判断hash中是否存在hashkey3:"+jedisCluster.hexists("hash","hashkey3"));
System.out.println("获取hash中的值:"+jedisCluster.hmget("hash","hashkey3"));
System.out.println("获取hash中的值:"+jedisCluster.hmget("hash","hashkey3","hashkey4"));
}
对hash操作的获取的值
散列hash的所有键值对为:{hashkey5=hashvalue5, hashkey4=hashvalue4, hashkey3=hashvalue3, hashkey2=hashvalue2, hashkey1=hashvalue1}
散列hash的所有键为:[hashkey5, hashkey4, hashkey3, hashkey2, hashkey1]
散列hash的所有值为:[hashvalue3, hashvalue2, hashvalue1, hashvalue4, hashvalue5]
将key6保存的值加上一个整数,如果hashkey6不存在则添加hashkey6:6
散列hash的所有键值对为:{hashkey6=6, hashkey5=hashvalue5, hashkey4=hashvalue4, hashkey3=hashvalue3, hashkey2=hashvalue2, hashkey1=hashvalue1}
将key6保存的值加上一个整数,如果hashkey6不存在则添加hashkey6:9
散列hash的所有键值对为:{hashkey6=9, hashkey5=hashvalue5, hashkey4=hashvalue4, hashkey3=hashvalue3, hashkey2=hashvalue2, hashkey1=hashvalue1}
删除一个或者多个键值对:1
散列hash的所有键值对为:{hashkey6=9, hashkey5=hashvalue5, hashkey4=hashvalue4, hashkey3=hashvalue3, hashkey1=hashvalue1}
散列hash中键值对的个数:5
判断hash中是否存在hashkey2:false
判断hash中是否存在hashkey3:true
获取hash中的值:[hashvalue3]
获取hash中的值:[hashvalue3, hashvalue4]
05、对排序的操作
/**
* 排序
*/
@Test
public void testSort(){
System.out.println("collections的push:"+jedisCluster.lpush("collectionskey", "collectionsvalue01", "collectionsvalue02", "collectionsvalue03"));
System.out.println("collections的内容:"+jedisCluster.lrange("collectionskey", 0, -1));
SortingParams sortingParameters = new SortingParams();
// 当数据集中保存的是字符串值时,你可以用 ALPHA,默认是升序
System.out.println("alpha排序方式:" + jedisCluster.sort("collectionskey",sortingParameters.alpha()));
System.out.println("---------------------------");
jedisCluster.lpush("sortedList", "3","1","2","8","7","4");
System.out.println("sortedList排序前:"+jedisCluster.lrange("sortedList", 0, -1));
System.out.println("升序:"+jedisCluster.sort("sortedList", sortingParameters.asc()));
System.out.println("降序:"+jedisCluster.sort("sortedList", sortingParameters.desc()));
}
对排序的操作获取的值
collections的push:3
collections的内容:[collectionsvalue03, collectionsvalue02, collectionsvalue01]
alpha排序方式:[collectionsvalue01, collectionsvalue02, collectionsvalue03]
---------------------------
sortedList排序前:[4, 7, 8, 2, 1, 3]
升序:[1, 2, 3, 4, 7, 8]
降序:[8, 7, 4, 3, 2, 1]
来源:CSDN
作者:筏镜
链接:https://blog.csdn.net/fajing_feiyue/article/details/104088373