Redis Jedis、jedisCluster的使用
1、Jedis客户端
https://redis.io/clients
2、Jedis源码包与使用介绍
https://github.com/xetorthio/jedis
3、项目中使用
通过maven引入Jedis的依赖包
复制代码
复制代码
4、Jedis的使用
创建Jedis对象,set方法存储key-value值,get方法获取key对应的值。主要是用于单个redis。
Jedis jedis = new Jedis(“192.168.56.180”, 6379);
jedis.set(“Jedis”, “Hello Work!”);
System.out.println(jedis.get(“Jedis”));
jedis.close();
这里是使用IDEA编辑器对Jedis类结构的一个关系图,从图中看出Jedis继承BinaryJedis,实现多个接口。
每一个接口都代表了一类Redis命令,例如JedisCommands中包含了SET GET等命令,MultiKeyCommands中包含了针对多个Key的MSET MGET等命令。
5、JedisCluster的使用
Jediscluster类图和jedis类图大致一样,不过jedisCluster有一些命令是不可用的。比如BinaryJedisCluster类被作废的命令。主要是用在集群中。
类图:
6、单个reids中API的测试
复制代码
package com.redis;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.SortingParams;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
单机环境Redis操作:一台Redis服务器
*/
public class Standalone {
private static Jedis jedis;
static {
jedis = new Jedis(“192.168.56.180”, 6379);
jedis.auth(“123456”); // 之前我在redis配置中配置了权限密码,这里需要设置
}
/**
/***
字符串操作
memcached和redis同样有append的操作,但是memcached有prepend的操作,redis中并没有。
@throws InterruptedException
*/
@Test
public void testString() throws InterruptedException {
jedis.flushDB();
System.out.println("=增加数据=");
System.out.println(jedis.set(“key1”,“value1”));
System.out.println(jedis.set(“key2”,“value2”));
System.out.println(jedis.set(“key3”, “value3”));
System.out.println(“删除键key2:”+jedis.del(“key2”));
System.out.println(“获取键key2:”+jedis.get(“key2”));
System.out.println(“修改key1:”+jedis.set(“key1”, “value1Changed”));
System.out.println(“获取key1的值:”+jedis.get(“key1”));
System.out.println(“在key3后面加入值:”+jedis.append(“key3”, “End”));
System.out.println(“key3的值:”+jedis.get(“key3”));
System.out.println(“增加多个键值对:”+jedis.mset(“key01”,“value01”,“key02”,“value02”,“key03”,“value03”));
System.out.println(“获取多个键值对:”+jedis.mget(“key01”,“key02”,“key03”));
System.out.println(“获取多个键值对:”+jedis.mget(“key01”,“key02”,“key03”,“key04”));
System.out.println(“删除多个键值对:”+jedis.del(new String[]{“key01”,“key02”}));
System.out.println(“获取多个键值对:”+jedis.mget(“key01”,“key02”,“key03”));
jedis.flushDB();
System.out.println("=新增键值对防止覆盖原先值====");
System.out.println(jedis.setnx(“key1”, “value1”));
System.out.println(jedis.setnx(“key2”, “value2”));
System.out.println(jedis.setnx(“key2”, “value2-new”));
System.out.println(jedis.get(“key1”));
System.out.println(jedis.get(“key2”));
System.out.println("=新增键值对并设置有效时间===");
System.out.println(jedis.setex(“key3”, 2, “value3”));
System.out.println(jedis.get(“key3”));
TimeUnit.SECONDS.sleep(3);
System.out.println(jedis.get(“key3”));
System.out.println("=获取原值,更新为新值");//GETSET is an atomic set this value and return the old value command.
System.out.println(jedis.getSet(“key2”, “key2GetSet”));
System.out.println(jedis.get(“key2”));
System.out.println(“获得key2的值的字串:”+jedis.getrange(“key2”, 2, 4));
}
/***
/***
/***
/***
/**
/**
复制代码
7、集群reids中API的测试
复制代码
package com.redis;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.SortingParams;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
集群环境下Jedis操作
*/
public class Cluster {
private static JedisCluster jedis;
static {
// 添加集群的服务节点Set集合
Set hostAndPortsSet = new HashSet();
// 添加节点
hostAndPortsSet.add(new HostAndPort(“192.168.56.180”, 7777));
hostAndPortsSet.add(new HostAndPort(“192.168.56.180”, 8888));
hostAndPortsSet.add(new HostAndPort(“192.168.56.181”, 7777));
hostAndPortsSet.add(new HostAndPort(“192.168.56.181”, 8888));
hostAndPortsSet.add(new HostAndPort(“192.168.56.182”, 7777));
hostAndPortsSet.add(new HostAndPort(“192.168.56.182”, 8888));
// Jedis连接池配置
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大空闲连接数, 默认8个
jedisPoolConfig.setMaxIdle(100);
// 最大连接数, 默认8个
jedisPoolConfig.setMaxTotal(500);
//最小空闲连接数, 默认0
jedisPoolConfig.setMinIdle(0);
// 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒
//对拿到的connection进行validateObject校验
jedisPoolConfig.setTestOnBorrow(true);
jedis = new JedisCluster(hostAndPortsSet, jedisPoolConfig);
}
/**
/***
字符串操作
memcached和redis同样有append的操作,但是memcached有prepend的操作,redis中并没有。
集群中flushDB、keys、del(多个值)、mset(多个值)废弃
@throws InterruptedException
*/
@Test
public void testString() throws InterruptedException {
//jedis.flushDB();
System.out.println("=增加数据=");
System.out.println(jedis.set(“key1”,“value1”));
System.out.println(jedis.set(“key2”,“value2”));
System.out.println(jedis.set(“key3”, “value3”));
System.out.println(“删除键key2:”+jedis.del(“key2”));
System.out.println(“获取键key2:”+jedis.get(“key2”));
System.out.println(“修改key1:”+jedis.set(“key1”, “value1Changed”));
System.out.println(“获取key1的值:”+jedis.get(“key1”));
System.out.println(“在key3后面加入值:”+jedis.append(“key3”, “End”));
System.out.println(“key3的值:”+jedis.get(“key3”));
//命令的时候才会去连接连接,集群中连接是对一个节点连接,不能判断多个key经过crc16算法所对应的槽在一个节点上,不支持多key获取、删除
//System.out.println(“增加多个键值对:”+jedis.mset(“key01”,“value01”,“key02”,“value02”));
//System.out.println(“获取多个键值对:”+jedis.mget(“key01”,“key02”,“key03”));
//System.out.println(“获取多个键值对:”+jedis.mget(“key01”,“key02”,“key03”,“key04”));
//System.out.println(“删除多个键值对:”+jedis.del(new String[]{“key01”,“key02”}));
//System.out.println(“获取多个键值对:”+jedis.mget(“key01”,“key02”,“key03”));
//jedis.flushDB();
System.out.println("=新增键值对防止覆盖原先值====");
System.out.println(jedis.setnx(“key1”, “value1”));
System.out.println(jedis.setnx(“key2”, “value2”));
System.out.println(jedis.setnx(“key2”, “value2-new”));
System.out.println(jedis.get(“key1”));
System.out.println(jedis.get(“key2”));
System.out.println("=新增键值对并设置有效时间===");
System.out.println(jedis.setex(“key3”, 2, “value3”));
System.out.println(jedis.get(“key3”));
TimeUnit.SECONDS.sleep(3);
System.out.println(jedis.get(“key3”));
System.out.println("=获取原值,更新为新值");//GETSET is an atomic set this value and return the old value command.
System.out.println(jedis.getSet(“key2”, “key2GetSet”));
System.out.println(jedis.get(“key2”));
System.out.println(“获得key2的值的字串:”+jedis.getrange(“key2”, 2, 4)); // 相当截取字符串的第二个位置-第四个位置的字符串
}
/***
/***
/***
set集合
*/
@Test
public void testSet() {
System.out.println(“向集合中添加元素”);
System.out.println(jedis.sadd(“eleSet”, “e1”,“e2”,“e4”,“e3”,“e0”,“e8”,“e7”,“e5”));
System.out.println(jedis.sadd(“eleSet”, “e6”));
System.out.println(jedis.sadd(“eleSet”, “e6”)); // 返回0,集合中已经存在
System.out.println(“eleSet的所有元素为:”+jedis.smembers(“eleSet”));
System.out.println(“删除一个元素e0:”+jedis.srem(“eleSet”, “e0”));
System.out.println(“eleSet的所有元素为:”+jedis.smembers(“eleSet”));
System.out.println(“删除两个元素e7和e6:”+jedis.srem(“eleSet”, “e7”,“e6”));
System.out.println(“eleSet的所有元素为:”+jedis.smembers(“eleSet”));
System.out.println(“随机的移除集合中的一个元素:”+jedis.spop(“eleSet”));
System.out.println(“随机的移除集合中的一个元素:”+jedis.spop(“eleSet”));
System.out.println(“eleSet的所有元素为:”+jedis.smembers(“eleSet”));
System.out.println(“eleSet中包含元素的个数:”+jedis.scard(“eleSet”));
System.out.println(“e3是否在eleSet中:”+jedis.sismember(“eleSet”, “e3”));
System.out.println(“e1是否在eleSet中:”+jedis.sismember(“eleSet”, “e1”));
System.out.println(“e5是否在eleSet中:”+jedis.sismember(“eleSet”, “e5”));
// 集群下并存会报错:redis.clients.jedis.exceptions.JedisClusterException: No way to dispatch this command to Redis Cluster because keys have different slots.
// Redis集群,从key1集合与key2集合并存、交集、差集,两个键经过crc16算法可能有不同的槽。
/*System.out.println("=================================");
System.out.println(jedis.sadd(“eleSet1”, “e1”,“e2”,“e4”,“e3”,“e0”,“e8”,“e7”,“e5”));
System.out.println(jedis.sadd(“eleSet2”, “e1”,“e2”,“e4”,“e3”,“e0”,“e8”));
System.out.println(“将eleSet1中删除e1并存入eleSet3中:”+jedis.smove(“eleSet1”, “eleSet3”, “e1”));
System.out.println(“将eleSet1中删除e2并存入eleSet3中:”+jedis.smove(“eleSet1”, “eleSet3”, “e2”));
System.out.println(“eleSet1中的元素:”+jedis.smembers(“eleSet1”));
System.out.println(“eleSet3中的元素:”+jedis.smembers(“eleSet3”));*/
/*System.out.println(“集合运算=====”);
System.out.println(“eleSet1中的元素:”+jedis.smembers(“eleSet1”));
System.out.println(“eleSet2中的元素:”+jedis.smembers(“eleSet2”));
System.out.println(“eleSet1和eleSet2的交集:”+jedis.sinter(“eleSet1”,“eleSet2”));
System.out.println(“eleSet1和eleSet2的并集:”+jedis.sunion(“eleSet1”,“eleSet2”));
System.out.println(“eleSet1和eleSet2的差集:”+jedis.sdiff(“eleSet1”,“eleSet2”));*/
jedis.del(“eleSet”);
jedis.del(“eleSet1”);
jedis.del(“eleSet2”);
jedis.del(“eleSet3”);
}
/***
/**
/**