Redis 有序集合
Redis 有序集合简介
Redis 有序集合是按关联分数排序的唯一字符串(成员)集合。当多个字符串具有相同分数时,字符串按字典顺序排序。有序集合的一些用例包括
- 排行榜。例如,您可以使用有序集合轻松维护大型在线游戏中最高分数的有序列表。
- 速率限制器。特别地,您可以使用有序集合构建滑动窗口速率限制器,以防止过多的 API 请求。
您可以将有序集合视为集合和哈希的混合。与集合一样,有序集合由唯一的、非重复的字符串元素组成,因此在某种意义上,有序集合也是一个集合。
但是,虽然集合中的元素没有排序,但有序集合中的每个元素都与一个浮点值相关联,称为分数(这就是类型也类似于哈希的原因,因为每个元素都映射到一个值)。
此外,有序集合中的元素是按顺序取出的(因此它们不是按请求排序的,顺序是用于表示有序集合的数据结构的特性)。它们根据以下规则排序
- 如果 B 和 A 是两个分数不同的元素,则如果 A.score 大于 B.score,则 A > B。
- 如果 B 和 A 的分数完全相同,则如果 A 字符串在字典序上大于 B 字符串,则 A > B。B 和 A 字符串不能相等,因为排序集只包含唯一元素。
让我们从一个简单的例子开始,我们将添加所有参赛者以及他们在第一场比赛中的得分
> ZADD racer_scores 10 "Norem"
(integer) 1
> ZADD racer_scores 12 "Castilla"
(integer) 1
> ZADD racer_scores 8 "Sam-Bodden" 10 "Royce" 6 "Ford" 14 "Prickett"
(integer) 4
"""
Code samples for Sorted set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sorted-sets/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.zadd("racer_scores", {"Norem": 10})
print(res1) # >>> 1
res2 = r.zadd("racer_scores", {"Castilla": 12})
print(res2) # >>> 1
res3 = r.zadd(
"racer_scores",
{"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12},
)
print(res3) # >>> 4
res4 = r.zrange("racer_scores", 0, -1)
print(res4) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
res5 = r.zrevrange("racer_scores", 0, -1)
print(res5) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford']
res6 = r.zrange("racer_scores", 0, -1, withscores=True)
print(
res6
)
# >>> [
# ('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0),
# ('Castilla', 12.0), ('Prickett', 14.0)
# ]
res7 = r.zrangebyscore("racer_scores", "-inf", 10)
print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
res8 = r.zrem("racer_scores", "Castilla")
print(res8) # >>> 1
res9 = r.zremrangebyscore("racer_scores", "-inf", 9)
print(res9) # >>> 2
res10 = r.zrange("racer_scores", 0, -1)
print(res10) # >>> ['Norem', 'Royce', 'Prickett']
res11 = r.zrank("racer_scores", "Norem")
print(res11) # >>> 0
res12 = r.zrevrank("racer_scores", "Norem")
print(res12) # >>> 2
res13 = r.zadd(
"racer_scores",
{
"Norem": 0,
"Sam-Bodden": 0,
"Royce": 0,
"Ford": 0,
"Prickett": 0,
"Castilla": 0,
},
)
print(res13) # >>> 3
res14 = r.zrange("racer_scores", 0, -1)
print(res14) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
res15 = r.zrangebylex("racer_scores", "[A", "[L")
print(res15) # >>> ['Castilla', 'Ford']
res16 = r.zadd("racer_scores", {"Wood": 100})
print(res16) # >>> 1
res17 = r.zadd("racer_scores", {"Henshaw": 100})
print(res17) # >>> 1
res18 = r.zadd("racer_scores", {"Henshaw": 150})
print(res18) # >>> 0
res19 = r.zincrby("racer_scores", 50, "Wood")
print(res19) # >>> 150.0
res20 = r.zincrby("racer_scores", 50, "Henshaw")
print(res20) # >>> 200.0
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' });
console.log(res1); // >>> 1
const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' });
console.log(res2); // >>> 1
const res3 = await client.zAdd('racer_scores', [
{ score: 8, value: 'Sam-Bodden' },
{ score: 10, value: 'Royce' },
{ score: 6, value: 'Ford' },
{ score: 14, value: 'Prickett' },
{ score: 12, value: 'Castilla' }
]);
console.log(res3); // >>> 4
const res4 = await client.zRange('racer_scores', 0, -1);
console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
const res6 = await client.zRangeWithScores('racer_scores', 0, -1);
console.log(res6);
// >>> [
// { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 },
// { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 },
// { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 }
// ]
const res7 = await client.zRangeByScore('racer_scores', '-inf', 10);
console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
const res8 = await client.zRem('racer_scores', 'Castilla');
console.log(res8); // >>> 1
const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9);
console.log(res9); // >>> 2
const res10 = await client.zRange('racer_scores', 0, -1);
console.log(res10); // >>> ['Norem', 'Royce', 'Prickett']
const res11 = await client.zRank('racer_scores', 'Norem');
console.log(res11); // >>> 0
const res12 = await client.zRevRank('racer_scores', 'Norem');
console.log(res12); // >>> 2
const res13 = await client.zAdd('racer_scores', [
{ score: 0, value: 'Norem' },
{ score: 0, value: 'Sam-Bodden' },
{ score: 0, value: 'Royce' },
{ score: 0, value: 'Ford' },
{ score: 0, value: 'Prickett' },
{ score: 0, value: 'Castilla' }
]);
console.log(res13); // >>> 3
const res14 = await client.zRange('racer_scores', 0, -1);
console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
const res15 = await client.zRangeByLex('racer_scores', '[A', '[L');
console.log(res15); // >>> ['Castilla', 'Ford']
const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' });
console.log(res16); // >>> 1
const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' });
console.log(res17); // >>> 1
const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true });
console.log(res18); // >>> 0
const res19 = await client.zIncrBy('racer_scores', 50, 'Wood');
console.log(res19); // >>> 150.0
const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw');
console.log(res20); // >>> 200.0
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.resps.Tuple;
public class SortedSetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.zadd("racer_scores", 10d, "Norem");
System.out.println(res1); // >>> 1
long res2 = jedis.zadd("racer_scores", 12d, "Castilla");
System.out.println(res2); // >>> 1
long res3 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Sam-Bodden", 8d);
put("Royce", 10d);
put("Ford", 6d);
put("Prickett", 14d);
put("Castilla", 12d);
}});
System.out.println(res3); // >>> 4
List<String> res4 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett]
List<String> res5 = jedis.zrevrange("racer_scores", 0, -1);
System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford]
List<Tuple> res6 = jedis.zrangeWithScores("racer_scores", 0, -1);
System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]]
List<String> res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d);
System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce]
long res8 = jedis.zrem("racer_scores", "Castilla");
System.out.println(res8); // >>> 1
long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d);
System.out.println(res9); // >>> 2
List<String> res10 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res10); // >>> [Norem, Royce, Prickett]
long res11 = jedis.zrank("racer_scores", "Norem");
System.out.println(res11); // >>> 0
long res12 = jedis.zrevrank("racer_scores", "Norem");
System.out.println(res12); // >>> 2
long res13 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Norem", 0d);
put("Sam-Bodden", 0d);
put("Royce", 0d);
put("Ford", 0d);
put("Prickett", 0d);
put("Castilla", 0d);
}});
System.out.println(res13); // >>> 3
List<String> res14 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden]
List<String> res15 = jedis.zrangeByLex("racer_scores", "[A", "[L");
System.out.println(res15); // >>> [Castilla, Ford]
long res16 = jedis.zadd("racer_scores", 100d, "Wood");
System.out.println(res16); // >>> 1
long res17 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res17); // >>> 1
long res18 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res18); // >>> 0
double res19 = jedis.zincrby("racer_scores", 50d, "Wood");
System.out.println(res19); // >>> 150.0
double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw");
System.out.println(res20); // >>> 200.0
}
}
using StackExchange.Redis;
public class SortedSetExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10);
Console.WriteLine(res1); // >>> True
bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12);
Console.WriteLine(res2); // >>> True
long res3 = db.SortedSetAdd("racer_scores", new[]{
new SortedSetEntry("Sam-Bodden", 8),
new SortedSetEntry("Royce", 10),
new SortedSetEntry("Ford", 6),
new SortedSetEntry("Prickett", 14),
new SortedSetEntry("Castilla", 12)
});
Console.WriteLine(res3); // >>> 4
RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett
RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending);
Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford
SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14
RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10);
Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce
bool res8 = db.SortedSetRemove("racer_scores", "Castilla");
Console.WriteLine(res8); // >>> True
long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9);
Console.WriteLine(res9); // >>> 2
RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett
long? res11 = db.SortedSetRank("racer_scores", "Norem");
Console.WriteLine(res11); // >>> 0
long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending);
Console.WriteLine(res12); // >>> 2
long res13 = db.SortedSetAdd("racer_scores", new[] {
new SortedSetEntry("Norem", 0),
new SortedSetEntry("Sam-Bodden", 0),
new SortedSetEntry("Royce", 0),
new SortedSetEntry("Ford", 0),
new SortedSetEntry("Prickett", 0),
new SortedSetEntry("Castilla", 0)
});
Console.WriteLine(res13); // >>> 3
RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden
RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None);
Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford
bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100);
Console.WriteLine(res16); // >>> True
bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100);
Console.WriteLine(res17); // >>> True
bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150);
Console.WriteLine(res18); // >>> False
double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50);
Console.WriteLine(res19); // >>> 150.0
double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50);
Console.WriteLine(res20); // >>> 200.0
}
}
如您所见,ZADD
类似于 SADD
,但它需要一个额外的参数(放在要添加的元素之前),该参数是分数。 ZADD
也是可变参数的,因此您可以自由指定多个分数-值对,即使在上面的示例中没有使用。
使用排序集,可以很容易地返回按出生年份排序的黑客列表,因为他们实际上已经排序。
实现说明:排序集通过包含跳跃列表和哈希表这两种双端口数据结构实现,因此每次添加元素时,Redis 都执行 O(log(N)) 操作。这很好,但是当我们要求排序元素时,Redis 根本不需要做任何工作,因为它已经排序了。请注意,ZRANGE
的顺序是从低到高,而 ZREVRANGE
的顺序是从高到低。
> ZRANGE racer_scores 0 -1
1) "Ford"
2) "Sam-Bodden"
3) "Norem"
4) "Royce"
5) "Castilla"
6) "Prickett"
> ZREVRANGE racer_scores 0 -1
1) "Prickett"
2) "Castilla"
3) "Royce"
4) "Norem"
5) "Sam-Bodden"
6) "Ford"
"""
Code samples for Sorted set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sorted-sets/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.zadd("racer_scores", {"Norem": 10})
print(res1) # >>> 1
res2 = r.zadd("racer_scores", {"Castilla": 12})
print(res2) # >>> 1
res3 = r.zadd(
"racer_scores",
{"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12},
)
print(res3) # >>> 4
res4 = r.zrange("racer_scores", 0, -1)
print(res4) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
res5 = r.zrevrange("racer_scores", 0, -1)
print(res5) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford']
res6 = r.zrange("racer_scores", 0, -1, withscores=True)
print(
res6
)
# >>> [
# ('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0),
# ('Castilla', 12.0), ('Prickett', 14.0)
# ]
res7 = r.zrangebyscore("racer_scores", "-inf", 10)
print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
res8 = r.zrem("racer_scores", "Castilla")
print(res8) # >>> 1
res9 = r.zremrangebyscore("racer_scores", "-inf", 9)
print(res9) # >>> 2
res10 = r.zrange("racer_scores", 0, -1)
print(res10) # >>> ['Norem', 'Royce', 'Prickett']
res11 = r.zrank("racer_scores", "Norem")
print(res11) # >>> 0
res12 = r.zrevrank("racer_scores", "Norem")
print(res12) # >>> 2
res13 = r.zadd(
"racer_scores",
{
"Norem": 0,
"Sam-Bodden": 0,
"Royce": 0,
"Ford": 0,
"Prickett": 0,
"Castilla": 0,
},
)
print(res13) # >>> 3
res14 = r.zrange("racer_scores", 0, -1)
print(res14) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
res15 = r.zrangebylex("racer_scores", "[A", "[L")
print(res15) # >>> ['Castilla', 'Ford']
res16 = r.zadd("racer_scores", {"Wood": 100})
print(res16) # >>> 1
res17 = r.zadd("racer_scores", {"Henshaw": 100})
print(res17) # >>> 1
res18 = r.zadd("racer_scores", {"Henshaw": 150})
print(res18) # >>> 0
res19 = r.zincrby("racer_scores", 50, "Wood")
print(res19) # >>> 150.0
res20 = r.zincrby("racer_scores", 50, "Henshaw")
print(res20) # >>> 200.0
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' });
console.log(res1); // >>> 1
const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' });
console.log(res2); // >>> 1
const res3 = await client.zAdd('racer_scores', [
{ score: 8, value: 'Sam-Bodden' },
{ score: 10, value: 'Royce' },
{ score: 6, value: 'Ford' },
{ score: 14, value: 'Prickett' },
{ score: 12, value: 'Castilla' }
]);
console.log(res3); // >>> 4
const res4 = await client.zRange('racer_scores', 0, -1);
console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
const res6 = await client.zRangeWithScores('racer_scores', 0, -1);
console.log(res6);
// >>> [
// { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 },
// { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 },
// { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 }
// ]
const res7 = await client.zRangeByScore('racer_scores', '-inf', 10);
console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
const res8 = await client.zRem('racer_scores', 'Castilla');
console.log(res8); // >>> 1
const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9);
console.log(res9); // >>> 2
const res10 = await client.zRange('racer_scores', 0, -1);
console.log(res10); // >>> ['Norem', 'Royce', 'Prickett']
const res11 = await client.zRank('racer_scores', 'Norem');
console.log(res11); // >>> 0
const res12 = await client.zRevRank('racer_scores', 'Norem');
console.log(res12); // >>> 2
const res13 = await client.zAdd('racer_scores', [
{ score: 0, value: 'Norem' },
{ score: 0, value: 'Sam-Bodden' },
{ score: 0, value: 'Royce' },
{ score: 0, value: 'Ford' },
{ score: 0, value: 'Prickett' },
{ score: 0, value: 'Castilla' }
]);
console.log(res13); // >>> 3
const res14 = await client.zRange('racer_scores', 0, -1);
console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
const res15 = await client.zRangeByLex('racer_scores', '[A', '[L');
console.log(res15); // >>> ['Castilla', 'Ford']
const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' });
console.log(res16); // >>> 1
const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' });
console.log(res17); // >>> 1
const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true });
console.log(res18); // >>> 0
const res19 = await client.zIncrBy('racer_scores', 50, 'Wood');
console.log(res19); // >>> 150.0
const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw');
console.log(res20); // >>> 200.0
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.resps.Tuple;
public class SortedSetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.zadd("racer_scores", 10d, "Norem");
System.out.println(res1); // >>> 1
long res2 = jedis.zadd("racer_scores", 12d, "Castilla");
System.out.println(res2); // >>> 1
long res3 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Sam-Bodden", 8d);
put("Royce", 10d);
put("Ford", 6d);
put("Prickett", 14d);
put("Castilla", 12d);
}});
System.out.println(res3); // >>> 4
List<String> res4 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett]
List<String> res5 = jedis.zrevrange("racer_scores", 0, -1);
System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford]
List<Tuple> res6 = jedis.zrangeWithScores("racer_scores", 0, -1);
System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]]
List<String> res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d);
System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce]
long res8 = jedis.zrem("racer_scores", "Castilla");
System.out.println(res8); // >>> 1
long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d);
System.out.println(res9); // >>> 2
List<String> res10 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res10); // >>> [Norem, Royce, Prickett]
long res11 = jedis.zrank("racer_scores", "Norem");
System.out.println(res11); // >>> 0
long res12 = jedis.zrevrank("racer_scores", "Norem");
System.out.println(res12); // >>> 2
long res13 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Norem", 0d);
put("Sam-Bodden", 0d);
put("Royce", 0d);
put("Ford", 0d);
put("Prickett", 0d);
put("Castilla", 0d);
}});
System.out.println(res13); // >>> 3
List<String> res14 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden]
List<String> res15 = jedis.zrangeByLex("racer_scores", "[A", "[L");
System.out.println(res15); // >>> [Castilla, Ford]
long res16 = jedis.zadd("racer_scores", 100d, "Wood");
System.out.println(res16); // >>> 1
long res17 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res17); // >>> 1
long res18 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res18); // >>> 0
double res19 = jedis.zincrby("racer_scores", 50d, "Wood");
System.out.println(res19); // >>> 150.0
double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw");
System.out.println(res20); // >>> 200.0
}
}
using StackExchange.Redis;
public class SortedSetExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10);
Console.WriteLine(res1); // >>> True
bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12);
Console.WriteLine(res2); // >>> True
long res3 = db.SortedSetAdd("racer_scores", new[]{
new SortedSetEntry("Sam-Bodden", 8),
new SortedSetEntry("Royce", 10),
new SortedSetEntry("Ford", 6),
new SortedSetEntry("Prickett", 14),
new SortedSetEntry("Castilla", 12)
});
Console.WriteLine(res3); // >>> 4
RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett
RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending);
Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford
SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14
RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10);
Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce
bool res8 = db.SortedSetRemove("racer_scores", "Castilla");
Console.WriteLine(res8); // >>> True
long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9);
Console.WriteLine(res9); // >>> 2
RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett
long? res11 = db.SortedSetRank("racer_scores", "Norem");
Console.WriteLine(res11); // >>> 0
long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending);
Console.WriteLine(res12); // >>> 2
long res13 = db.SortedSetAdd("racer_scores", new[] {
new SortedSetEntry("Norem", 0),
new SortedSetEntry("Sam-Bodden", 0),
new SortedSetEntry("Royce", 0),
new SortedSetEntry("Ford", 0),
new SortedSetEntry("Prickett", 0),
new SortedSetEntry("Castilla", 0)
});
Console.WriteLine(res13); // >>> 3
RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden
RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None);
Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford
bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100);
Console.WriteLine(res16); // >>> True
bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100);
Console.WriteLine(res17); // >>> True
bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150);
Console.WriteLine(res18); // >>> False
double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50);
Console.WriteLine(res19); // >>> 150.0
double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50);
Console.WriteLine(res20); // >>> 200.0
}
}
注意:0 和 -1 表示从元素索引 0 到最后一个元素(-1 在这里的工作方式与 LRANGE
命令一样)。
可以使用 WITHSCORES
参数返回分数。
> ZRANGE racer_scores 0 -1 withscores
1) "Ford"
2) "6"
3) "Sam-Bodden"
4) "8"
5) "Norem"
6) "10"
7) "Royce"
8) "10"
9) "Castilla"
10) "12"
11) "Prickett"
12) "14"
"""
Code samples for Sorted set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sorted-sets/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.zadd("racer_scores", {"Norem": 10})
print(res1) # >>> 1
res2 = r.zadd("racer_scores", {"Castilla": 12})
print(res2) # >>> 1
res3 = r.zadd(
"racer_scores",
{"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12},
)
print(res3) # >>> 4
res4 = r.zrange("racer_scores", 0, -1)
print(res4) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
res5 = r.zrevrange("racer_scores", 0, -1)
print(res5) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford']
res6 = r.zrange("racer_scores", 0, -1, withscores=True)
print(
res6
)
# >>> [
# ('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0),
# ('Castilla', 12.0), ('Prickett', 14.0)
# ]
res7 = r.zrangebyscore("racer_scores", "-inf", 10)
print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
res8 = r.zrem("racer_scores", "Castilla")
print(res8) # >>> 1
res9 = r.zremrangebyscore("racer_scores", "-inf", 9)
print(res9) # >>> 2
res10 = r.zrange("racer_scores", 0, -1)
print(res10) # >>> ['Norem', 'Royce', 'Prickett']
res11 = r.zrank("racer_scores", "Norem")
print(res11) # >>> 0
res12 = r.zrevrank("racer_scores", "Norem")
print(res12) # >>> 2
res13 = r.zadd(
"racer_scores",
{
"Norem": 0,
"Sam-Bodden": 0,
"Royce": 0,
"Ford": 0,
"Prickett": 0,
"Castilla": 0,
},
)
print(res13) # >>> 3
res14 = r.zrange("racer_scores", 0, -1)
print(res14) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
res15 = r.zrangebylex("racer_scores", "[A", "[L")
print(res15) # >>> ['Castilla', 'Ford']
res16 = r.zadd("racer_scores", {"Wood": 100})
print(res16) # >>> 1
res17 = r.zadd("racer_scores", {"Henshaw": 100})
print(res17) # >>> 1
res18 = r.zadd("racer_scores", {"Henshaw": 150})
print(res18) # >>> 0
res19 = r.zincrby("racer_scores", 50, "Wood")
print(res19) # >>> 150.0
res20 = r.zincrby("racer_scores", 50, "Henshaw")
print(res20) # >>> 200.0
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' });
console.log(res1); // >>> 1
const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' });
console.log(res2); // >>> 1
const res3 = await client.zAdd('racer_scores', [
{ score: 8, value: 'Sam-Bodden' },
{ score: 10, value: 'Royce' },
{ score: 6, value: 'Ford' },
{ score: 14, value: 'Prickett' },
{ score: 12, value: 'Castilla' }
]);
console.log(res3); // >>> 4
const res4 = await client.zRange('racer_scores', 0, -1);
console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
const res6 = await client.zRangeWithScores('racer_scores', 0, -1);
console.log(res6);
// >>> [
// { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 },
// { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 },
// { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 }
// ]
const res7 = await client.zRangeByScore('racer_scores', '-inf', 10);
console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
const res8 = await client.zRem('racer_scores', 'Castilla');
console.log(res8); // >>> 1
const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9);
console.log(res9); // >>> 2
const res10 = await client.zRange('racer_scores', 0, -1);
console.log(res10); // >>> ['Norem', 'Royce', 'Prickett']
const res11 = await client.zRank('racer_scores', 'Norem');
console.log(res11); // >>> 0
const res12 = await client.zRevRank('racer_scores', 'Norem');
console.log(res12); // >>> 2
const res13 = await client.zAdd('racer_scores', [
{ score: 0, value: 'Norem' },
{ score: 0, value: 'Sam-Bodden' },
{ score: 0, value: 'Royce' },
{ score: 0, value: 'Ford' },
{ score: 0, value: 'Prickett' },
{ score: 0, value: 'Castilla' }
]);
console.log(res13); // >>> 3
const res14 = await client.zRange('racer_scores', 0, -1);
console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
const res15 = await client.zRangeByLex('racer_scores', '[A', '[L');
console.log(res15); // >>> ['Castilla', 'Ford']
const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' });
console.log(res16); // >>> 1
const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' });
console.log(res17); // >>> 1
const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true });
console.log(res18); // >>> 0
const res19 = await client.zIncrBy('racer_scores', 50, 'Wood');
console.log(res19); // >>> 150.0
const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw');
console.log(res20); // >>> 200.0
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.resps.Tuple;
public class SortedSetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.zadd("racer_scores", 10d, "Norem");
System.out.println(res1); // >>> 1
long res2 = jedis.zadd("racer_scores", 12d, "Castilla");
System.out.println(res2); // >>> 1
long res3 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Sam-Bodden", 8d);
put("Royce", 10d);
put("Ford", 6d);
put("Prickett", 14d);
put("Castilla", 12d);
}});
System.out.println(res3); // >>> 4
List<String> res4 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett]
List<String> res5 = jedis.zrevrange("racer_scores", 0, -1);
System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford]
List<Tuple> res6 = jedis.zrangeWithScores("racer_scores", 0, -1);
System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]]
List<String> res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d);
System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce]
long res8 = jedis.zrem("racer_scores", "Castilla");
System.out.println(res8); // >>> 1
long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d);
System.out.println(res9); // >>> 2
List<String> res10 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res10); // >>> [Norem, Royce, Prickett]
long res11 = jedis.zrank("racer_scores", "Norem");
System.out.println(res11); // >>> 0
long res12 = jedis.zrevrank("racer_scores", "Norem");
System.out.println(res12); // >>> 2
long res13 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Norem", 0d);
put("Sam-Bodden", 0d);
put("Royce", 0d);
put("Ford", 0d);
put("Prickett", 0d);
put("Castilla", 0d);
}});
System.out.println(res13); // >>> 3
List<String> res14 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden]
List<String> res15 = jedis.zrangeByLex("racer_scores", "[A", "[L");
System.out.println(res15); // >>> [Castilla, Ford]
long res16 = jedis.zadd("racer_scores", 100d, "Wood");
System.out.println(res16); // >>> 1
long res17 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res17); // >>> 1
long res18 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res18); // >>> 0
double res19 = jedis.zincrby("racer_scores", 50d, "Wood");
System.out.println(res19); // >>> 150.0
double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw");
System.out.println(res20); // >>> 200.0
}
}
using StackExchange.Redis;
public class SortedSetExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10);
Console.WriteLine(res1); // >>> True
bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12);
Console.WriteLine(res2); // >>> True
long res3 = db.SortedSetAdd("racer_scores", new[]{
new SortedSetEntry("Sam-Bodden", 8),
new SortedSetEntry("Royce", 10),
new SortedSetEntry("Ford", 6),
new SortedSetEntry("Prickett", 14),
new SortedSetEntry("Castilla", 12)
});
Console.WriteLine(res3); // >>> 4
RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett
RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending);
Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford
SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14
RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10);
Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce
bool res8 = db.SortedSetRemove("racer_scores", "Castilla");
Console.WriteLine(res8); // >>> True
long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9);
Console.WriteLine(res9); // >>> 2
RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett
long? res11 = db.SortedSetRank("racer_scores", "Norem");
Console.WriteLine(res11); // >>> 0
long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending);
Console.WriteLine(res12); // >>> 2
long res13 = db.SortedSetAdd("racer_scores", new[] {
new SortedSetEntry("Norem", 0),
new SortedSetEntry("Sam-Bodden", 0),
new SortedSetEntry("Royce", 0),
new SortedSetEntry("Ford", 0),
new SortedSetEntry("Prickett", 0),
new SortedSetEntry("Castilla", 0)
});
Console.WriteLine(res13); // >>> 3
RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden
RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None);
Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford
bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100);
Console.WriteLine(res16); // >>> True
bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100);
Console.WriteLine(res17); // >>> True
bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150);
Console.WriteLine(res18); // >>> False
double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50);
Console.WriteLine(res19); // >>> 150.0
double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50);
Console.WriteLine(res20); // >>> 200.0
}
}
对范围进行操作
排序集比这更强大。它们可以对范围进行操作。让我们获取所有得分小于等于 10 的参赛者。我们使用 ZRANGEBYSCORE
命令来完成它
> ZRANGEBYSCORE racer_scores -inf 10
1) "Ford"
2) "Sam-Bodden"
3) "Norem"
4) "Royce"
"""
Code samples for Sorted set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sorted-sets/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.zadd("racer_scores", {"Norem": 10})
print(res1) # >>> 1
res2 = r.zadd("racer_scores", {"Castilla": 12})
print(res2) # >>> 1
res3 = r.zadd(
"racer_scores",
{"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12},
)
print(res3) # >>> 4
res4 = r.zrange("racer_scores", 0, -1)
print(res4) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
res5 = r.zrevrange("racer_scores", 0, -1)
print(res5) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford']
res6 = r.zrange("racer_scores", 0, -1, withscores=True)
print(
res6
)
# >>> [
# ('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0),
# ('Castilla', 12.0), ('Prickett', 14.0)
# ]
res7 = r.zrangebyscore("racer_scores", "-inf", 10)
print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
res8 = r.zrem("racer_scores", "Castilla")
print(res8) # >>> 1
res9 = r.zremrangebyscore("racer_scores", "-inf", 9)
print(res9) # >>> 2
res10 = r.zrange("racer_scores", 0, -1)
print(res10) # >>> ['Norem', 'Royce', 'Prickett']
res11 = r.zrank("racer_scores", "Norem")
print(res11) # >>> 0
res12 = r.zrevrank("racer_scores", "Norem")
print(res12) # >>> 2
res13 = r.zadd(
"racer_scores",
{
"Norem": 0,
"Sam-Bodden": 0,
"Royce": 0,
"Ford": 0,
"Prickett": 0,
"Castilla": 0,
},
)
print(res13) # >>> 3
res14 = r.zrange("racer_scores", 0, -1)
print(res14) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
res15 = r.zrangebylex("racer_scores", "[A", "[L")
print(res15) # >>> ['Castilla', 'Ford']
res16 = r.zadd("racer_scores", {"Wood": 100})
print(res16) # >>> 1
res17 = r.zadd("racer_scores", {"Henshaw": 100})
print(res17) # >>> 1
res18 = r.zadd("racer_scores", {"Henshaw": 150})
print(res18) # >>> 0
res19 = r.zincrby("racer_scores", 50, "Wood")
print(res19) # >>> 150.0
res20 = r.zincrby("racer_scores", 50, "Henshaw")
print(res20) # >>> 200.0
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' });
console.log(res1); // >>> 1
const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' });
console.log(res2); // >>> 1
const res3 = await client.zAdd('racer_scores', [
{ score: 8, value: 'Sam-Bodden' },
{ score: 10, value: 'Royce' },
{ score: 6, value: 'Ford' },
{ score: 14, value: 'Prickett' },
{ score: 12, value: 'Castilla' }
]);
console.log(res3); // >>> 4
const res4 = await client.zRange('racer_scores', 0, -1);
console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
const res6 = await client.zRangeWithScores('racer_scores', 0, -1);
console.log(res6);
// >>> [
// { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 },
// { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 },
// { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 }
// ]
const res7 = await client.zRangeByScore('racer_scores', '-inf', 10);
console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
const res8 = await client.zRem('racer_scores', 'Castilla');
console.log(res8); // >>> 1
const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9);
console.log(res9); // >>> 2
const res10 = await client.zRange('racer_scores', 0, -1);
console.log(res10); // >>> ['Norem', 'Royce', 'Prickett']
const res11 = await client.zRank('racer_scores', 'Norem');
console.log(res11); // >>> 0
const res12 = await client.zRevRank('racer_scores', 'Norem');
console.log(res12); // >>> 2
const res13 = await client.zAdd('racer_scores', [
{ score: 0, value: 'Norem' },
{ score: 0, value: 'Sam-Bodden' },
{ score: 0, value: 'Royce' },
{ score: 0, value: 'Ford' },
{ score: 0, value: 'Prickett' },
{ score: 0, value: 'Castilla' }
]);
console.log(res13); // >>> 3
const res14 = await client.zRange('racer_scores', 0, -1);
console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
const res15 = await client.zRangeByLex('racer_scores', '[A', '[L');
console.log(res15); // >>> ['Castilla', 'Ford']
const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' });
console.log(res16); // >>> 1
const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' });
console.log(res17); // >>> 1
const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true });
console.log(res18); // >>> 0
const res19 = await client.zIncrBy('racer_scores', 50, 'Wood');
console.log(res19); // >>> 150.0
const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw');
console.log(res20); // >>> 200.0
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.resps.Tuple;
public class SortedSetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.zadd("racer_scores", 10d, "Norem");
System.out.println(res1); // >>> 1
long res2 = jedis.zadd("racer_scores", 12d, "Castilla");
System.out.println(res2); // >>> 1
long res3 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Sam-Bodden", 8d);
put("Royce", 10d);
put("Ford", 6d);
put("Prickett", 14d);
put("Castilla", 12d);
}});
System.out.println(res3); // >>> 4
List<String> res4 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett]
List<String> res5 = jedis.zrevrange("racer_scores", 0, -1);
System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford]
List<Tuple> res6 = jedis.zrangeWithScores("racer_scores", 0, -1);
System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]]
List<String> res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d);
System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce]
long res8 = jedis.zrem("racer_scores", "Castilla");
System.out.println(res8); // >>> 1
long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d);
System.out.println(res9); // >>> 2
List<String> res10 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res10); // >>> [Norem, Royce, Prickett]
long res11 = jedis.zrank("racer_scores", "Norem");
System.out.println(res11); // >>> 0
long res12 = jedis.zrevrank("racer_scores", "Norem");
System.out.println(res12); // >>> 2
long res13 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Norem", 0d);
put("Sam-Bodden", 0d);
put("Royce", 0d);
put("Ford", 0d);
put("Prickett", 0d);
put("Castilla", 0d);
}});
System.out.println(res13); // >>> 3
List<String> res14 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden]
List<String> res15 = jedis.zrangeByLex("racer_scores", "[A", "[L");
System.out.println(res15); // >>> [Castilla, Ford]
long res16 = jedis.zadd("racer_scores", 100d, "Wood");
System.out.println(res16); // >>> 1
long res17 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res17); // >>> 1
long res18 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res18); // >>> 0
double res19 = jedis.zincrby("racer_scores", 50d, "Wood");
System.out.println(res19); // >>> 150.0
double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw");
System.out.println(res20); // >>> 200.0
}
}
using StackExchange.Redis;
public class SortedSetExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10);
Console.WriteLine(res1); // >>> True
bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12);
Console.WriteLine(res2); // >>> True
long res3 = db.SortedSetAdd("racer_scores", new[]{
new SortedSetEntry("Sam-Bodden", 8),
new SortedSetEntry("Royce", 10),
new SortedSetEntry("Ford", 6),
new SortedSetEntry("Prickett", 14),
new SortedSetEntry("Castilla", 12)
});
Console.WriteLine(res3); // >>> 4
RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett
RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending);
Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford
SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14
RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10);
Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce
bool res8 = db.SortedSetRemove("racer_scores", "Castilla");
Console.WriteLine(res8); // >>> True
long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9);
Console.WriteLine(res9); // >>> 2
RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett
long? res11 = db.SortedSetRank("racer_scores", "Norem");
Console.WriteLine(res11); // >>> 0
long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending);
Console.WriteLine(res12); // >>> 2
long res13 = db.SortedSetAdd("racer_scores", new[] {
new SortedSetEntry("Norem", 0),
new SortedSetEntry("Sam-Bodden", 0),
new SortedSetEntry("Royce", 0),
new SortedSetEntry("Ford", 0),
new SortedSetEntry("Prickett", 0),
new SortedSetEntry("Castilla", 0)
});
Console.WriteLine(res13); // >>> 3
RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden
RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None);
Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford
bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100);
Console.WriteLine(res16); // >>> True
bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100);
Console.WriteLine(res17); // >>> True
bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150);
Console.WriteLine(res18); // >>> False
double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50);
Console.WriteLine(res19); // >>> 150.0
double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50);
Console.WriteLine(res20); // >>> 200.0
}
}
我们要求 Redis 返回所有分数在负无穷大到 10 之间(包括两个极端)的元素。
要删除元素,我们只需使用参赛者的姓名调用 ZREM
。还可以删除范围内的元素。让我们删除参赛者 Castilla 以及所有得分严格小于 10 的参赛者
> ZREM racer_scores "Castilla"
(integer) 1
> ZREMRANGEBYSCORE racer_scores -inf 9
(integer) 2
> ZRANGE racer_scores 0 -1
1) "Norem"
2) "Royce"
3) "Prickett"
"""
Code samples for Sorted set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sorted-sets/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.zadd("racer_scores", {"Norem": 10})
print(res1) # >>> 1
res2 = r.zadd("racer_scores", {"Castilla": 12})
print(res2) # >>> 1
res3 = r.zadd(
"racer_scores",
{"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12},
)
print(res3) # >>> 4
res4 = r.zrange("racer_scores", 0, -1)
print(res4) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
res5 = r.zrevrange("racer_scores", 0, -1)
print(res5) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford']
res6 = r.zrange("racer_scores", 0, -1, withscores=True)
print(
res6
)
# >>> [
# ('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0),
# ('Castilla', 12.0), ('Prickett', 14.0)
# ]
res7 = r.zrangebyscore("racer_scores", "-inf", 10)
print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
res8 = r.zrem("racer_scores", "Castilla")
print(res8) # >>> 1
res9 = r.zremrangebyscore("racer_scores", "-inf", 9)
print(res9) # >>> 2
res10 = r.zrange("racer_scores", 0, -1)
print(res10) # >>> ['Norem', 'Royce', 'Prickett']
res11 = r.zrank("racer_scores", "Norem")
print(res11) # >>> 0
res12 = r.zrevrank("racer_scores", "Norem")
print(res12) # >>> 2
res13 = r.zadd(
"racer_scores",
{
"Norem": 0,
"Sam-Bodden": 0,
"Royce": 0,
"Ford": 0,
"Prickett": 0,
"Castilla": 0,
},
)
print(res13) # >>> 3
res14 = r.zrange("racer_scores", 0, -1)
print(res14) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
res15 = r.zrangebylex("racer_scores", "[A", "[L")
print(res15) # >>> ['Castilla', 'Ford']
res16 = r.zadd("racer_scores", {"Wood": 100})
print(res16) # >>> 1
res17 = r.zadd("racer_scores", {"Henshaw": 100})
print(res17) # >>> 1
res18 = r.zadd("racer_scores", {"Henshaw": 150})
print(res18) # >>> 0
res19 = r.zincrby("racer_scores", 50, "Wood")
print(res19) # >>> 150.0
res20 = r.zincrby("racer_scores", 50, "Henshaw")
print(res20) # >>> 200.0
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' });
console.log(res1); // >>> 1
const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' });
console.log(res2); // >>> 1
const res3 = await client.zAdd('racer_scores', [
{ score: 8, value: 'Sam-Bodden' },
{ score: 10, value: 'Royce' },
{ score: 6, value: 'Ford' },
{ score: 14, value: 'Prickett' },
{ score: 12, value: 'Castilla' }
]);
console.log(res3); // >>> 4
const res4 = await client.zRange('racer_scores', 0, -1);
console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
const res6 = await client.zRangeWithScores('racer_scores', 0, -1);
console.log(res6);
// >>> [
// { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 },
// { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 },
// { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 }
// ]
const res7 = await client.zRangeByScore('racer_scores', '-inf', 10);
console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
const res8 = await client.zRem('racer_scores', 'Castilla');
console.log(res8); // >>> 1
const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9);
console.log(res9); // >>> 2
const res10 = await client.zRange('racer_scores', 0, -1);
console.log(res10); // >>> ['Norem', 'Royce', 'Prickett']
const res11 = await client.zRank('racer_scores', 'Norem');
console.log(res11); // >>> 0
const res12 = await client.zRevRank('racer_scores', 'Norem');
console.log(res12); // >>> 2
const res13 = await client.zAdd('racer_scores', [
{ score: 0, value: 'Norem' },
{ score: 0, value: 'Sam-Bodden' },
{ score: 0, value: 'Royce' },
{ score: 0, value: 'Ford' },
{ score: 0, value: 'Prickett' },
{ score: 0, value: 'Castilla' }
]);
console.log(res13); // >>> 3
const res14 = await client.zRange('racer_scores', 0, -1);
console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
const res15 = await client.zRangeByLex('racer_scores', '[A', '[L');
console.log(res15); // >>> ['Castilla', 'Ford']
const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' });
console.log(res16); // >>> 1
const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' });
console.log(res17); // >>> 1
const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true });
console.log(res18); // >>> 0
const res19 = await client.zIncrBy('racer_scores', 50, 'Wood');
console.log(res19); // >>> 150.0
const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw');
console.log(res20); // >>> 200.0
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.resps.Tuple;
public class SortedSetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.zadd("racer_scores", 10d, "Norem");
System.out.println(res1); // >>> 1
long res2 = jedis.zadd("racer_scores", 12d, "Castilla");
System.out.println(res2); // >>> 1
long res3 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Sam-Bodden", 8d);
put("Royce", 10d);
put("Ford", 6d);
put("Prickett", 14d);
put("Castilla", 12d);
}});
System.out.println(res3); // >>> 4
List<String> res4 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett]
List<String> res5 = jedis.zrevrange("racer_scores", 0, -1);
System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford]
List<Tuple> res6 = jedis.zrangeWithScores("racer_scores", 0, -1);
System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]]
List<String> res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d);
System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce]
long res8 = jedis.zrem("racer_scores", "Castilla");
System.out.println(res8); // >>> 1
long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d);
System.out.println(res9); // >>> 2
List<String> res10 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res10); // >>> [Norem, Royce, Prickett]
long res11 = jedis.zrank("racer_scores", "Norem");
System.out.println(res11); // >>> 0
long res12 = jedis.zrevrank("racer_scores", "Norem");
System.out.println(res12); // >>> 2
long res13 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Norem", 0d);
put("Sam-Bodden", 0d);
put("Royce", 0d);
put("Ford", 0d);
put("Prickett", 0d);
put("Castilla", 0d);
}});
System.out.println(res13); // >>> 3
List<String> res14 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden]
List<String> res15 = jedis.zrangeByLex("racer_scores", "[A", "[L");
System.out.println(res15); // >>> [Castilla, Ford]
long res16 = jedis.zadd("racer_scores", 100d, "Wood");
System.out.println(res16); // >>> 1
long res17 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res17); // >>> 1
long res18 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res18); // >>> 0
double res19 = jedis.zincrby("racer_scores", 50d, "Wood");
System.out.println(res19); // >>> 150.0
double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw");
System.out.println(res20); // >>> 200.0
}
}
using StackExchange.Redis;
public class SortedSetExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10);
Console.WriteLine(res1); // >>> True
bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12);
Console.WriteLine(res2); // >>> True
long res3 = db.SortedSetAdd("racer_scores", new[]{
new SortedSetEntry("Sam-Bodden", 8),
new SortedSetEntry("Royce", 10),
new SortedSetEntry("Ford", 6),
new SortedSetEntry("Prickett", 14),
new SortedSetEntry("Castilla", 12)
});
Console.WriteLine(res3); // >>> 4
RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett
RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending);
Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford
SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14
RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10);
Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce
bool res8 = db.SortedSetRemove("racer_scores", "Castilla");
Console.WriteLine(res8); // >>> True
long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9);
Console.WriteLine(res9); // >>> 2
RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett
long? res11 = db.SortedSetRank("racer_scores", "Norem");
Console.WriteLine(res11); // >>> 0
long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending);
Console.WriteLine(res12); // >>> 2
long res13 = db.SortedSetAdd("racer_scores", new[] {
new SortedSetEntry("Norem", 0),
new SortedSetEntry("Sam-Bodden", 0),
new SortedSetEntry("Royce", 0),
new SortedSetEntry("Ford", 0),
new SortedSetEntry("Prickett", 0),
new SortedSetEntry("Castilla", 0)
});
Console.WriteLine(res13); // >>> 3
RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden
RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None);
Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford
bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100);
Console.WriteLine(res16); // >>> True
bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100);
Console.WriteLine(res17); // >>> True
bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150);
Console.WriteLine(res18); // >>> False
double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50);
Console.WriteLine(res19); // >>> 150.0
double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50);
Console.WriteLine(res20); // >>> 200.0
}
}
ZREMRANGEBYSCORE
可能不是最好的命令名称,但它非常有用,并返回已删除元素的数量。
另一个对排序集元素定义的极其有用的操作是获取排名操作。可以询问元素在排序元素集中所处的位置。 ZREVRANK
命令也可以用于获取排名,但要考虑按降序排序的元素。
> ZRANK racer_scores "Norem"
(integer) 0
> ZREVRANK racer_scores "Norem"
(integer) 3
"""
Code samples for Sorted set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sorted-sets/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.zadd("racer_scores", {"Norem": 10})
print(res1) # >>> 1
res2 = r.zadd("racer_scores", {"Castilla": 12})
print(res2) # >>> 1
res3 = r.zadd(
"racer_scores",
{"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12},
)
print(res3) # >>> 4
res4 = r.zrange("racer_scores", 0, -1)
print(res4) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
res5 = r.zrevrange("racer_scores", 0, -1)
print(res5) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford']
res6 = r.zrange("racer_scores", 0, -1, withscores=True)
print(
res6
)
# >>> [
# ('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0),
# ('Castilla', 12.0), ('Prickett', 14.0)
# ]
res7 = r.zrangebyscore("racer_scores", "-inf", 10)
print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
res8 = r.zrem("racer_scores", "Castilla")
print(res8) # >>> 1
res9 = r.zremrangebyscore("racer_scores", "-inf", 9)
print(res9) # >>> 2
res10 = r.zrange("racer_scores", 0, -1)
print(res10) # >>> ['Norem', 'Royce', 'Prickett']
res11 = r.zrank("racer_scores", "Norem")
print(res11) # >>> 0
res12 = r.zrevrank("racer_scores", "Norem")
print(res12) # >>> 2
res13 = r.zadd(
"racer_scores",
{
"Norem": 0,
"Sam-Bodden": 0,
"Royce": 0,
"Ford": 0,
"Prickett": 0,
"Castilla": 0,
},
)
print(res13) # >>> 3
res14 = r.zrange("racer_scores", 0, -1)
print(res14) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
res15 = r.zrangebylex("racer_scores", "[A", "[L")
print(res15) # >>> ['Castilla', 'Ford']
res16 = r.zadd("racer_scores", {"Wood": 100})
print(res16) # >>> 1
res17 = r.zadd("racer_scores", {"Henshaw": 100})
print(res17) # >>> 1
res18 = r.zadd("racer_scores", {"Henshaw": 150})
print(res18) # >>> 0
res19 = r.zincrby("racer_scores", 50, "Wood")
print(res19) # >>> 150.0
res20 = r.zincrby("racer_scores", 50, "Henshaw")
print(res20) # >>> 200.0
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' });
console.log(res1); // >>> 1
const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' });
console.log(res2); // >>> 1
const res3 = await client.zAdd('racer_scores', [
{ score: 8, value: 'Sam-Bodden' },
{ score: 10, value: 'Royce' },
{ score: 6, value: 'Ford' },
{ score: 14, value: 'Prickett' },
{ score: 12, value: 'Castilla' }
]);
console.log(res3); // >>> 4
const res4 = await client.zRange('racer_scores', 0, -1);
console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
const res6 = await client.zRangeWithScores('racer_scores', 0, -1);
console.log(res6);
// >>> [
// { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 },
// { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 },
// { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 }
// ]
const res7 = await client.zRangeByScore('racer_scores', '-inf', 10);
console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
const res8 = await client.zRem('racer_scores', 'Castilla');
console.log(res8); // >>> 1
const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9);
console.log(res9); // >>> 2
const res10 = await client.zRange('racer_scores', 0, -1);
console.log(res10); // >>> ['Norem', 'Royce', 'Prickett']
const res11 = await client.zRank('racer_scores', 'Norem');
console.log(res11); // >>> 0
const res12 = await client.zRevRank('racer_scores', 'Norem');
console.log(res12); // >>> 2
const res13 = await client.zAdd('racer_scores', [
{ score: 0, value: 'Norem' },
{ score: 0, value: 'Sam-Bodden' },
{ score: 0, value: 'Royce' },
{ score: 0, value: 'Ford' },
{ score: 0, value: 'Prickett' },
{ score: 0, value: 'Castilla' }
]);
console.log(res13); // >>> 3
const res14 = await client.zRange('racer_scores', 0, -1);
console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
const res15 = await client.zRangeByLex('racer_scores', '[A', '[L');
console.log(res15); // >>> ['Castilla', 'Ford']
const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' });
console.log(res16); // >>> 1
const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' });
console.log(res17); // >>> 1
const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true });
console.log(res18); // >>> 0
const res19 = await client.zIncrBy('racer_scores', 50, 'Wood');
console.log(res19); // >>> 150.0
const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw');
console.log(res20); // >>> 200.0
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.resps.Tuple;
public class SortedSetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.zadd("racer_scores", 10d, "Norem");
System.out.println(res1); // >>> 1
long res2 = jedis.zadd("racer_scores", 12d, "Castilla");
System.out.println(res2); // >>> 1
long res3 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Sam-Bodden", 8d);
put("Royce", 10d);
put("Ford", 6d);
put("Prickett", 14d);
put("Castilla", 12d);
}});
System.out.println(res3); // >>> 4
List<String> res4 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett]
List<String> res5 = jedis.zrevrange("racer_scores", 0, -1);
System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford]
List<Tuple> res6 = jedis.zrangeWithScores("racer_scores", 0, -1);
System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]]
List<String> res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d);
System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce]
long res8 = jedis.zrem("racer_scores", "Castilla");
System.out.println(res8); // >>> 1
long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d);
System.out.println(res9); // >>> 2
List<String> res10 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res10); // >>> [Norem, Royce, Prickett]
long res11 = jedis.zrank("racer_scores", "Norem");
System.out.println(res11); // >>> 0
long res12 = jedis.zrevrank("racer_scores", "Norem");
System.out.println(res12); // >>> 2
long res13 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Norem", 0d);
put("Sam-Bodden", 0d);
put("Royce", 0d);
put("Ford", 0d);
put("Prickett", 0d);
put("Castilla", 0d);
}});
System.out.println(res13); // >>> 3
List<String> res14 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden]
List<String> res15 = jedis.zrangeByLex("racer_scores", "[A", "[L");
System.out.println(res15); // >>> [Castilla, Ford]
long res16 = jedis.zadd("racer_scores", 100d, "Wood");
System.out.println(res16); // >>> 1
long res17 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res17); // >>> 1
long res18 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res18); // >>> 0
double res19 = jedis.zincrby("racer_scores", 50d, "Wood");
System.out.println(res19); // >>> 150.0
double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw");
System.out.println(res20); // >>> 200.0
}
}
using StackExchange.Redis;
public class SortedSetExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10);
Console.WriteLine(res1); // >>> True
bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12);
Console.WriteLine(res2); // >>> True
long res3 = db.SortedSetAdd("racer_scores", new[]{
new SortedSetEntry("Sam-Bodden", 8),
new SortedSetEntry("Royce", 10),
new SortedSetEntry("Ford", 6),
new SortedSetEntry("Prickett", 14),
new SortedSetEntry("Castilla", 12)
});
Console.WriteLine(res3); // >>> 4
RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett
RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending);
Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford
SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14
RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10);
Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce
bool res8 = db.SortedSetRemove("racer_scores", "Castilla");
Console.WriteLine(res8); // >>> True
long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9);
Console.WriteLine(res9); // >>> 2
RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett
long? res11 = db.SortedSetRank("racer_scores", "Norem");
Console.WriteLine(res11); // >>> 0
long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending);
Console.WriteLine(res12); // >>> 2
long res13 = db.SortedSetAdd("racer_scores", new[] {
new SortedSetEntry("Norem", 0),
new SortedSetEntry("Sam-Bodden", 0),
new SortedSetEntry("Royce", 0),
new SortedSetEntry("Ford", 0),
new SortedSetEntry("Prickett", 0),
new SortedSetEntry("Castilla", 0)
});
Console.WriteLine(res13); // >>> 3
RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden
RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None);
Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford
bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100);
Console.WriteLine(res16); // >>> True
bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100);
Console.WriteLine(res17); // >>> True
bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150);
Console.WriteLine(res18); // >>> False
double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50);
Console.WriteLine(res19); // >>> 150.0
double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50);
Console.WriteLine(res20); // >>> 200.0
}
}
字典序分数
在 Redis 2.8 版本中,引入了一项新功能,允许在字典序上获取范围,假设排序集中所有元素都使用相同的分数插入(元素使用 C memcmp
函数进行比较,因此保证没有整理,并且每个 Redis 实例都将以相同的输出进行回复)。
用于操作字典序范围的主要命令是 ZRANGEBYLEX
、ZREVRANGEBYLEX
、ZREMRANGEBYLEX
和 ZLEXCOUNT
。
例如,让我们再次添加著名黑客列表,但这次对所有元素使用零分数。我们将看到,由于排序集排序规则,它们已经按字典序排序。使用 ZRANGEBYLEX
,我们可以要求字典序范围
> ZADD racer_scores 0 "Norem" 0 "Sam-Bodden" 0 "Royce" 0 "Castilla" 0 "Prickett" 0 "Ford"
(integer) 3
> ZRANGE racer_scores 0 -1
1) "Castilla"
2) "Ford"
3) "Norem"
4) "Prickett"
5) "Royce"
6) "Sam-Bodden"
> ZRANGEBYLEX racer_scores [A [L
1) "Castilla"
2) "Ford"
"""
Code samples for Sorted set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sorted-sets/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.zadd("racer_scores", {"Norem": 10})
print(res1) # >>> 1
res2 = r.zadd("racer_scores", {"Castilla": 12})
print(res2) # >>> 1
res3 = r.zadd(
"racer_scores",
{"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12},
)
print(res3) # >>> 4
res4 = r.zrange("racer_scores", 0, -1)
print(res4) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
res5 = r.zrevrange("racer_scores", 0, -1)
print(res5) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford']
res6 = r.zrange("racer_scores", 0, -1, withscores=True)
print(
res6
)
# >>> [
# ('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0),
# ('Castilla', 12.0), ('Prickett', 14.0)
# ]
res7 = r.zrangebyscore("racer_scores", "-inf", 10)
print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
res8 = r.zrem("racer_scores", "Castilla")
print(res8) # >>> 1
res9 = r.zremrangebyscore("racer_scores", "-inf", 9)
print(res9) # >>> 2
res10 = r.zrange("racer_scores", 0, -1)
print(res10) # >>> ['Norem', 'Royce', 'Prickett']
res11 = r.zrank("racer_scores", "Norem")
print(res11) # >>> 0
res12 = r.zrevrank("racer_scores", "Norem")
print(res12) # >>> 2
res13 = r.zadd(
"racer_scores",
{
"Norem": 0,
"Sam-Bodden": 0,
"Royce": 0,
"Ford": 0,
"Prickett": 0,
"Castilla": 0,
},
)
print(res13) # >>> 3
res14 = r.zrange("racer_scores", 0, -1)
print(res14) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
res15 = r.zrangebylex("racer_scores", "[A", "[L")
print(res15) # >>> ['Castilla', 'Ford']
res16 = r.zadd("racer_scores", {"Wood": 100})
print(res16) # >>> 1
res17 = r.zadd("racer_scores", {"Henshaw": 100})
print(res17) # >>> 1
res18 = r.zadd("racer_scores", {"Henshaw": 150})
print(res18) # >>> 0
res19 = r.zincrby("racer_scores", 50, "Wood")
print(res19) # >>> 150.0
res20 = r.zincrby("racer_scores", 50, "Henshaw")
print(res20) # >>> 200.0
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' });
console.log(res1); // >>> 1
const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' });
console.log(res2); // >>> 1
const res3 = await client.zAdd('racer_scores', [
{ score: 8, value: 'Sam-Bodden' },
{ score: 10, value: 'Royce' },
{ score: 6, value: 'Ford' },
{ score: 14, value: 'Prickett' },
{ score: 12, value: 'Castilla' }
]);
console.log(res3); // >>> 4
const res4 = await client.zRange('racer_scores', 0, -1);
console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
const res6 = await client.zRangeWithScores('racer_scores', 0, -1);
console.log(res6);
// >>> [
// { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 },
// { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 },
// { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 }
// ]
const res7 = await client.zRangeByScore('racer_scores', '-inf', 10);
console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
const res8 = await client.zRem('racer_scores', 'Castilla');
console.log(res8); // >>> 1
const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9);
console.log(res9); // >>> 2
const res10 = await client.zRange('racer_scores', 0, -1);
console.log(res10); // >>> ['Norem', 'Royce', 'Prickett']
const res11 = await client.zRank('racer_scores', 'Norem');
console.log(res11); // >>> 0
const res12 = await client.zRevRank('racer_scores', 'Norem');
console.log(res12); // >>> 2
const res13 = await client.zAdd('racer_scores', [
{ score: 0, value: 'Norem' },
{ score: 0, value: 'Sam-Bodden' },
{ score: 0, value: 'Royce' },
{ score: 0, value: 'Ford' },
{ score: 0, value: 'Prickett' },
{ score: 0, value: 'Castilla' }
]);
console.log(res13); // >>> 3
const res14 = await client.zRange('racer_scores', 0, -1);
console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
const res15 = await client.zRangeByLex('racer_scores', '[A', '[L');
console.log(res15); // >>> ['Castilla', 'Ford']
const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' });
console.log(res16); // >>> 1
const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' });
console.log(res17); // >>> 1
const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true });
console.log(res18); // >>> 0
const res19 = await client.zIncrBy('racer_scores', 50, 'Wood');
console.log(res19); // >>> 150.0
const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw');
console.log(res20); // >>> 200.0
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.resps.Tuple;
public class SortedSetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.zadd("racer_scores", 10d, "Norem");
System.out.println(res1); // >>> 1
long res2 = jedis.zadd("racer_scores", 12d, "Castilla");
System.out.println(res2); // >>> 1
long res3 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Sam-Bodden", 8d);
put("Royce", 10d);
put("Ford", 6d);
put("Prickett", 14d);
put("Castilla", 12d);
}});
System.out.println(res3); // >>> 4
List<String> res4 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett]
List<String> res5 = jedis.zrevrange("racer_scores", 0, -1);
System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford]
List<Tuple> res6 = jedis.zrangeWithScores("racer_scores", 0, -1);
System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]]
List<String> res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d);
System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce]
long res8 = jedis.zrem("racer_scores", "Castilla");
System.out.println(res8); // >>> 1
long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d);
System.out.println(res9); // >>> 2
List<String> res10 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res10); // >>> [Norem, Royce, Prickett]
long res11 = jedis.zrank("racer_scores", "Norem");
System.out.println(res11); // >>> 0
long res12 = jedis.zrevrank("racer_scores", "Norem");
System.out.println(res12); // >>> 2
long res13 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{
put("Norem", 0d);
put("Sam-Bodden", 0d);
put("Royce", 0d);
put("Ford", 0d);
put("Prickett", 0d);
put("Castilla", 0d);
}});
System.out.println(res13); // >>> 3
List<String> res14 = jedis.zrange("racer_scores", 0, -1);
System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden]
List<String> res15 = jedis.zrangeByLex("racer_scores", "[A", "[L");
System.out.println(res15); // >>> [Castilla, Ford]
long res16 = jedis.zadd("racer_scores", 100d, "Wood");
System.out.println(res16); // >>> 1
long res17 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res17); // >>> 1
long res18 = jedis.zadd("racer_scores", 100d, "Henshaw");
System.out.println(res18); // >>> 0
double res19 = jedis.zincrby("racer_scores", 50d, "Wood");
System.out.println(res19); // >>> 150.0
double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw");
System.out.println(res20); // >>> 200.0
}
}
using StackExchange.Redis;
public class SortedSetExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10);
Console.WriteLine(res1); // >>> True
bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12);
Console.WriteLine(res2); // >>> True
long res3 = db.SortedSetAdd("racer_scores", new[]{
new SortedSetEntry("Sam-Bodden", 8),
new SortedSetEntry("Royce", 10),
new SortedSetEntry("Ford", 6),
new SortedSetEntry("Prickett", 14),
new SortedSetEntry("Castilla", 12)
});
Console.WriteLine(res3); // >>> 4
RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett
RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending);
Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford
SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14
RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10);
Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce
bool res8 = db.SortedSetRemove("racer_scores", "Castilla");
Console.WriteLine(res8); // >>> True
long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9);
Console.WriteLine(res9); // >>> 2
RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett
long? res11 = db.SortedSetRank("racer_scores", "Norem");
Console.WriteLine(res11); // >>> 0
long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending);
Console.WriteLine(res12); // >>> 2
long res13 = db.SortedSetAdd("racer_scores", new[] {
new SortedSetEntry("Norem", 0),
new SortedSetEntry("Sam-Bodden", 0),
new SortedSetEntry("Royce", 0),
new SortedSetEntry("Ford", 0),
new SortedSetEntry("Prickett", 0),
new SortedSetEntry("Castilla", 0)
});
Console.WriteLine(res13); // >>> 3
RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden
RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None);
Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford
bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100);
Console.WriteLine(res16); // >>> True
bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100);
Console.WriteLine(res17); // >>> True
bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150);
Console.WriteLine(res18); // >>> False
double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50);
Console.WriteLine(res19); // >>> 150.0
double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50);
Console.WriteLine(res20); // >>> 200.0
}
}
范围可以是包含的或排除的(取决于第一个字符),字符串无穷大和小无穷大分别用 +
和 -
字符串指定。有关更多信息,请参阅文档。
此功能很重要,因为它允许我们将排序集用作通用索引。例如,如果您要按 128 位无符号整数参数索引元素,您只需将元素添加到排序集中,并使用相同的分数(例如 0),但使用 16 字节前缀,该前缀由 **128 位数字的大端序** 组成。由于大端序数字在字典序(以原始字节顺序)上排序时实际上也是按数字排序的,因此您可以在 128 位空间中要求范围,并获取元素的值,丢弃前缀。
更新分数:排行榜
在切换到下一个主题之前,关于排序集的最后一点说明。排序集的分数可以在任何时候更新。只需对排序集中已包含的元素调用 ZADD
,就会以 O(log(N)) 时间复杂度更新其分数(和位置)。因此,排序集适用于更新频繁的情况。
由于这种特性,排行榜是一个常见的用例。典型的应用是 Facebook 游戏,您可以在其中结合获取按最高分排序的用户的功能和获取排名操作,以便显示前 N 个用户以及用户在排行榜中的排名(例如,“您是这里排名第 4932 的最佳得分”)。
示例
- 我们可以用两种方式使用排序集来表示排行榜。如果我们知道参赛者的新分数,我们可以通过
ZADD
命令直接更新它。但是,如果我们想在现有分数上添加分数,我们可以使用ZINCRBY
命令。> ZADD racer_scores 100 "Wood" (integer) 1 > ZADD racer_scores 100 "Henshaw" (integer) 1 > ZADD racer_scores 150 "Henshaw" (integer) 0 > ZINCRBY racer_scores 50 "Wood" "150" > ZINCRBY racer_scores 50 "Henshaw" "200"
您是否厌倦了使用 redis-cli?试试 Redis Insight - Redis 的开发者 GUI。""" Code samples for Sorted set doc pages: https://redis.ac.cn/docs/latest/develop/data-types/sorted-sets/ """ import redis r = redis.Redis(decode_responses=True) res1 = r.zadd("racer_scores", {"Norem": 10}) print(res1) # >>> 1 res2 = r.zadd("racer_scores", {"Castilla": 12}) print(res2) # >>> 1 res3 = r.zadd( "racer_scores", {"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12}, ) print(res3) # >>> 4 res4 = r.zrange("racer_scores", 0, -1) print(res4) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett'] res5 = r.zrevrange("racer_scores", 0, -1) print(res5) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford'] res6 = r.zrange("racer_scores", 0, -1, withscores=True) print( res6 ) # >>> [ # ('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0), # ('Castilla', 12.0), ('Prickett', 14.0) # ] res7 = r.zrangebyscore("racer_scores", "-inf", 10) print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce'] res8 = r.zrem("racer_scores", "Castilla") print(res8) # >>> 1 res9 = r.zremrangebyscore("racer_scores", "-inf", 9) print(res9) # >>> 2 res10 = r.zrange("racer_scores", 0, -1) print(res10) # >>> ['Norem', 'Royce', 'Prickett'] res11 = r.zrank("racer_scores", "Norem") print(res11) # >>> 0 res12 = r.zrevrank("racer_scores", "Norem") print(res12) # >>> 2 res13 = r.zadd( "racer_scores", { "Norem": 0, "Sam-Bodden": 0, "Royce": 0, "Ford": 0, "Prickett": 0, "Castilla": 0, }, ) print(res13) # >>> 3 res14 = r.zrange("racer_scores", 0, -1) print(res14) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden'] res15 = r.zrangebylex("racer_scores", "[A", "[L") print(res15) # >>> ['Castilla', 'Ford'] res16 = r.zadd("racer_scores", {"Wood": 100}) print(res16) # >>> 1 res17 = r.zadd("racer_scores", {"Henshaw": 100}) print(res17) # >>> 1 res18 = r.zadd("racer_scores", {"Henshaw": 150}) print(res18) # >>> 0 res19 = r.zincrby("racer_scores", 50, "Wood") print(res19) # >>> 150.0 res20 = r.zincrby("racer_scores", 50, "Henshaw") print(res20) # >>> 200.0
import assert from 'assert'; import { createClient } from 'redis'; const client = createClient(); await client.connect(); const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' }); console.log(res1); // >>> 1 const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' }); console.log(res2); // >>> 1 const res3 = await client.zAdd('racer_scores', [ { score: 8, value: 'Sam-Bodden' }, { score: 10, value: 'Royce' }, { score: 6, value: 'Ford' }, { score: 14, value: 'Prickett' }, { score: 12, value: 'Castilla' } ]); console.log(res3); // >>> 4 const res4 = await client.zRange('racer_scores', 0, -1); console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett'] const res6 = await client.zRangeWithScores('racer_scores', 0, -1); console.log(res6); // >>> [ // { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 }, // { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 }, // { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 } // ] const res7 = await client.zRangeByScore('racer_scores', '-inf', 10); console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce'] const res8 = await client.zRem('racer_scores', 'Castilla'); console.log(res8); // >>> 1 const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9); console.log(res9); // >>> 2 const res10 = await client.zRange('racer_scores', 0, -1); console.log(res10); // >>> ['Norem', 'Royce', 'Prickett'] const res11 = await client.zRank('racer_scores', 'Norem'); console.log(res11); // >>> 0 const res12 = await client.zRevRank('racer_scores', 'Norem'); console.log(res12); // >>> 2 const res13 = await client.zAdd('racer_scores', [ { score: 0, value: 'Norem' }, { score: 0, value: 'Sam-Bodden' }, { score: 0, value: 'Royce' }, { score: 0, value: 'Ford' }, { score: 0, value: 'Prickett' }, { score: 0, value: 'Castilla' } ]); console.log(res13); // >>> 3 const res14 = await client.zRange('racer_scores', 0, -1); console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden'] const res15 = await client.zRangeByLex('racer_scores', '[A', '[L'); console.log(res15); // >>> ['Castilla', 'Ford'] const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' }); console.log(res16); // >>> 1 const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' }); console.log(res17); // >>> 1 const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true }); console.log(res18); // >>> 0 const res19 = await client.zIncrBy('racer_scores', 50, 'Wood'); console.log(res19); // >>> 150.0 const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw'); console.log(res20); // >>> 200.0
package io.redis.examples; import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.resps.Tuple; public class SortedSetsExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.zadd("racer_scores", 10d, "Norem"); System.out.println(res1); // >>> 1 long res2 = jedis.zadd("racer_scores", 12d, "Castilla"); System.out.println(res2); // >>> 1 long res3 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{ put("Sam-Bodden", 8d); put("Royce", 10d); put("Ford", 6d); put("Prickett", 14d); put("Castilla", 12d); }}); System.out.println(res3); // >>> 4 List<String> res4 = jedis.zrange("racer_scores", 0, -1); System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett] List<String> res5 = jedis.zrevrange("racer_scores", 0, -1); System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford] List<Tuple> res6 = jedis.zrangeWithScores("racer_scores", 0, -1); System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]] List<String> res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d); System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce] long res8 = jedis.zrem("racer_scores", "Castilla"); System.out.println(res8); // >>> 1 long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d); System.out.println(res9); // >>> 2 List<String> res10 = jedis.zrange("racer_scores", 0, -1); System.out.println(res10); // >>> [Norem, Royce, Prickett] long res11 = jedis.zrank("racer_scores", "Norem"); System.out.println(res11); // >>> 0 long res12 = jedis.zrevrank("racer_scores", "Norem"); System.out.println(res12); // >>> 2 long res13 = jedis.zadd("racer_scores", new HashMap<String,Double>() {{ put("Norem", 0d); put("Sam-Bodden", 0d); put("Royce", 0d); put("Ford", 0d); put("Prickett", 0d); put("Castilla", 0d); }}); System.out.println(res13); // >>> 3 List<String> res14 = jedis.zrange("racer_scores", 0, -1); System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden] List<String> res15 = jedis.zrangeByLex("racer_scores", "[A", "[L"); System.out.println(res15); // >>> [Castilla, Ford] long res16 = jedis.zadd("racer_scores", 100d, "Wood"); System.out.println(res16); // >>> 1 long res17 = jedis.zadd("racer_scores", 100d, "Henshaw"); System.out.println(res17); // >>> 1 long res18 = jedis.zadd("racer_scores", 100d, "Henshaw"); System.out.println(res18); // >>> 0 double res19 = jedis.zincrby("racer_scores", 50d, "Wood"); System.out.println(res19); // >>> 150.0 double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw"); System.out.println(res20); // >>> 200.0 } }
using StackExchange.Redis; public class SortedSetExample { public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10); Console.WriteLine(res1); // >>> True bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12); Console.WriteLine(res2); // >>> True long res3 = db.SortedSetAdd("racer_scores", new[]{ new SortedSetEntry("Sam-Bodden", 8), new SortedSetEntry("Royce", 10), new SortedSetEntry("Ford", 6), new SortedSetEntry("Prickett", 14), new SortedSetEntry("Castilla", 12) }); Console.WriteLine(res3); // >>> 4 RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1); Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending); Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1); Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14 RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10); Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce bool res8 = db.SortedSetRemove("racer_scores", "Castilla"); Console.WriteLine(res8); // >>> True long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9); Console.WriteLine(res9); // >>> 2 RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1); Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett long? res11 = db.SortedSetRank("racer_scores", "Norem"); Console.WriteLine(res11); // >>> 0 long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending); Console.WriteLine(res12); // >>> 2 long res13 = db.SortedSetAdd("racer_scores", new[] { new SortedSetEntry("Norem", 0), new SortedSetEntry("Sam-Bodden", 0), new SortedSetEntry("Royce", 0), new SortedSetEntry("Ford", 0), new SortedSetEntry("Prickett", 0), new SortedSetEntry("Castilla", 0) }); Console.WriteLine(res13); // >>> 3 RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1); Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None); Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100); Console.WriteLine(res16); // >>> True bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100); Console.WriteLine(res17); // >>> True bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150); Console.WriteLine(res18); // >>> False double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50); Console.WriteLine(res19); // >>> 150.0 double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50); Console.WriteLine(res20); // >>> 200.0 } }
您将看到,当成员已存在(分数已更新)时,ZADD
返回 0,而 ZINCRBY
返回新分数。参赛者 Henshaw 的分数从 100 变成了 150,不考虑之前的分数是什么,然后又增加了 50,变成了 200。
基本命令
ZADD
将新成员及其关联的分数添加到排序集。如果成员已存在,则更新分数。ZRANGE
返回排序集中成员,按给定范围内排序。ZRANK
返回所提供成员的排名,假设排序集按升序排序。ZREVRANK
返回所提供成员的排名,假设排序集按降序排序。
查看 排序集命令的完整列表。
性能
大多数排序集操作为 O(log(n)),其中 n 是成员数。
在使用 ZRANGE
命令返回大量结果时(例如,数万个或更多),请注意。此命令的时间复杂度为 O(log(n) + m),其中 m 是返回的结果数。
替代方案
Redis 排序集有时用于索引其他 Redis 数据结构。如果您需要索引和查询数据,请考虑 JSON 数据类型和 搜索和查询 功能。
了解更多
- Redis 排序集解释 是对 Redis 中排序集的引人入胜的介绍。
- Redis 大学的 RU101 详细探讨了 Redis 排序集。