Esempio n. 1
0
        public async Task SortedSetTestAsync()
        {
            const string key = "top10";

            Assert.True(await _redis.SortedSetAddAsync(key, "colin", 8));
            var score0 = await _redis.SortedSetIncrementAsync(key, "colin", 1);

            var score1 = await _redis.SortedSetDecrementAsync(key, "colin", 1);

            Assert.Equal(1, score0 - score1);

            Assert.True(await _redis.SortedSetAddAsync(key, "robin", 6));
            Assert.True(await _redis.SortedSetAddAsync(key, "tom", 7));
            Assert.True(await _redis.SortedSetAddAsync(key, "bob", 5));
            Assert.True(await _redis.SortedSetAddAsync(key, "elle", 5));
            Assert.True(await _redis.SortedSetAddAsync(key, "helen", 5));

            //返回排名前五,无论分数多少
            var top5 = await _redis.SortedSetRangeByRankWithScoresAsync(key, 0, 4, Order.Descending);

            foreach (var(k, v) in top5)
            {
                _testOutputHelper.WriteLine($"{k}\t{v}");
            }

            _testOutputHelper.WriteLine("---------------");

            //返回6-10分之间前五
            var highScore =
                await _redis.SortedSetRangeByScoreWithScoresAsync(key, 6, 10, order : Order.Descending, take : 5);

            foreach (var(k, v) in highScore)
            {
                _testOutputHelper.WriteLine($"{k}\t{v}");
            }

            await _redis.KeyDeleteAsync(new[] { key });
        }