前言 Jedis is a blazingly small and sane Redis java client. Jedis was conceived to be EASY to use. Jedis is fully compatible with redis 2.8.x, 3.x.x and above*. (Github )
创建Jedis对象 1 Jedis jedis = new Jedis ("127.0.0.1" , 6379 );
插入数据 1 jedis.set("key" ,"value" );
如果没有key,进行写操作 1 jedis.setnx("key" ,"value" );
创建数据的同时设定超时时间
<num>
:超时时间(单位:秒)
1 jedis.setex("key" , <num>,"value" );
带参数的写操作
xx()
:有key,则进行写操作(也就是修改操作)nx()
:没有key,则进行写操作px()
:创建数据的同时设置超时时间,保证原子性(单位:毫秒)ex()
:创建数据的同时设置超时时间,保证原子性(单位:秒)
同时保证,在没有key的情况下创建value,并且设置超时时间
1 2 3 SetParams setParams = new SetParams ();setParams.nx().ex(<num>); jedis.set("key" , "value" , setParams);
获取数据
对哈希数据的操作 插入数据 1 2 jedis.hset("person" ,"id" ,"1" ); jedis.hset("person" ,"name" ,"zhangsan" );
获取key-value 1 Map<String, String> map = jedis.hgetAll("person" );
获取key 1 Set<String> set = jedis.hkeys("person" );
获取value 1 List<String> list = jedis.hvals("person" );
对列表数据的操作 1 2 jedis.lpush("key" ,"value_1" ,"value_2" ); jedis.rpop("key" )
开启事务 1 2 3 4 5 6 7 8 9 10 11 Transaction transaction = jedis.multi();try { transaction.set("key" ,"value" ); transaction.exec(); } catch (Exception e) { transaction.discard(); }
完成