Jedis对Redis的常用命令操作
本篇主要总结一些Jedis对Redis的常用命令操作:
1.对key操作命令
2.对String操作命令
3.对List操作命令
4.对Set操作命令
5.对Hash操作命令
6.排序操作指令
一、项目中添加redis和junit的依赖。
1 <dependency> 2 <groupId>redis.clients</groupId> 3 <artifactId>jedis</artifactId> 4 <version>2.9.0</version> 5 </dependency> 6 <dependency> 7 <groupId>junit</groupId> 8 <artifactId>junit</artifactId> 9 </dependency>
二、创建JedisUtil工具类。
1 import redis.clients.jedis.Jedis; 2 import redis.clients.jedis.JedisPool; 3 import redis.clients.jedis.JedisPoolConfig; 4 5 import java.util.HashMap; 6 import java.util.Map; 7 8 /** 9 * @Author guoyuanhao 10 * @Date 2018/4/8 18:41 11 */ 12 public class JedisUtil { 13 14 private static JedisUtil jedisUtil; 15 16 private JedisUtil(){}; 17 18 public static JedisUtil getInstance(){ 19 if (jedisUtil == null){ 20 synchronized(JedisUtil.class){ 21 if (jedisUtil == null){ 22 jedisUtil = new JedisUtil(); 23 } 24 } 25 } 26 return jedisUtil; 27 } 28 29 //Jedis的连接池 30 private static Map<String,JedisPool> maps = new HashMap<String,JedisPool>(); 31 32 private static JedisPool getPool(String ip, int port){ 33 String key = ip+":"+port; 34 JedisPool pool = null; 35 if(!maps.containsKey(key)){ 36 JedisPoolConfig config = new JedisPoolConfig(); 37 config.setMaxIdle(RedisConfig.MAX_IDLE); 38 config.setMinIdle(RedisConfig.MIN_IDLE); 39 config.setMaxTotal(RedisConfig.MAX_TOTAL); 40 config.setMaxWaitMillis(RedisConfig.MAX_WAIT); 41 config.setTestOnBorrow(true); 42 config.setTestOnReturn(true); 43 44 pool = new JedisPool(config,ip,port,RedisConfig.TIMEOUT); 45 maps.put(key, pool); 46 }else{ 47 pool = maps.get(key); 48 } 49 return pool; 50 } 51 52 public Jedis getJedis(String ip, int port){ 53 Jedis jedis = null; 54 int count = 0; 55 do{ 56 try{ 57 jedis = getPool(ip,port).getResource(); 58 }catch (Exception e){ 59 getPool(ip,port).returnBrokenResource(jedis); 60 } 61 }while(jedis == null && count<RedisConfig.RETRY_NUM); 62 return jedis; 63 } 64 65 public void closeJedis(Jedis jedis, String ip, int port){ 66 if(jedis != null){ 67 getPool(ip,port).returnResource(jedis); 68 } 69 } 70 71 public static class RedisConfig { 72 //最大实例总数 73 public static int MAX_TOTAL = 150; 74 75 //最大空闲连接数, 默认8个 76 public static int MAX_IDLE = 100; 77 78 //最小空闲连接数, 默认8个 79 public static int MIN_IDLE = 10; 80 81 //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; 82 public static int MAX_WAIT = 10000; 83 84 public static int TIMEOUT = 10000; 85 86 public static int RETRY_NUM = 5; 87 88 public RedisConfig(){}; 89 } 90 }
三、用Juint单元测试命令操作
1.静态变量,ip,端口号。
1 private static final String ipAddr = "192.168.16.142"; 2 private static final int port = 6379; 3 private static Jedis jedis = null;
2.测试类方法执行之前从连接池中获取jedis对象,执行之后关闭jedis对象。
1 @Before 2 public void init() { jedis = JedisUtil.getInstance().getJedis(ipAddr, port); } 3 @After 4 public void close() { JedisUtil.getInstance().closeJedis(jedis,ipAddr, port); }
3.单元测试
3.1.对key操作的命令
1 @Test 2 public void testKey(){ 3 System.out.println("清空数据:"+jedis.flushDB()); 4 System.out.println("判断某个键是否存在:"+jedis.exists("username")); 5 System.out.println("新增<'username','zzh'>的键值对:"+jedis.set("username", "zzh")); 6 System.out.println("新增<'password','password'>的键值对:"+jedis.set("password", "password")); 7 System.out.print("系统中所有的键如下:"); 8 Set<String> keys = jedis.keys("*"); 9 System.out.println(keys); 10 System.out.println("删除键password:"+jedis.del("password")); 11 System.out.println("判断键password是否存在:"+jedis.exists("password")); 12 System.out.println("查看键username所存储的值的类型:"+jedis.type("username")); 13 System.out.println("随机返回key空间的一个:"+jedis.randomKey()); 14 System.out.println("重命名key:"+jedis.rename("username","name")); 15 System.out.println("取出改后的name:"+jedis.get("name")); 16 System.out.println("按索引查询:"+jedis.select(0)); 17 System.out.println("删除当前选择数据库中的所有key:"+jedis.flushDB()); 18 System.out.println("返回当前数据库中key的数目:"+jedis.dbSize()); 19 System.out.println("删除所有数据库中的所有key:"+jedis.flushAll()); 20 }
3.2.对String操作的命令
1 @Test 2 public void testString(){ 3 jedis.flushDB(); 4 System.out.println("===========增加数据==========="); 5 System.out.println(jedis.set("key1","value1")); 6 System.out.println(jedis.set("key2","value2")); 7 System.out.println(jedis.set("key3", "value3")); 8 System.out.println("删除键key2:"+jedis.del("key2")); 9 System.out.println("获取键key2:"+jedis.get("key2")); 10 System.out.println("修改key1:"+jedis.set("key1", "value1Changed")); 11 System.out.println("获取key1的值:"+jedis.get("key1")); 12 System.out.println("在key3后面加入值:"+jedis.append("key3", "End")); 13 System.out.println("key3的值:"+jedis.get("key3")); 14 System.out.println("增加多个键值对:"+jedis.mset("key01","value01","key02","value02","key03","value03")); 15 System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03")); 16 System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03","key04")); 17 System.out.println("删除多个键值对:"+jedis.del(new String[]{"key01","key02"})); 18 System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03")); 19 20 jedis.flushDB(); 21 System.out.println("===========新增键值对防止覆盖原先值=============="); 22 System.out.println(jedis.setnx("key1", "value1")); 23 System.out.println(jedis.setnx("key2", "value2")); 24 System.out.println(jedis.setnx("key2", "value2-new")); 25 System.out.println(jedis.get("key1")); 26 System.out.println(jedis.get("key2")); 27 28 System.out.println("===========新增键值对并设置有效时间============="); 29 System.out.println(jedis.setex("key3", 2, "value3")); 30 System.out.println(jedis.get("key3")); 31 try { 32 TimeUnit.SECONDS.sleep(3); 33 } catch (InterruptedException e) { 34 e.printStackTrace(); 35 } 36 System.out.println(jedis.get("key3")); 37 38 System.out.println("===========获取原值,更新为新值=========="); 39 System.out.println(jedis.getSet("key2", "key2GetSet")); 40 System.out.println(jedis.get("key2")); 41 42 System.out.println("获得key2的值的字串:"+jedis.getrange("key2", 2, 4)); 43 }
3.3.对List操作命令
1 @Test 2 public void testList(){ 3 jedis.flushDB(); 4 System.out.println("===========添加一个list==========="); 5 //jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap"); 6 jedis.lpush("collections", "HashSet"); 7 jedis.lpush("collections", "TreeSet"); 8 jedis.lpush("collections", "TreeMap"); 9 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));//-1代表倒数第一个元素,-2代表倒数第二个元素,end为-1表示查询全部 10 System.out.println("collections区间0-3的元素:"+jedis.lrange("collections",0,3)); 11 System.out.println("==============================="); 12 // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈 13 System.out.println("删除指定元素个数:"+jedis.lrem("collections", 2, "HashMap")); 14 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); 15 System.out.println("删除下表0-3区间之外的元素:"+jedis.ltrim("collections", 0, 3)); 16 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); 17 System.out.println("collections列表出栈(左端):"+jedis.lpop("collections")); 18 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); 19 System.out.println("collections添加元素,从列表右端,与lpush相对应:"+jedis.rpush("collections", "EnumMap")); 20 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); 21 System.out.println("collections列表出栈(右端):"+jedis.rpop("collections")); 22 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); 23 System.out.println("修改collections指定下标1的内容:"+jedis.lset("collections", 1, "LinkedArrayList")); 24 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); 25 System.out.println("==============================="); 26 System.out.println("collections的长度:"+jedis.llen("collections")); 27 System.out.println("获取collections下标为2的元素:"+jedis.lindex("collections", 2)); 28 System.out.println("==============================="); 29 //jedis.lpush("sortedList", "3","6","2","0","7","4"); 30 System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1)); 31 System.out.println(jedis.sort("sortedList")); 32 System.out.println("sortedList排序后:"+jedis.lrange("sortedList", 0, -1)); 33 }
3.4.对Set的操作命令
1 @Test 2 public void testSet(){ 3 jedis.flushDB(); 4 System.out.println("============向集合中添加元素(不重复)============"); 5 System.out.println(jedis.sadd("eleSet", "e1","e2","e4","e3","e0","e8","e7","e5")); 6 System.out.println(jedis.sadd("eleSet", "e6")); 7 System.out.println(jedis.sadd("eleSet", "e6")); 8 System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet")); 9 System.out.println("删除一个元素e0:"+jedis.srem("eleSet", "e0")); 10 System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet")); 11 System.out.println("删除两个元素e7和e6:"+jedis.srem("eleSet", "e7","e6")); 12 System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet")); 13 System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet")); 14 System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet")); 15 System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet")); 16 System.out.println("eleSet中包含元素的个数:"+jedis.scard("eleSet")); 17 System.out.println("e3是否在eleSet中:"+jedis.sismember("eleSet", "e3")); 18 System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e1")); 19 System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e5")); 20 System.out.println("================================="); 21 System.out.println(jedis.sadd("eleSet1", "e1","e2","e4","e3","e0","e8","e7","e5")); 22 System.out.println(jedis.sadd("eleSet2", "e1","e2","e4","e3","e0","e8")); 23 System.out.println("将eleSet1中删除e1并存入eleSet3中:"+jedis.smove("eleSet1", "eleSet3", "e1"));//移到集合元素 24 System.out.println("将eleSet1中删除e2并存入eleSet3中:"+jedis.smove("eleSet1", "eleSet3", "e2")); 25 System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1")); 26 System.out.println("eleSet3中的元素:"+jedis.smembers("eleSet3")); 27 System.out.println("============集合运算================="); 28 System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1")); 29 System.out.println("eleSet2中的元素:"+jedis.smembers("eleSet2")); 30 System.out.println("eleSet1和eleSet2的交集:"+jedis.sinter("eleSet1","eleSet2")); 31 System.out.println("eleSet1和eleSet2的并集:"+jedis.sunion("eleSet1","eleSet2")); 32 System.out.println("eleSet1和eleSet2的差集:"+jedis.sdiff("eleSet1","eleSet2"));//eleSet1中有,eleSet2中没有 33 jedis.sinterstore("eleSet4","eleSet1","eleSet2");//求交集并将交集保存到dstkey的集合 34 System.out.println("eleSet4中的元素:"+jedis.smembers("eleSet4")); 35 }
3.5.对Hash的操作命令
1 @Test 2 public void testHash(){ 3 jedis.flushDB(); 4 Map<String,String> map = new HashMap<>(); 5 map.put("key1","value1"); 6 map.put("key2","value2"); 7 map.put("key3","value3"); 8 map.put("key4","value4"); 9 //添加名称为hash(key)的hash元素 10 jedis.hmset("hash",map); 11 //向名称为hash的hash中添加key为key5,value为value5元素 12 jedis.hset("hash", "key5", "value5"); 13 System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));//return Map<String,String> 14 System.out.println("散列hash的所有键为:"+jedis.hkeys("hash"));//return Set<String> 15 System.out.println("散列hash的所有值为:"+jedis.hvals("hash"));//return List<String> 16 System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:"+jedis.hincrBy("hash", "key6", 6)); 17 System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); 18 System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:"+jedis.hincrBy("hash", "key6", 3)); 19 System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); 20 System.out.println("删除一个或者多个键值对:"+jedis.hdel("hash", "key2")); 21 System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); 22 System.out.println("散列hash中键值对的个数:"+jedis.hlen("hash")); 23 System.out.println("判断hash中是否存在key2:"+jedis.hexists("hash","key2")); 24 System.out.println("判断hash中是否存在key3:"+jedis.hexists("hash","key3")); 25 System.out.println("获取hash中的值:"+jedis.hmget("hash","key3")); 26 System.out.println("获取hash中的值:"+jedis.hmget("hash","key3","key4")); 27 }
3.6.排序操作指令
1 @Test 2 public void testSort(){ 3 jedis.flushDB(); 4 //jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap"); 5 jedis.lpush("collections","ArrayList"); 6 jedis.lpush("collections","Vector"); 7 jedis.lpush("collections","Stack"); 8 jedis.lpush("collections","HashMap"); 9 jedis.lpush("collections","WeakHashMap"); 10 jedis.lpush("collections","LinkedHashMap"); 11 System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); 12 SortingParams sortingParameters = new SortingParams(); 13 System.out.println(jedis.sort("collections",sortingParameters.alpha())); 14 System.out.println("==============================="); 15 //jedis.lpush("sortedList", "3","6","2","0","7","4"); 16 jedis.lpush("sortedList","3"); 17 jedis.lpush("sortedList","6"); 18 jedis.lpush("sortedList","2"); 19 jedis.lpush("sortedList","0"); 20 jedis.lpush("sortedList","7"); 21 jedis.lpush("sortedList","4"); 22 System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1)); 23 System.out.println("升序:"+jedis.sort("sortedList", sortingParameters.asc())); 24 System.out.println("降序:"+jedis.sort("sortedList", sortingParameters.desc())); 25 }