Jedis对redis的五大类型操作代码详解
程序员文章站
2023-12-18 19:42:40
本篇主要阐述jedis对redis的五大类型的操作:字符串、列表、散列、集合、有序集合。
jedisutil
这里的测试用例采用junit4进行运行,准备代码如下:...
本篇主要阐述jedis对redis的五大类型的操作:字符串、列表、散列、集合、有序集合。
jedisutil
这里的测试用例采用junit4进行运行,准备代码如下:
private static final string ipaddr = "10.10.195.112"; private static final int port = 6379; private static jedis jedis= null; @beforeclass public static void init() { jedis = jedisutil.getinstance().getjedis(ipaddr, port); } @afterclass public static void close() { jedisutil.getinstance().closejedis(jedis,ipaddr, port); }
其中jedisutil是对jedis做的简单封装,代码如下:
import org.apache.log4j.logger; import java.util.hashmap; import java.util.map; import redis.clients.jedis.jedis; import redis.clients.jedis.jedispool; import redis.clients.jedis.jedispoolconfig; public class jedisutil { private logger logger = logger.getlogger(this.getclass().getname()); private jedisutil(){ } private static class redisutilholder{ private static final jedisutil instance = new jedisutil(); } public static jedisutil getinstance(){ return redisutilholder.instance; } private static map<string,jedispool> maps = new hashmap<string,jedispool>(); private static jedispool getpool(string ip, int port){ string key = ip+":"+port; jedispool pool = null; if(!maps.containskey(key)) { jedispoolconfig config = new jedispoolconfig(); config.setmaxactive(redisconfig.max_active); config.setmaxidle(redisconfig.max_idle); config.setmaxwait(redisconfig.max_wait); config.settestonborrow(true); config.settestonreturn(true); pool = new jedispool(config,ip,port,redisconfig.timeout); maps.put(key, pool); } else { pool = maps.get(key); } return pool; } public jedis getjedis(string ip, int port) { jedis jedis = null; int count = 0; do { try { jedis = getpool(ip,port).getresource(); } catch (exception e) { logger.error("get redis master1 failed!",e); getpool(ip,port).returnbrokenresource(jedis); } } while(jedis == null && count<redisconfig.retry_num); return jedis; } public void closejedis(jedis jedis, string ip, int port){ if(jedis != null) { getpool(ip,port).returnresource(jedis); } } } public class redisconfig { //可用连接实例的最大数目,默认值为8; //如果赋值为-1,则表示不限制;如果pool已经分配了maxactive个jedis实例,则此时pool的状态为exhausted(耗尽)。 public static int max_active = 1024; //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 public static int max_idle = 200; //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出jedisconnectionexception; public static int max_wait = 10000; public static int timeout = 10000; public static int retry_num = 5; }
键操作
@test public void testkey() throws interruptedexception { system.out.println("清空数据:"+jedis.flushdb()); system.out.println("判断某个键是否存在:"+jedis.exists("username")); system.out.println("新增<'username','zzh'>的键值对:"+jedis.set("username", "zzh")); system.out.println(jedis.exists("name")); system.out.println("新增<'password','password'>的键值对:"+jedis.set("password", "password")); system.out.print("系统中所有的键如下:"); set<string> keys = jedis.keys("*"); system.out.println(keys); system.out.println("删除键password:"+jedis.del("password")); system.out.println("判断键password是否存在:"+jedis.exists("password")); system.out.println("设置键username的过期时间为5s:"+jedis.expire("username", 5)); timeunit.seconds.sleep(2); system.out.println("查看键username的剩余生存时间:"+jedis.ttl("username")); system.out.println("移除键username的生存时间:"+jedis.persist("username")); system.out.println("查看键username的剩余生存时间:"+jedis.ttl("username")); system.out.println("查看键username所存储的值的类型:"+jedis.type("username")); }
输出结果:
清空数据:ok 判断某个键是否存在:false 新增<'username','zzh'>的键值对:ok false 新增<'password','password'>的键值对:ok 系统中所有的键如下:[username, password] 删除键password:1 判断键password是否存在:false 设置键username的过期时间为5s:1 查看键username的剩余生存时间:3 移除键username的生存时间:1 查看键username的剩余生存时间:-1 查看键username所存储的值的类型:string
字符串操作
在redis里面,字符串可以存储三种类型的值:
字节串(byte string)
整数
浮点数
字节串
@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)); }
输出结果:
===========增加数据=========== ok ok ok 删除键key2:1 获取键key2:null 修改key1:ok 获取key1的值:value1changed 在key3后面加入值:9 key3的值:value3end 增加多个键值对:ok 获取多个键值对:[value01, value02, value03] 获取多个键值对:[value01, value02, value03, null] 删除多个键值对:2 获取多个键值对:[null, null, value03] ===========新增键值对防止覆盖原先值============== 1 1 0 value1 value2 ===========新增键值对并设置有效时间============= ok value3 null ===========获取原值,更新为新值========== value2 key2getset 获得key2的值的字串:y2g
memcached和redis同样有append的操作,但是memcached有prepend的操作,redis中并没有。
整数和浮点数
@test public void testnumber() { jedis.flushdb(); jedis.set("key1", "1"); jedis.set("key2", "2"); jedis.set("key3", "2.3"); system.out.println("key1的值:"+jedis.get("key1")); system.out.println("key2的值:"+jedis.get("key2")); system.out.println("key1的值加1:"+jedis.incr("key1")); system.out.println("获取key1的值:"+jedis.get("key1")); system.out.println("key2的值减1:"+jedis.decr("key2")); system.out.println("获取key2的值:"+jedis.get("key2")); system.out.println("将key1的值加上整数5:"+jedis.incrby("key1", 5)); system.out.println("获取key1的值:"+jedis.get("key1")); system.out.println("将key2的值减去整数5:"+jedis.decrby("key2", 5)); system.out.println("获取key2的值:"+jedis.get("key2")); }
输出结果:
key1的值:1 key2的值:2 key1的值加1:2 获取key1的值:2 key2的值减1:1 获取key2的值:1 将key1的值加上整数5:7 获取key1的值:7 将key2的值减去整数5:-4 获取key2的值:-4
在redis2.6或以上版本中有这个命令:incrbyfloat,即将键存储的值加上浮点数amount,jedis-2.1.0中不支持这一操作。
列表
@test public void testlist() { jedis.flushdb(); system.out.println("===========添加一个list==========="); jedis.lpush("collections", "arraylist", "vector", "stack", "hashmap", "weakhashmap", "linkedhashmap"); jedis.lpush("collections", "hashset"); jedis.lpush("collections", "treeset"); jedis.lpush("collections", "treemap"); system.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));//-1代表倒数第一个元素,-2代表倒数第二个元素 system.out.println("collections区间0-3的元素:"+jedis.lrange("collections",0,3)); system.out.println("==============================="); // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈 system.out.println("删除指定元素个数:"+jedis.lrem("collections", 2, "hashmap")); system.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); system.out.println("删除下表0-3区间之外的元素:"+jedis.ltrim("collections", 0, 3)); system.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); system.out.println("collections列表出栈(左端):"+jedis.lpop("collections")); system.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); system.out.println("collections添加元素,从列表右端,与lpush相对应:"+jedis.rpush("collections", "enummap")); system.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); system.out.println("collections列表出栈(右端):"+jedis.rpop("collections")); system.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); system.out.println("修改collections指定下标1的内容:"+jedis.lset("collections", 1, "linkedarraylist")); system.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); system.out.println("==============================="); system.out.println("collections的长度:"+jedis.llen("collections")); system.out.println("获取collections下标为2的元素:"+jedis.lindex("collections", 2)); system.out.println("==============================="); jedis.lpush("sortedlist", "3","6","2","0","7","4"); system.out.println("sortedlist排序前:"+jedis.lrange("sortedlist", 0, -1)); system.out.println(jedis.sort("sortedlist")); system.out.println("sortedlist排序后:"+jedis.lrange("sortedlist", 0, -1)); }
输出结果:
===========添加一个list=========== collections的内容:[treemap, treeset, hashset, linkedhashmap, weakhashmap, hashmap, stack, vector, arraylist] collections区间0-3的元素:[treemap, treeset, hashset, linkedhashmap] =============================== 删除指定元素个数:1 collections的内容:[treemap, treeset, hashset, linkedhashmap, weakhashmap, stack, vector, arraylist] 删除下表0-3区间之外的元素:ok collections的内容:[treemap, treeset, hashset, linkedhashmap] collections列表出栈(左端):treemap collections的内容:[treeset, hashset, linkedhashmap] collections添加元素,从列表右端,与lpush相对应:4 collections的内容:[treeset, hashset, linkedhashmap, enummap] collections列表出栈(右端):enummap collections的内容:[treeset, hashset, linkedhashmap] 修改collections指定下标1的内容:ok collections的内容:[treeset, linkedarraylist, linkedhashmap] =============================== collections的长度:3 获取collections下标为2的元素:linkedhashmap =============================== sortedlist排序前:[4, 7, 0, 2, 6, 3] [0, 2, 3, 4, 6, 7] sortedlist排序后:[4, 7, 0, 2, 6, 3]
redis中还有阻塞式的列表弹出命令以及在列表之间移动元素的命令:blpop, brpop, rpoplpush, brpoplpush等。
集合(set)
@test public void testset() { jedis.flushdb(); 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")); 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("e1是否在eleset中:"+jedis.sismember("eleset", "e5")); 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"));//eleset1中有,eleset2中没有 }
输出结果:
============向集合中添加元素============ 8 1 0 eleset的所有元素为:[e3, e4, e1, e2, e0, e8, e7, e6, e5] 删除一个元素e0:1 eleset的所有元素为:[e3, e4, e1, e2, e8, e7, e6, e5] 删除两个元素e7和e6:2 eleset的所有元素为:[e3, e4, e1, e2, e8, e5] 随机的移除集合中的一个元素:e5 随机的移除集合中的一个元素:e2 eleset的所有元素为:[e3, e4, e1, e8] eleset中包含元素的个数:4 e3是否在eleset中:true e1是否在eleset中:true e1是否在eleset中:false ================================= 8 6 将eleset1中删除e1并存入eleset3中:1 将eleset1中删除e2并存入eleset3中:1 eleset1中的元素:[e3, e4, e0, e8, e7, e5] eleset3中的元素:[e1, e2] ============集合运算================= eleset1中的元素:[e3, e4, e0, e8, e7, e5] eleset2中的元素:[e3, e4, e1, e2, e0, e8] eleset1和eleset2的交集:[e3, e4, e0, e8] eleset1和eleset2的并集:[e3, e4, e1, e2, e0, e8, e7, e5] eleset1和eleset2的差集:[e7, e5]
关于set还有一些其他命令:srandmember, sdiffstore, sinterstore, sunionstore等。
散列
@test public void testhash() { jedis.flushdb(); map<string,string> map = new hashmap<>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); map.put("key4","value4"); jedis.hmset("hash",map); jedis.hset("hash", "key5", "value5"); system.out.println("散列hash的所有键值对为:"+jedis.hgetall("hash"));//return map<string,string> system.out.println("散列hash的所有键为:"+jedis.hkeys("hash"));//return set<string> system.out.println("散列hash的所有值为:"+jedis.hvals("hash"));//return list<string> system.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:"+jedis.hincrby("hash", "key6", 6)); system.out.println("散列hash的所有键值对为:"+jedis.hgetall("hash")); system.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:"+jedis.hincrby("hash", "key6", 3)); system.out.println("散列hash的所有键值对为:"+jedis.hgetall("hash")); system.out.println("删除一个或者多个键值对:"+jedis.hdel("hash", "key2")); system.out.println("散列hash的所有键值对为:"+jedis.hgetall("hash")); system.out.println("散列hash中键值对的个数:"+jedis.hlen("hash")); system.out.println("判断hash中是否存在key2:"+jedis.hexists("hash","key2")); system.out.println("判断hash中是否存在key3:"+jedis.hexists("hash","key3")); system.out.println("获取hash中的值:"+jedis.hmget("hash","key3")); system.out.println("获取hash中的值:"+jedis.hmget("hash","key3","key4")); }
输出结果:
散列hash的所有键值对为:{key4=value4, key3=value3, key5=value5, key2=value2, key1=value1} 散列hash的所有键为:[key4, key3, key5, key2, key1] 散列hash的所有值为:[value4, value3, value1, value2, value5] 将key6保存的值加上一个整数,如果key6不存在则添加key6:6 散列hash的所有键值对为:{key4=value4, key3=value3, key6=6, key5=value5, key2=value2, key1=value1} 将key6保存的值加上一个整数,如果key6不存在则添加key6:9 散列hash的所有键值对为:{key4=value4, key3=value3, key6=9, key5=value5, key2=value2, key1=value1} 删除一个或者多个键值对:1 散列hash的所有键值对为:{key4=value4, key3=value3, key6=9, key5=value5, key1=value1} 散列hash中键值对的个数:5 判断hash中是否存在key2:false 判断hash中是否存在key3:true 获取hash中的值:[value3] 获取hash中的值:[value3, value4]
有序集合
@test public void testsortedset() { jedis.flushdb(); map<double,string> map = new hashmap<>(); map.put(1.2,"key2"); map.put(4.0, "key3"); map.put(5.0,"key4"); map.put(0.2,"key5"); system.out.println(jedis.zadd("zset", 3,"key1")); system.out.println(jedis.zadd("zset",map)); system.out.println("zset中的所有元素:"+jedis.zrange("zset", 0, -1)); system.out.println("zset中的所有元素:"+jedis.zrangewithscores("zset", 0, -1)); system.out.println("zset中的所有元素:"+jedis.zrangebyscore("zset", 0,100)); system.out.println("zset中的所有元素:"+jedis.zrangebyscorewithscores("zset", 0,100)); system.out.println("zset中key2的分值:"+jedis.zscore("zset", "key2")); system.out.println("zset中key2的排名:"+jedis.zrank("zset", "key2")); system.out.println("删除zset中的元素key3:"+jedis.zrem("zset", "key3")); system.out.println("zset中的所有元素:"+jedis.zrange("zset", 0, -1)); system.out.println("zset中元素的个数:"+jedis.zcard("zset")); system.out.println("zset中分值在1-4之间的元素的个数:"+jedis.zcount("zset", 1, 4)); system.out.println("key2的分值加上5:"+jedis.zincrby("zset", 5, "key2")); system.out.println("key3的分值加上4:"+jedis.zincrby("zset", 4, "key3")); system.out.println("zset中的所有元素:"+jedis.zrange("zset", 0, -1)); }
输出结果:
1 4 zset中的所有元素:[key5, key2, key1, key3, key4] zset中的所有元素:[[[107, 101, 121, 53],0.2], [[107, 101, 121, 50],1.2], [[107, 101, 121, 49],3.0], [[107, 101, 121, 51],4.0], [[107, 101, 121, 52],5.0]] zset中的所有元素:[key5, key2, key1, key3, key4] zset中的所有元素:[[[107, 101, 121, 53],0.2], [[107, 101, 121, 50],1.2], [[107, 101, 121, 49],3.0], [[107, 101, 121, 51],4.0], [[107, 101, 121, 52],5.0]] zset中key2的分值:1.2 zset中key2的排名:1 删除zset中的元素key3:1 zset中的所有元素:[key5, key2, key1, key4] zset中元素的个数:4 zset中分值在1-4之间的元素的个数:2 key2的分值加上5:6.2 key3的分值加上4:4.0 zset中的所有元素:[key5, key1, key3, key4, key2]
有序集合还有诸如zinterstore, zunionstore, zremrangebyscore, zremrangebyrank, zrevrank, zrevrange, zrangebyscore等命令。
排序sort
@test public void testsort() { jedis.flushdb(); jedis.lpush("collections", "arraylist", "vector", "stack", "hashmap", "weakhashmap", "linkedhashmap"); system.out.println("collections的内容:"+jedis.lrange("collections", 0, -1)); sortingparams sortingparameters = new sortingparams(); system.out.println(jedis.sort("collections",sortingparameters.alpha())); system.out.println("==============================="); jedis.lpush("sortedlist", "3","6","2","0","7","4"); system.out.println("sortedlist排序前:"+jedis.lrange("sortedlist", 0, -1)); system.out.println("升序:"+jedis.sort("sortedlist", sortingparameters.asc())); system.out.println("升序:"+jedis.sort("sortedlist", sortingparameters.desc())); system.out.println("==============================="); jedis.lpush("userlist", "33"); jedis.lpush("userlist", "22"); jedis.lpush("userlist", "55"); jedis.lpush("userlist", "11"); jedis.hset("user:66", "name", "66"); jedis.hset("user:55", "name", "55"); jedis.hset("user:33", "name", "33"); jedis.hset("user:22", "name", "79"); jedis.hset("user:11", "name", "24"); jedis.hset("user:11", "add", "beijing"); jedis.hset("user:22", "add", "shanghai"); jedis.hset("user:33", "add", "guangzhou"); jedis.hset("user:55", "add", "chongqing"); jedis.hset("user:66", "add", "xi'an"); sortingparameters = new sortingparams(); sortingparameters.get("user:*->name"); sortingparameters.get("user:*->add"); system.out.println(jedis.sort("userlist",sortingparameters)); }
输出结果:
collections的内容:[linkedhashmap, weakhashmap, hashmap, stack, vector, arraylist] [arraylist, hashmap, linkedhashmap, stack, vector, weakhashmap] =============================== sortedlist排序前:[4, 7, 0, 2, 6, 3] 升序:[0, 2, 3, 4, 6, 7] 升序:[7, 6, 4, 3, 2, 0] =============================== [24, beijing, 79, shanghai, 33, guangzhou, 55, chongqing]
总结
以上就是本文关于jedis对redis的五大操作代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
如有不足之处,欢迎留言指出。
推荐阅读
-
Jedis对redis的五大类型操作代码详解
-
php操作redis中的hash和zset类型数据的方法和代码例子
-
Redis中常用的五大数据类型的介绍以及代码的实现
-
php操作redis中的hash和zset类型数据的方法和代码例子
-
Java中使用Jedis连接Redis对List进行操作的常用命令
-
Java中使用Jedis连接Redis对Set进行操作的常用命令
-
MyBatis对MySQL数据库的增删改查操作代码详解
-
Java中使用Jedis连接Redis对String进行操作的常用命令
-
Java中使用Jedis连接Redis对Key进行操作的常用命令
-
php操作redis中的hash和zset类型数据的方法和代码例子