学习 Redis SortedSet(有序集合)
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.springframework.data.redis.connection.DefaultTuple;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.RedisZSetCommands.Limit;
import org.springframework.data.redis.connection.RedisZSetCommands.Range;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ScanOptions.ScanOptionsBuilder;
public class TestRedisSortedSet {
private static JedisConnectionFactory getJedisConnectionFactory() {
JedisConnectionFactory jcf = new JedisConnectionFactory();
jcf.setHostName("127.0.0.1");
jcf.setPort(6379);
jcf.setPassword("123");
jcf.afterPropertiesSet();
return jcf;
}
private static RedisConnection getRedisConnection() {
return getJedisConnectionFactory().getConnection();
}
public static void main(String[] args) {
RedisConnection rc = getRedisConnection();
//ZADD 将一个或多个 member 元素及其 score 值加入到有序集 key 当中。
boolean zAdd = rc.zAdd("zset".getBytes(), 11, "k".getBytes());
System.out.println("ZADD [" + zAdd + "]");
Set<Tuple> tuples = new HashSet<Tuple>();
Tuple tuplev = new DefaultTuple("v".getBytes(), 12d);
Tuple tuplew = new DefaultTuple("w".getBytes(), 12d);
tuples.add(tuplev);
tuples.add(tuplew);
long mzAdd = rc.zAdd("zset".getBytes(), tuples);
System.out.println("mzAdd [" + mzAdd + "]");
//ZCARD 返回有序集 key 的基数。
long zCard = rc.zCard("zset".getBytes());
System.out.println("ZCARD [" + zCard + "]");
//ZCOUNT 返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量。
long zCount = rc.zCount("salary".getBytes(), 2000, 4000);
System.out.println("ZCOUNT [" + zCount + "]");
long total = rc.zCount("salary".getBytes(), Range.unbounded());
System.out.println("total [" + total + "]");
Range range = new Range();
range.gt(1000);
range.lt(3000);
long ltgt = rc.zCount("salary".getBytes(), range);
System.out.println("ltgt [" + ltgt + "]");
Range range2 = new Range();
range2.gte(1000);
range2.lte(3000);
long ltegte = rc.zCount("salary".getBytes(), range2);
System.out.println("ltegte [" + ltegte + "]");
//ZINCRBY 为有序集 key 的成员 member 的 score 值加上增量 increment 。
double zIncrBy = rc.zIncrBy("salary".getBytes(), 0, "Linda".getBytes());
System.out.println("ZINCRBY [" + zIncrBy + "]");
//ZRANGE 返回有序集 key 中,指定区间内的成员。
Set<byte[]> zRange = rc.zRange("salary".getBytes(), 0, -1);
Iterator<byte[]> itr = zRange.iterator();
while (itr.hasNext()) {
System.out.println("ZRANGE [" + new String(itr.next()) + "]");
}
Set<Tuple> zRangeWithScores = rc.zRangeWithScores("salary".getBytes(), 0, -1);
Iterator<Tuple> itrTuple = zRangeWithScores.iterator();
while (itrTuple.hasNext()) {
Tuple tuple = itrTuple.next();
System.out.println(tuple.getScore() + " " + new String(tuple.getValue()));
}
//ZRANGEBYSCORE 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。有序集成员按 score 值递增(从小到大)次序排列。
Set<byte[]> zRangeByScore = rc.zRangeByScore("salary".getBytes(), range2);
Iterator<byte[]> itrzRangeByScore = zRangeByScore.iterator();
while (itrzRangeByScore.hasNext()) {
System.out.println("zRangeByScore [" + new String(itrzRangeByScore.next()) + "]");
}
Set<byte[]> zRangeByScoreWithLimit = rc.zRangeByScore("salary".getBytes(), range2, Limit.limit().offset(1).count(3));
Iterator<byte[]> iters = zRangeByScoreWithLimit.iterator();
while (iters.hasNext()) {
System.out.println("zRangeByScoreWithLimit [" + new String(iters.next()) + "]");
}
//ZRANK 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递增(从小到大)顺序排列。
long zRank = rc.zRank("salary".getBytes(), "Scott".getBytes());
System.out.println("ZRANK [" + zRank + "]");
//ZREM 移除有序集 key 中的一个或多个成员,不存在的成员将被忽略。
long zRem = rc.zRem("zdel".getBytes(), "a".getBytes(), "b".getBytes());
System.out.println("ZREM [" + zRem + "]");
//ZREMRANGEBYRANK 移除有序集 key 中,指定排名(rank)区间内的所有成员。
long zRemRange = rc.zRemRange("zdel".getBytes(), 3, 5);
System.out.println("zRemRange [" + zRemRange + "]");
//ZREMRANGEBYSCORE 移除有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。
long zRemRangeByScore = rc.zRemRangeByScore("zdel".getBytes(), 0, 5);
System.out.println("ZREMRANGEBYSCORE [" + zRemRangeByScore + "]");
//ZREVRANGE
Set<byte[]> zRevRange = rc.zRevRange("salary".getBytes(), 0, -1);
Iterator<byte[]> itrzRevRange = zRevRange.iterator();
while (itrzRevRange.hasNext()) {
System.out.println("ZREVRANGE [" + new String(itrzRevRange.next()) + "]");
}
Set<Tuple> zRevRangeWithScores = rc.zRevRangeWithScores("salary".getBytes(), 0, -1);
Iterator<Tuple> itrWithScores = zRevRangeWithScores.iterator();
while (itrWithScores.hasNext()) {
Tuple tuple = itrWithScores.next();
System.out.println("zRevRangeWithScores [" + tuple.getScore() + " " + new String(tuple.getValue()) + "]");
}
//ZREVRANGEBYSCORE 返回有序集 key 中, score 值介于 max 和 min 之间(默认包括等于 max 或 min )的所有的成员。有序集成员按 score 值递减(从大到小)的次序排列。
Set<byte[]> zRevRangeByScore = rc.zRevRangeByScore("salary".getBytes(), 1000, 5000);
Iterator<byte[]> itrzRevRangeByScore = zRevRangeByScore.iterator();
while (itrzRevRangeByScore.hasNext()) {
System.out.println("zRevRangeByScore [" + new String(itrzRevRangeByScore.next()) + "]");
}
//ZREVRANK 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递减(从大到小)排序。
long zRevRank = rc.zRevRank("salary".getBytes(), "Tom".getBytes());
System.out.println("ZREVRANK [" + zRevRank + "]");
//ZSCORE 返回有序集 key 中,成员 member 的 score 值。
double zScore = rc.zScore("salary".getBytes(), "Jim".getBytes());
System.out.println("ZSCORE [" + zScore + "]");
//ZUNIONSTORE 计算给定的一个或多个有序集的并集,其中给定 key 的数量必须以 numkeys 参数指定,并将该并集(结果集)储存到 destination 。
//默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。
long zUnionStore = rc.zUnionStore("salary4".getBytes(), "salary".getBytes(), "salary2".getBytes());
System.out.println("ZUNIONSTORE [" + zUnionStore + "]");
int[] weights = {2, 4};
long mzUnionStore = rc.zUnionStore("salary4".getBytes(), Aggregate.MAX, weights, "salary".getBytes(), "salary2".getBytes());
System.out.println("mzUnionStore [" + mzUnionStore + "]");
//ZINTERSTORE 计算给定的一个或多个有序集的交集,其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。
//默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之和.
long zInterStore = rc.zInterStore("salary5".getBytes(), "salary".getBytes(), "salary2".getBytes());
System.out.println("zInterStore [" + zInterStore + "]");
long mzInterStore = rc.zInterStore("salary5".getBytes(), Aggregate.MAX, weights, "salary".getBytes(), "salary2".getBytes());
System.out.println("mzInterStore [" + mzInterStore + "]");
//ZSCAN
ScanOptionsBuilder sob = ScanOptions.scanOptions().count(1).match("???");
ScanOptions options = sob.build();
Cursor<Tuple> zScan = rc.zScan("salary".getBytes(), options);
while (zScan.hasNext()) {
Tuple tuple = zScan.next();
System.out.println("ZSCAN [" + tuple.getScore() + " " + new String(tuple.getValue()) + "]");
}
}
}