コード例 #1
0
        public void SortedSet_Ordered()
        {
            var    cacheKey = Guid.NewGuid().ToString();
            IRedis redis    = ServiceStackRedis.Default;

            var random = new Random();
            var list   = Enumerable.Repeat(0, 4).Select(r => random.Next(100)).ToList();

            list.ForEach(i => redis.SortedSetAdd(cacheKey, i.ToString(), (double)i));

            var list1 = redis.SortedSetRangeByRank(cacheKey, order: Order.Ascending);

            Assert.AreEqual(list1.Length, list.Count);

            var array1 = list.ToArray();

            Array.Sort(array1);
            for (int i = 0; i < list1.Length; i++)
            {
                Assert.AreEqual((Int32)list1[i], array1[i]);
            }

            var list2 = redis.SortedSetRangeByRank(cacheKey, order: Order.Descending);

            Assert.AreEqual(list2.Length, list.Count);

            var array2 = array1.Reverse().ToArray();

            for (int i = 0; i < list2.Length; i++)
            {
                Assert.AreEqual((Int32)list2[i], array2[i]);
            }
        }
コード例 #2
0
        public void SortedSetRange()
        {
            var    cacheKey = Guid.NewGuid().ToString();
            IRedis redis    = ServiceStackRedis.Default;

            var random = new Random();
            var list   = Enumerable.Repeat(0, 20).Select(r => random.Next(100)).Distinct().ToList();

            for (int i = 0; i < list.Count; i++)
            {
                redis.SortedSetAdd(cacheKey, list[i].ToString(), i);
                var len = redis.SortedSetLength(cacheKey);
                Assert.AreEqual(len, i + 1);
            }

            var values = redis.SortedSetRangeByRank(cacheKey, 0, -1);

            Assert.AreEqual(values.Length, list.Count);
            for (int i = 0; i < list.Count; i++)
            {
                Assert.AreEqual((String)values[i], list[i].ToString());
            }

            for (int i = 0; i < 3; i++)
            {
                var index1 = random.Next(list.Count);
                var index2 = redis.SortedSetRank(cacheKey, list[index1].ToString());
                Assert.AreEqual(index1, index2);
            }

            for (int i = 0; i < 3; i++)
            {
                var index = random.Next(list.Count);
                var value = list[index];
                list.RemoveAt(index);
                var removed = redis.SortedSetRemove(cacheKey, value.ToString());
                Assert.IsTrue(removed > 0);
                var len = redis.SortedSetLength(cacheKey);
                Assert.AreEqual(len, list.Count);
            }

            Assert.IsTrue(redis.SortedSetLength(cacheKey) > 3);
            var removedByRank = redis.SortedSetRemoveRangeByRank(cacheKey, 0, 2);

            Assert.AreEqual(removedByRank, 3);
        }