/**
*
*/
package redis;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class RedisClient {
private Jedis jedis;// 非切片額客戶端鏈接
private JedisPool jedisPool;// 非切片鏈接池
private ShardedJedis shardedJedis;// 切片額客戶端鏈接
private ShardedJedisPool shardedJedisPool;// 切片鏈接池
public RedisClient() {
initialPool();
initialShardedPool();
shardedJedis = shardedJedisPool.getResource();
jedis = jedisPool.getResource();
}
/**
* 初始化非切片池
*/
private void initialPool() {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000l);
config.setTestOnBorrow(false);
jedisPool = new JedisPool(config, "127.0.0.1", 6379);
}
/**
* 初始化切片池
*/
private void initialShardedPool() {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000l);
config.setTestOnBorrow(false);
// slave連接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("127.0.0.1", 6379, "master"));
// 構造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}
public void stringTest(){
Long l = jedis.getDB();
System.out.println(l);
jedis.set("strKey", "adfadfadsf");
System.out.println(jedis.get("strKey"));
}
public void KeysOpt(){
Set<String> key = jedis.keys("*");
Iterator<String> it = key.iterator();
System.out.println("----------key -----------");
while (it.hasNext()) {
String key1 = it.next();
String type = jedis.type(key1);
System.out.print("key = "+key1 +" type = "+type +" ");
if("string".equals(type)){
showStringValue(key1);
}else if("list".equals(type)){
showListValue(key1);
}else if("set".equals(type)){
showSetValue(key1);
}else if("zset".equals(type)){
showZSetValue(key1);
}else if("hash".equals(type)){
showHashValue(key1);
}
System.out.println();
}
}
private void showZSetValue(String key) {
System.out.print("value = "+jedis.zcard(key));
}
private void showSetValue(String key){
System.out.print("value = "+jedis.smembers(key));
}
private void showHashValue(String key){
System.out.print("value = "+jedis.hgetAll(key));
}
private void showListValue(String key){
System.out.print("value = "+jedis.lpop(key));
}
private void showStringValue(String key){
System.out.print("value = "+jedis.get(key));
}
public void Optation(){
jedis.flushDB();
//鍵操做
KeysOpt();
//字符串操做
stringOpt();
stringOpt1();
//列表操做
listOpt();
listOpt1();
//集合
setOpt();
setOpt1();
//hash
hashOpt();
hashOpt1();
KeysOpt();
}
private void hashOpt1() {
System.out.println("----------------shardedJedis set --------------");
shardedJedis.hset("hash","hashKey1","hashValue1");
shardedJedis.hset("hash","hashKey2","hashValue2");
shardedJedis.hset("hash","hashKey3","hashValue3");
shardedJedis.hset("hash","hashKey4","hashValue4");
System.out.println("刪除hashKey1 = "+shardedJedis.hdel("hash", "hashKey1"));
jedis.hset("hash", "hashKey2", "hashValue222222222222");
System.out.println("查詢單個 hashKey2="+shardedJedis.hget("hash", "hashKey2"));
System.out.println("查詢多個 hash key"+shardedJedis.hmget("hash", "hashKey2","hashKey3","hashKey4"));
ScanResult<Entry<String, String>> scanResult = shardedJedis.hscan("hash", 2);
System.out.println("獲取多個value "+scanResult.getResult());
}
private void hashOpt() {
System.out.println("----------------jedis set --------------");
jedis.hset("hash","hashKey1","hashValue1");
jedis.hset("hash","hashKey2","hashValue2");
jedis.hset("hash","hashKey3","hashValue3");
jedis.hset("hash","hashKey4","hashValue4");
System.out.println("刪除hashKey1 = "+jedis.hdel("hash", "hashKey1"));
jedis.hset("hash", "hashKey2", "hashValue222222222222");
System.out.println("查詢單個 hashKey2="+jedis.hget("hash", "hashKey2"));
System.out.println("查詢多個 hash key"+jedis.hmget("hash", "hashKey2","hashKey3","hashKey4"));
ScanResult<Entry<String, String>> scanResult = jedis.hscan("hash", 2);
System.out.println("獲取多個value "+scanResult.getResult());
}
private void setOpt1() {
System.out.println("----------------shardedJedis set --------------");
shardedJedis.sadd("set1", "sValue1");
shardedJedis.sadd("set2", "sValue2");
shardedJedis.sadd("set3", "sValue3");
shardedJedis.sadd("set4", "sValue4");
shardedJedis.sadd("set4", "sValue4444");
shardedJedis.sadd("set4", "sValue555");
shardedJedis.sadd("set5", "sValue5");
shardedJedis.sadd("set5", "sValue555");
shardedJedis.sadd("set5", "sValue5555555");
System.out.println("刪除 " +shardedJedis.del("set1"));
System.out.println("刪除" + shardedJedis.srem("set5", "sValue5"));
shardedJedis.sadd("set5", "sValue555555555555");
System.out.println("查詢"+shardedJedis.smembers("set5"));
System.out.println("值的個數:"+shardedJedis.scard("set5"));
System.out.println("鍵值的值判斷:"+shardedJedis.sismember("set5","sValue5555555"));
}
private void setOpt() {
System.out.println("----------------無序 jedis set --------------");
jedis.sadd("set1", "sValue1");
jedis.sadd("set2", "sValue2");
jedis.sadd("set3", "sValue3");
jedis.sadd("set4", "sValue4");
jedis.sadd("set4", "sValue4444");
jedis.sadd("set4", "sValue555");
jedis.sadd("set5", "sValue5");
jedis.sadd("set5", "sValue555");
jedis.sadd("set5", "sValue5555555");
System.out.println("刪除 " +jedis.del("set1"));
System.out.println("刪除" + jedis.srem("set5", "sValue5"));
jedis.sadd("set5", "sValue555555555555");
System.out.println("查詢"+jedis.smembers("set5"));
System.out.println("值的個數:"+jedis.scard("set5"));
System.out.println("鍵值的值判斷:"+jedis.sismember("set5","sValue5555555"));
System.out.println("並集 "+jedis.sunion("set4","set5"));
System.out.println("交集 "+jedis.sinter("set4","set5"));
System.out.println("差集 "+jedis.sdiff("set4","set5"));
System.out.println("並集 "+jedis.sunion("set5","set4"));
System.out.println("交集 "+jedis.sinter("set5","set4"));
System.out.println("差集 "+jedis.sdiff("set5","set4"));
System.out.println("---------------- 有序 jedis set --------------");
jedis.zadd("zset1",9,"sValue1");
jedis.zadd("zset2", 8,"sValue2");
jedis.zadd("zset3", 7,"sValue3");
jedis.zadd("zset4", 6,"sValue4");
jedis.zadd("zset4", 5,"sValue4444");
jedis.zadd("zset4", 4,"sValue555");
jedis.zadd("zset5", 3,"sValue5");
jedis.zadd("zset5", 2,"sValue555");
jedis.zadd("zset5", 1,"sValue5555555");
System.out.println("刪除 " +jedis.del("zset1"));
jedis.zadd("zset5",10, "sValue555555555555");
System.out.println("值的個數:"+jedis.zcard("zset5"));
System.out.println(jedis.zscore("zset5",""));
}
private void listOpt() {
System.out.println("----------------jedis list --------------");
jedis.lpush("list1", "lValue1");
jedis.lpush("list2", "lValue2");
jedis.lpush("list3", "lValue3");
jedis.lpush("list4", "lValue4");
jedis.lpush("list5", "lValue5");
jedis.lpush("list5", "lValue555");
jedis.lpush("list5", "lValue555555");
jedis.lpush("list5", "lValue5555555555");
System.out.println("刪除鍵 list1" + jedis.del("list1"));
System.out.println("刪除區間的值:"+jedis.ltrim("list5", 0, 2));
jedis.lpush("list2", "lValue2222");
System.out.println("查詢list2鍵的值 "+jedis.lpop("list2"));
System.out.println("鍵值的個數:"+jedis.llen("list5"));
System.out.println("-----------範圍取值----------");
List<String> list = jedis.lrange("list5", 0, -1);
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
private void listOpt1() {
System.out.println("----------------shardedJedis list --------------");
shardedJedis.lpush("list1", "lValue1");
shardedJedis.lpush("list2", "lValue2");
shardedJedis.lpush("list3", "lValue3");
shardedJedis.lpush("list4", "lValue4");
shardedJedis.lpush("list5", "lValue5");
shardedJedis.lpush("list5", "lValue555");
shardedJedis.lpush("list5", "lValue555555");
shardedJedis.lpush("list5", "lValue5555555555");
System.out.println("刪除鍵 list1" + shardedJedis.del("list1"));
System.out.println("刪除區間的值:"+shardedJedis.ltrim("list5", 0, 2));
shardedJedis.lpush("list2", "lValue2222");
System.out.println("查詢list2鍵的值 "+shardedJedis.lpop("list2"));
System.out.println("鍵值的個數:"+shardedJedis.llen("list5"));
System.out.println("-----------範圍取值----------");
List<String> list = shardedJedis.lrange("list5", 0, -1);
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
private void stringOpt() {
System.out.println("--------------- jedis string ---------------");
jedis.set("key1", "value1");
jedis.set("key2", "value2");
jedis.set("key3", "value3");
jedis.set("key4", "value4");
System.out.println("獲取指定鍵key1 的值:"+jedis.get("key1"));
System.out.println("獲取指定鍵key2 的值:"+jedis.get("key2"));
System.out.println("刪除key1的值");
System.out.println("---------刪除key內容-------");
jedis.del("key1");
System.out.println("獲取指定鍵的值:"+jedis.get("key1"));
System.out.println("---------追加內容-------");
jedis.append("key2", "value222");
System.out.println("獲取指定鍵key2 的值:"+jedis.get("key2"));
System.out.println("---------覆蓋內容-------");
jedis.set("key2", "value222");
System.out.println("獲取指定鍵key2 的值:"+jedis.get("key2"));
System.out.println("---------合併內容-------");
System.out.println(jedis.mset("key2","key3","key4","adfad"));
System.out.println(jedis.mget("key2","key4"));
System.out.println("鍵是否存在"+jedis.exists("key2"));
System.out.println("鍵是否存在"+jedis.exists("key1"));
}
private void stringOpt1() {
System.out.println("--------------- shardedJedis string ---------------");
shardedJedis.set("key1", "value1");
shardedJedis.set("key2", "value2");
shardedJedis.set("key3", "value3");
shardedJedis.set("key4", "value4");
System.out.println("獲取指定鍵key1 的值:"+shardedJedis.get("key1"));
System.out.println("獲取指定鍵key2 的值:"+shardedJedis.get("key2"));
System.out.println("刪除key1的值");
System.out.println("---------刪除key內容-------");
shardedJedis.del("key1");
System.out.println("獲取指定鍵的值:"+jedis.get("key1"));
System.out.println("---------追加內容-------");
jedis.append("key2", "value222");
System.out.println("獲取指定鍵key2 的值:"+shardedJedis.get("key2"));
System.out.println("---------覆蓋內容-------");
jedis.set("key2", "value222");
System.out.println("獲取指定鍵key2 的值:"+shardedJedis.get("key2"));
System.out.println("鍵是否存在"+shardedJedis.exists("key2"));
System.out.println("鍵是否存在"+shardedJedis.exists("key1"));
}
public static void main(String[] args) {
new RedisClient().Optation();
}
}
java