Java 使用 Redis
Redis需要和其他数据库 ( 例如 mySQL等)配合使用吗?还是可以单独使用?
单独使用是可以的,更多的肯定是与数据库使用,对于完整的业务来说,是要和其他数据库搭配使用的
还是要看具体场景和业务需求,如果和其他数据库结合的话,还是作为缓存的情况比较多;
1. 引入 maven 依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
2. 连接到 redis 服务(需要开启服务器端 redis-server.exe redis.conf 才可以连接上)
package com.my.dm.redis;import redis.clients.jedis.Jedis;
public class UseRedis {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span> Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("localhost"<span style="color: rgba(0, 0, 0, 1)">);
// jedis.auth("abc123");//设置有密码的 redis
System.out.println("连接成功!");
System.out.println(jedis.ping());
}
}
3.Redis Java String(字符串) 实例
jedis.set("testName", "hello word!");
System.out.println(jedis.get("testName"));
import redis.clients.jedis.Jedis;public class RedisStringJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("连接成功");
//设置 redis 字符串数据
jedis.set("w3ckey", "www.w3cschool.cn");
// 获取存储的数据并输出
System.out.println("redis 存储的字符串为:"+ jedis.get("w3ckey"));
}
}
package com.my.dm.redis;import redis.clients.jedis.Jedis;
public class UseRedis {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span> Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("localhost"<span style="color: rgba(0, 0, 0, 1)">); jedis.auth(</span>"abc123");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置有密码的redis</span>
System.out.println(jedis.ping());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1 2 get set</span> jedis.set("num", "123456789"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"num"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3.相当于sbuString</span> System.out.println(jedis.getrange("num", 0, 2<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4.Redis Getset 命令用于设置指定 key 的值,并返回 key 的旧值</span> System.out.println(jedis.getSet("num1", "123667"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.exists(</span>"num1"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否存在</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> * 5 7 Getbit setbit * Redis Getbit 命令用于对 key 所储存的字符串值,获取指定偏移量上的位(bit)。 当偏移量 OFFSET * 比字符串值的长度大,或者 key 不存在时,返回 0 * # bit 默认被初始化为 0 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> jedis.set(</span>"num2", "123456789123456789123456789123456789"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.getbit(</span>"num2", 100));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> # 对不存在的 key 或者不存在的 offset 进行 GETBIT, 返回 0</span> System.out.println(jedis.setbit("num2", 100, "1"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.getbit(</span>"num2", 100<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">6.Redis Mget 命令返回所有(一个或多个)给定 key 的值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil 。</span> System.out.println(jedis.mget("num","num1","num2"<span style="color: rgba(0, 0, 0, 1)">)); String key </span>= "abc"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">int</span> seconds = 1<span style="color: rgba(0, 0, 0, 1)">; String value </span>= "abc123456789"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">8.Redis Setex 命令为指定的 key 设置值及其过期时间。如果 key 已经存在, SETEX 命令将会替换旧的值。</span>
System.out.println(jedis.setex(key, seconds, value));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">9.Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。</span> System.out.println("9.setnx "+jedis.setnx(key,"999"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.get(key)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">10.Redis Setrange 命令用指定的字符串覆盖给定 key 所储存的字符串值,覆盖的位置从偏移量 offset 开始。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">被修改后的字符串长度。</span> System.out.println(jedis.setrange(key, 3, "xixiix"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.get(key)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">11.Redis Strlen 命令用于获取指定 key 所储存的字符串值的长度。当 key 储存的不是字符串值时,返回一个错误。</span>
System.out.println(jedis.strlen(key));
jedis.incrBy("count", 50); System.out.println(jedis.get("count"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">12.Redis Mset 命令用于同时设置一个或多个 key-value 对。</span> System.out.println(jedis.mset("a","11","b","222"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.mget(</span>"a","b"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">13.Redis Msetnx 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对。 </span> System.out.println(jedis.mset("a","11","b","222","c","555"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.mget(</span>"a","b","c"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">14.Redis Psetex 命令以毫秒为单位设置 key 的生存时间。可用版本>= 2.6.0 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println(jedis.psetex(key, 1000, value)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">15.Redis Incr 命令将 key 中储存的数字值增一。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">本操作的值限制在 64 位(bit)有符号数字表示之内。</span> jedis.set("count", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.incr(</span>"count"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"count"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">16.Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCRBY 命令。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">本操作的值限制在 64 位(bit)有符号数字表示之内。</span>
jedis.decr("count"); System.out.println("decr:" + jedis.get("count"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">17.Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可用版本>= 2.6.0 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jedis.incrByFloat("count",0.52);</span> System.out.println(jedis.get("count"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">18.Redis Decr 命令将 key 中储存的数字值减一。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECR 操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">本操作的值限制在 64 位(bit)有符号数字表示之内。 </span>
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">19.Redis Decrby 命令将 key 所储存的值减去指定的减量值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECRBY 操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">本操作的值限制在 64 位(bit)有符号数字表示之内。</span> jedis.decrBy("count", 11<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"decr: " + jedis.get("count"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">20.Redis Append 命令用于为指定的 key 追加值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回值 :追加指定值之后, key 中字符串的长度。</span> jedis.append("lk", "11"<span style="color: rgba(0, 0, 0, 1)">); jedis.append(</span>"lk", "22"<span style="color: rgba(0, 0, 0, 1)">); jedis.append(</span>"lk", "33"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"lk"<span style="color: rgba(0, 0, 0, 1)">)); }
}
4.Redis Java List(列表) 实例
import java.util.List import redis.clients.jedis.Jedis;public class RedisListJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("连接成功");
//存储数据到列表中
jedis.lpush("tutorial-list", "Redis");
jedis.lpush("tutorial-list", "Mongodb");
jedis.lpush("tutorial-list", "Mysql");
// 获取存储的数据并输出
List<String> list = jedis.lrange("tutorial-list", 0 ,2);
for(int i=0; i<list.size(); i++) {
System.out.println("列表项为:"+list.get(i));
}
}
}
5.Redis Java Keys 实例
import java.util.Iterator; import java.util.Set; import redis.clients.jedis.Jedis;public class RedisKeyJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("连接成功");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取数据并输出</span> Set<String> keys= jedis.keys("*"<span style="color: rgba(0, 0, 0, 1)">); Iterator</span><String> it=<span style="color: rgba(0, 0, 0, 1)">keys.iterator(); </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext) { String key</span>=<span style="color: rgba(0, 0, 0, 1)">it.next(); System.out.println(</span>"key"<span style="color: rgba(0, 0, 0, 1)">); }
}
}
6.Redis Java HASH 实例
package com.my.dm.redis;import java.util.HashMap;
import java.util.Map;import redis.clients.jedis.Jedis;
public class TestHash {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.auth("abc123");
System.out.println(jedis.ping());
Map<String, String> hash = new HashMap<>();
hash.put("user1", "abc");
hash.put("user2", "abc111");
jedis.hmset("user1", hash);
System.out.println(jedis.hgetAll("user"));Map<String, String> nums = new HashMap<>(); nums.put("a", "1"); nums.put("b", "2"); jedis.hmset("nums", nums); jedis.hincrBy("nums", "a", 12); System.out.println(jedis.hgetAll("nums"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略。</span> jedis.hdel("user", "user1"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.hgetAll(</span>"user"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.Redis Hexists 命令用于查看哈希表的指定字段是否存在。</span> System.out.println(jedis.hexists("user", "user2"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3.Redis Hget 命令用于返回哈希表中指定字段的值。</span> System.out.println(jedis.hget("user", "user2"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4.Redis Hgetall 命令用于返回哈希表中,所有的字段和值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jdk jar 里已封装</span> System.out.println(jedis.hgetAll("user"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5.Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">增量也可以为负数,相当于对指定字段进行减法操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果哈希表的 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">本操作的值被限制在 64 位(bit)有符号数字表示之内。</span>
System.out.println(jedis.hvals("user1"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">6.Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">7.Redis Hkeys 命令用于获取哈希表中的所有域(field)。</span> System.out.println(jedis.hkeys("nums"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">8.Redis Hlen 命令用于获取哈希表中字段的数量</span> System.out.println(jedis.hlen("nums"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">9.Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果指定的字段不存在于哈希表,那么返回一个 nil 值。</span> System.out.println(jedis.hmget("nums","a","b","c"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">10.Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">此命令会覆盖哈希表中已存在的字段。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作。</span> jedis.hmset("user1"<span style="color: rgba(0, 0, 0, 1)">, hash); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">11.Redis Hset 命令用于为哈希表中的字段赋值 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果字段已经存在于哈希表中,旧值将被覆盖。</span> jedis.hset("user1", "a22", "www"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">12.Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果字段已经存在于哈希表中,操作无效。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令。</span> jedis.hsetnx("user1", "a22", "www"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">13.Redis Hvals 命令返回哈希表所有域(field)的值</span>
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">14.HSCAN key cursor [MATCH pattern] [COUNT count] 迭代哈希表中的键值对。</span><span style="color: rgba(0, 0, 0, 1)"> }
}
6.Redis Java LIST 实例
package com.my.dm.redis;import redis.clients.jedis.Jedis;
import redis.clients.jedis.ListPosition;public class TestList {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
jedis.auth("abc123");
jedis.lpush("strs", "qq");
System.out.println(jedis.lrange("strs", 0, 100));jedis.lpush("inserts", "ooo"); jedis.linsert("inserts", ListPosition.BEFORE, "ooo", "wwww"); System.out.println(jedis.lrange("inserts", 0, 100));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.Redis Blpop 命令移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</span> System.out.println(jedis.blpop(1,"strs"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.lrange(</span>"strs", 0, 100<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.Redis Brpop 命令移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</span> System.out.println(jedis.brpop(1,"strs"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.lrange(</span>"strs", 0, 100<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"==="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3.Redis Brpoplpush 命令从列表中取出最后一个元素, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">并插入到另外一个列表的头部; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</span> jedis.lpush("reciver", "aaa","bbb"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"brpoplpush : " + jedis.brpoplpush("strs", "reciver", 1<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.lrange(</span>"reciver", 0, 100<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4.Redis Lindex 命令用于通过索引获取列表中的元素。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。</span> System.out.println(jedis.lindex("reciver", -1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5.Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定元素不存在于列表中时,不执行任何操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当列表不存在时,被视为空列表,不执行任何操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 key 不是列表类型,返回一个错误。</span>
System.out.println(jedis.llen("inserts"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">6.Redis Llen 命令用于返回列表的长度。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误。</span>
jedis.lpush("inserts", "qwew","oi");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">7.Redis Lpop 命令用于移除并返回列表的第一个元素。</span> System.out.println(jedis.lpop("inserts"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">8.Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意:在Redis 2.4版本以前的 LPUSH 命令,都只接受单个 value 值。</span>
jedis.lrem("inserts", -1, "22222");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">9.Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效</span> jedis.lpushx("inserts", "tail"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">10.Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">11.Redis Lrem 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">COUNT 的值可以是以下几种: </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">count = 0 : 移除表中所有与 VALUE 相等的值。</span>
jedis.lset("inserts", 1, "22222");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">12.Redis Lset 通过索引来设置元素的值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关于列表下标的更多信息,请参考 LINDEX 命令。</span>
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">13.Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。</span> jedis.ltrim("inserts", 0, 3<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">14.Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">15.Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">16.Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">17.Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效。</span><span style="color: rgba(0, 0, 0, 1)"> }
}
7.Redis Java set 实例
package com.my.dm.redis;import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;public class TestSet {
System.out.println(jedis.sadd("myset", "xixi"));</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span> Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("localhost"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.ping(</span>"wwww"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当集合 key 不是集合类型时,返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意:在Redis2.4版本以前, SADD 只接受单个成员值。</span>
System.out.println(jedis.scard("myset"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.Redis Scard 命令返回集合中元素的数量</span>
System.out.println(jedis.sinter("a","b"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3.Redis Sdiff 命令返回给定集合之间的差集。不存在的集合 key 将视为空集。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。</span> jedis.sadd("a", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"a", "2"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"a", "13"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"b", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"b", "13"<span style="color: rgba(0, 0, 0, 1)">); System.err.println(jedis.sdiff(</span>"a","b"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4.Redis Sdiffstore 命令将给定集合之间的差集存储在指定的集合中。如果指定的集合 key 已存在,则会被覆盖。</span> jedis.sdiffstore("dskey", "a","b"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.smembers(</span>"dskey"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5.Redis Sinter 命令返回给定所有给定集合的交集。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。</span>
System.out.println(jedis.smove("inkeys","newkey1", "1"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">6.Redis Sinterstore 命令将给定集合之间的交集存储在指定的集合中。如果指定的集合已经存在,则将其覆盖。</span> jedis.sinterstore("inkeys", "a","b"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">7.Redis Sismember 命令判断成员元素是否是集合的成员。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果成员元素是集合的成员,返回 1 。 如果成员元素不是集合的成员,或 key 不存在,返回 0 。</span> System.out.println(jedis.sismember("inkeys", "1"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">8.Redis Smembers 命令返回集合中的所有的成员。 不存在的集合 key 被视为空集合。</span> System.out.println(jedis.smembers("inkeys"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">9.Redis Smove 命令将指定成员 member 元素从 source 集合移动到 destination 集合。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">SMOVE 是原子性操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 source 集合不存在或不包含指定的 member 元素,则 SMOVE 命令不执行任何操作,仅返回 0 。否则, member 元素从 source 集合中被移除,并添加到 destination 集合中去。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当 source 或 destination 不是集合类型时,返回一个错误。</span>
System.out.println(jedis.smembers(</span>"newkey1"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">10.Redis Spop 命令用于移除集合中的指定 key 的一个或多个随机元素,移除后会返回移除的元素。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该命令类似 Srandmember 命令,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。</span> System.out.println(jedis.spop("newkey1"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">11.Redis Srandmember 命令用于返回集合中的一个随机元素。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从 Redis 2.6 版本开始, Srandmember 命令接受可选的 count 参数: </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 count 为正数,且小于集合基数,那么命令返回一个包含 count 个元素的数组,数组中的元素各不相同。如果 count 大于等于集合基数,那么返回整个集合。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。</span> jedis.sadd("newkey1", "11"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"newkey1", "22"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"newkey1", "33"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"newkey1", "44"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"newkey1", "66"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.srandmember(</span>"newkey1",2<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">12.Redis Srem 命令用于移除集合中的一个或多个成员元素,不存在的成员元素会被忽略。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当 key 不是集合类型,返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在 Redis 2.4 版本以前, SREM 只接受单个成员值。</span> System.out.println(jedis.srem("newkey1","66","44"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">13.Redis Sunion 命令返回给定集合的并集。不存在的集合 key 被视为空集 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">SUNION KEY KEY1..KEYN </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">14.Redis Sunionstore 命令 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果 destination 已经存在,则将其覆盖 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">SUNIONSTORE myset myset1 myset2 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">15.Redis Sscan 命令用于迭代集合中键的元素。</span> jedis.sadd("myset", "a1234"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "b2234"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "va3234"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "a4234"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.sscan(</span>"myset", "0"<span style="color: rgba(0, 0, 0, 1)">)); }
}
8.Redis Java set 实例
package com.my.dm.redis;import redis.clients.jedis.Jedis;
import redis.clients.jedis.ZParams;public class TestZset {
Jedis jedis = new Jedis("localhost");</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span>
System.out.println(jedis.zadd("zsort", 1, "www")); System.out.println(jedis.zadd("zsort", 0, "eee")); System.out.println(jedis.zadd("zsort", 7, "mmm")); System.out.println(jedis.zrangeByScore("zsort", 0, 20));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1.Redis Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果某个成员已经是有序集的成员,那么更新这个成员的分数值,并通过重新插入这个成员元素,来保证该成员在正确的位置上。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分数值可以是整数值或双精度浮点数。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果有序集合 key 不存在,则创建一个空的有序集并执行 ZADD 操作。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当 key 存在但不是有序集类型时,返回一个错误。</span>
System.out.println(jedis.zlexcount("aa", "[22", "[55"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2.Redis Zcard 命令用于计算集合中元素的数量。</span> System.out.println(jedis.zcard("zsort"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3.Redis Zcount 命令用于计算有序集合中指定分数区间的成员数量。</span> System.out.println(jedis.zcount("zsort", 0, 5<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 4.Redis Zincrby 命令对有序集合中指定成员的分数加上增量 increment </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可以通过传递一个负数值 increment ,让分数减去相应的值,比如 ZINCRBY key -5 member ,就是让 member </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 的 score 值减去 5 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当 key 不存在,或分数不是 key 的成员时, ZINCRBY key increment member 等同于 ZADD key </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> increment member 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当 key 不是有序集类型时,返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分数值可以是整数值或双精度浮点数。</span> System.out.println(jedis.zincrby("zsort", 3, "www"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 5.Redis Zinterstore 命令计算给定的一个或多个有序集的交集,其中给定 key 的数量必须以 numkeys </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 参数指定,并将该交集(结果集)储存到 destination 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 默认情况下,结果集中某个成员的分数值是所有给定集下该成员分数值之和。</span> jedis.zadd("aa", 1, "22"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"aa", 2, "55"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"aa", 3, "66"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"aa", 4, "77"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.zrange(</span>"aa", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); jedis.zadd(</span>"bb", 1, "22"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"bb", 1, "55"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"bb", 1, "66"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"aa", 1, "787"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.zrange(</span>"bb", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.zinterstore(</span>"asd", "aa", "bb"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.zrange(</span>"asd", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 6.Redis Zlexcount 命令在计算有序集合中指定字典区间内成员数量。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 其参数min和max,必须包含 ( 或 [ ,分别表示开区间和闭区间。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 比如,zrangebylex zset (cra [d,会在zset集合中,列出字典序大小比cra大,而且小于等于d的元素。</span>
System.out.println(jedis.zrangeByLex("aa", "[22", "[6666"));</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> * 7.Redis Zrange 返回有序集中,指定区间内的成员。 其中成员的位置按分数值递增(从小到大)来排序。 * 具有相同分数值的成员按字典序(lexicographical order )来排列。 如果你需要成员按值递减(从大到小)来排列,请使用 * ZREVRANGE 命令。 下标参数 start 和 stop 都以 0 为底,也就是说,以 0 表示有序集第一个成员,以 1 * 表示有序集第二个成员,以此类推。 你也可以使用负数下标,以 -1 表示最后一个成员, -2 表示倒数第二个成员,以此类推。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> System.out.println(jedis.zrange(</span>"aa", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 8.Redis Zrangebylex 通过字典区间返回有序集合的成员。</span>
System.out.println(jedis.zrank("aa", "66"));</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> * 9.Redis Zrangebyscore 返回有序集合中指定分数区间的成员列表。有序集成员按分数值递增(从小到大)次序排列。 * 具有相同分数值的成员按字典序来排列(该属性是有序集提供的,不需要额外的计算)。 默认情况下,区间的取值使用闭区间 * (小于等于或大于等于),你也可以通过给参数前增加 ( 符号来使用可选的开区间 (小于或大于)。 举个例子: ZRANGEBYSCORE * zset (1 5 返回所有符合条件 1 < score <= 5 的成员,而 ZRANGEBYSCORE zset (5 (10 * 则返回所有符合条件 5 < score < 10 的成员。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> System.out.println(jedis.zrangeByScore(</span>"aa", 1, 3<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 10 Redis Zrank 返回有序集中指定成员的排名。其中有序集成员按分数值递增(从小到大)顺序排列。</span>
System.out.println(jedis.zrem("aa", "66","55"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 11.Redis Zrem 命令用于移除有序集中的一个或多个成员,不存在的成员将被忽略。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> key 存在但不是有序集类型时,返回一个错误。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 注意: 在 Redis 2.4 版本以前, ZREM 每次只能删除一个元素。</span>
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">12.Redis Zremrangebylex 命令用于移除有序集合中给定的字典区间的所有成员。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZREMRANGEBYLEX myzset [alpha [omega </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">13.Redis Zremrangebyrank 命令用于移除有序集中,指定排名(rank)区间内的所有成员。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ZREMRANGEBYRANK salary 0 1 # 移除下标 0 至 1 区间内的成员 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">14.Redis Zremrangebyscore 命令用于移除有序集中,指定分数(score)区间内的所有成员。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZREMRANGEBYSCORE salary 1500 3500 # 移除所有薪水在 1500 到 3500 内的员工 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">15.Redis Zrevrangebyscore 返回有序集中指定分数区间内的所有的成员。有序集成员按分数值递减(从大到小)的次序排列。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">具有相同分数值的成员按字典序的逆序(reverse lexicographical order )排列。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">除了成员按分数值递减的次序排列这一点外, ZREVRANGEBYSCORE 命令的其他方面和 ZRANGEBYSCORE 命令一样。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZREVRANGE salary 0 -1 WITHSCORES # 递减排列 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">16.Redis Zrevrange 命令返回有序集中,指定区间内的成员。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">其中成员的位置按分数值递减(从大到小)来排列。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">具有相同分数值的成员按字典序的逆序(reverse lexicographical order)排列。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">除了成员按分数值递减的次序排列这一点外, ZREVRANGE 命令的其他方面和 ZRANGE 命令一样。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">17.ZRANGE salary 0 -1 WITHSCORES # 递增排列 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZREVRANGE salary 0 -1 WITHSCORES # 递减排列 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">18.Redis Zscore 命令返回有序集中,成员的分数值。 如果成员元素不是有序集 key 的成员,或 key 不存在,返回 nil 。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZSCORE salary peter # 注意返回值是字符串 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">19.Redis Zunionstore 命令计算给定的一个或多个有序集的并集,其中给定 key 的数量必须以 numkeys 参数指定,并将该并集(结果集)储存到 destination 。
// 默认情况下,结果集中某个成员的分数值是所有给定集下该成员分数值之和 。
}</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ZUNIONSTORE salary 2 programmer manager WEIGHTS 1 3 # 公司决定加薪。。。除了程序员。。。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">20.Redis Zscan 命令用于迭代有序集合中的元素(包括元素成员和元素分值) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZSCAN key cursor [MATCH pattern] [COUNT count]</span>
}