示例#1
0
        public static void Main(string[] args)
        {
            using (var redis = new RedisClient()) {
                // Performance to Test
                // Adding full list to redis
                // Get all indexes as a time table
                // want to see how time changes for different items in the list

                redis.FlushDb();
                var data = new MockedData(300000);

                var list = new RedisList(Guid.NewGuid().ToString(), redis, data);
                var set  = new RedisSortedSet(Guid.NewGuid().ToString(), redis, data);

                Console.WriteLine("List Testing");
                PerformanceTesting.TimeOperation("Insert all data into a list", list.AddAllRecords);
                PerformanceTesting.TimeOperation("Lookup all records sequentially - one by one", list.LookupRecordsSequentially);

                Console.WriteLine("++++++++++============================================++++++++++");

                Console.WriteLine("Sorted Set Testing");
                PerformanceTesting.TimeOperation("Insert all data into a sortedset", set.AddAllRecords);
                PerformanceTesting.TimeOperation("Lookup all records sequentially - one by one", set.LookupRecordsSequentially);
            }
        }
        /// <summary>
        ///     初始化数据库环境、实例化子类中,所有Set属性
        /// </summary>
        public void Initializer()
        {
            if (IsInitializer)
            {
                return;
            }
            _client       = new RedisClient(_redisConnection);
            IsInitializer = true;

            Connection  = new RedisConnection(_client);
            Hash        = new RedisHash(_client);
            Key         = new RedisKey(_client);
            List        = new RedisList(_client);
            PubSub      = new RedisPubSub(_client);
            Script      = new RedisScript(_client);
            Server      = new RedisServer(_client);
            Set         = new RedisSet(_client);
            SortedSet   = new RedisSortedSet(_client);
            String      = new RedisString(_client);
            Transaction = new RedisTransaction(_client);
            Bit         = new RedisBit(_client);
            Expire      = new RedisExpire(_client);
            Sort        = new RedisSort(_client);
            Number      = new RedisNumber(_client);
        }
示例#3
0
        public void SortedSet_Normal()
        {
            var rkey = "zset_test";

            // 删除已有
            _redis.Remove(rkey);

            var zset = new RedisSortedSet(_redis, rkey);
            var list = new SortedList <Double, String>();

            // 插入数据
            for (var i = 0; i < 17; i++)
            {
                var key   = Rand.NextString(8);
                var score = Rand.Next() / 1000d;

                list.Add(score, key);
                var rs = zset.Add(key, score);
                Assert.True(rs);
            }

            Assert.Equal(list.Count, zset.Count);

            var ks = list.Keys.ToList();
            var vs = list.Values.ToList();

            // 删除
            {
                list.Remove(ks[0]);

                var rs = zset.Remove(vs[0]);
                Assert.True(rs);

                Assert.Equal(list.Count, zset.Count);

                ks.RemoveAt(0);
                vs.RemoveAt(0);
            }

            // 取值
            {
                var key   = vs[0];
                var score = ks[0];

                Assert.Equal(score, zset.GetScore(key));
            }

            // 最小两个
            {
                var keys = zset.Range(0, 1);
                Assert.Equal(vs[0], keys[0]);
                Assert.Equal(vs[1], keys[1]);
            }

            // 最大三个
            {
                var keys = zset.Range(-2, -1);
                Assert.Equal(vs[^ 1], keys[1]);
        public async Task SortedSetRangeByRankWithScore()
        {
            var set = new RedisSortedSet<string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);
            await set.Add("d", 10000);
            await set.Add("b", 100);
            await set.Add("f", 1000000);
            await set.Add("e", 100000);
            await set.Add("c", 1000);

            var range = await set.RangeByRankWithScoresAndRank();
            range.Select(x => x.Value).Is("a", "b", "c", "d", "e", "f");
            range.Select(x => x.Score).Is(10, 100, 1000, 10000, 100000, 1000000);
            range.Select(x => x.Rank).Is(0, 1, 2, 3, 4, 5);

            range = await set.RangeByRankWithScoresAndRank(start: 3); // 3 to last
            range.Select(x => x.Rank).Is(3, 4, 5);

            range = await set.RangeByRankWithScoresAndRank(start: 4, stop: 2);
            range.Length.Is(0);

            range = await set.RangeByRankWithScoresAndRank(order: Order.Descending);
            range.Select(x => x.Value).Is("f", "e", "d", "c", "b", "a");
            range.Select(x => x.Score).Is(1000000, 100000, 10000, 1000, 100, 10);
            range.Select(x => x.Rank).Is(0, 1, 2, 3, 4, 5);

            range = await set.RangeByRankWithScoresAndRank(start: 3, order: Order.Descending); // 3 to last
            range.Select(x => x.Value).Is("c", "b", "a");
            range.Select(x => x.Rank).Is(3, 4, 5);

            range = await set.RangeByRankWithScoresAndRank(start: 2, stop: 4, order: Order.Descending); // 2 to 4
            range.Select(x => x.Value).Is("d", "c", "b");
            range.Select(x => x.Rank).Is(2, 3, 4);

            // start < 0

            range = await set.RangeByRankWithScoresAndRank(start: -1, stop: -1, order: Order.Ascending); // last to last
            range.Select(x => x.Value).Is("f");
            range.Select(x => x.Score).Is(1000000);
            range.Select(x => x.Rank).Is(5);

            range = await set.RangeByRankWithScoresAndRank(start: -3, stop: -2, order: Order.Ascending);
            range.Select(x => x.Value).Is("d", "e");
            range.Select(x => x.Rank).Is(3, 4);

            range = await set.RangeByRankWithScoresAndRank(start: -2, stop: 5, order: Order.Descending);
            range.Select(x => x.Value).Is("b", "a");
            range.Select(x => x.Rank).Is(4, 5);



            range = await set.RangeByRankWithScoresAndRank(start: -100, stop: -120, order: Order.Descending);
            range.Length.Is(0);

        }
        public async Task SortedSetAddRange()
        {
            var set = new RedisSortedSet<string>(GlobalSettings.Default, "set");
            await set.Delete();

            (await set.Add(new[] { new KeyValuePair<string, double>("hogehoge", 10), new KeyValuePair<string, double>("huga", 100) })).Is(2);
            (await set.Add(new[] { new KeyValuePair<string, double>("huga", 100), new KeyValuePair<string, double>("hugahuga", 1000) })).Is(1);

            var tako = await set.RangeByRank();
            (await set.RangeByRank()).Is("hogehoge", "huga", "hugahuga");
            (await set.RangeByRank(order: Order.Descending)).Is("hugahuga", "huga", "hogehoge");
        }
        public async Task SortedSetAdd()
        {
            var set = new RedisSortedSet <string>(GlobalSettings.Default, "set");
            await set.Delete();

            (await set.Add("hogehoge", 10)).Is(true);
            (await set.Add("hogehoge", 100)).Is(false); // already added but updated
            (await set.Add("huga", 1)).Is(true);

            (await set.RangeByRank()).Is("huga", "hogehoge");
            (await set.RangeByRank(order: Order.Descending)).Is("hogehoge", "huga");
        }
        public async Task SortedSetAdd()
        {
            var set = new RedisSortedSet<string>(GlobalSettings.Default, "set");
            await set.Delete();

            (await set.Add("hogehoge", 10)).Is(true);
            (await set.Add("hogehoge", 100)).Is(false); // already added but updated
            (await set.Add("huga", 1)).Is(true);

            (await set.RangeByRank()).Is("huga", "hogehoge");
            (await set.RangeByRank(order: Order.Descending)).Is("hogehoge", "huga");
        }
示例#8
0
        public override void Init(int flowCount, long flowRecordCount)
        {
            clients   = new RedisTypedClient <KeyValuePair <long, TickRecord> > [flowCount];
            sortedSet = new RedisSortedSet();

            for (int i = 0; i < flowCount; i++)
            {
                clients[i] = new RedisTypedClient <KeyValuePair <long, TickRecord> >(new RedisClient(Server, Port));
            }

            clients[0].FlushAll();// Remove all keys from all databases
        }
        public async Task SortedSetAddRange()
        {
            var set = new RedisSortedSet <string>(GlobalSettings.Default, "set");
            await set.Delete();

            (await set.Add(new[] { new KeyValuePair <string, double>("hogehoge", 10), new KeyValuePair <string, double>("huga", 100) })).Is(2);
            (await set.Add(new[] { new KeyValuePair <string, double>("huga", 100), new KeyValuePair <string, double>("hugahuga", 1000) })).Is(1);

            var tako = await set.RangeByRank();

            (await set.RangeByRank()).Is("hogehoge", "huga", "hugahuga");
            (await set.RangeByRank(order: Order.Descending)).Is("hugahuga", "huga", "hogehoge");
        }
        public async Task Remove()
        {
            var set = new RedisSortedSet <string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);

            await set.Add("b", 100);

            await set.Add("c", 1000);

            await set.Add("d", 10000);

            await set.Add("e", 100000);

            await set.Add("f", 1000000);

            var c = await set.Get("c");

            c.Value.Is("c");
            c.Rank.Is(2);
            c.Score.Is(1000);
            (await set.Remove("c")).IsTrue();
            (await set.Remove("c")).IsFalse();
            (await set.Get("c")).IsNull();

            await set.Add("c", 1000);

            (await set.RemoveRangeByRank(2, 3)).Is(2);
            (await set.RangeByRank()).Is("a", "b", "e", "f");

            await set.Delete();

            await set.Add("a", 10);

            await set.Add("b", 100);

            await set.Add("c", 1000);

            await set.Add("d", 10000);

            await set.Add("e", 100000);

            await set.Add("f", 1000000);

            (await set.RemoveRangeByScore(100, 10000)).Is(3);
            (await set.RangeByRank()).Is("a", "e", "f");
        }
        public async Task RangeByScore()
        {
            var set = new RedisSortedSet <string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);

            await set.Add("b", 100);

            await set.Add("c", 1000);

            await set.Add("d", 10000);

            await set.Add("e", 100000);

            await set.Add("f", 1000000);

            (await set.RangeByScore(100, 10000, exclude: Exclude.None)).Is("b", "c", "d");
            (await set.RangeByScore(100, 10000, exclude: Exclude.Start)).Is("c", "d");
            (await set.RangeByScore(100, 10000, exclude: Exclude.Stop)).Is("b", "c");
            (await set.RangeByScore(100, 10000, exclude: Exclude.Both)).Is("c");

            (await set.RangeByScore(100, 10000, exclude: Exclude.None, order: Order.Descending)).Is("d", "c", "b");
            var hoge = await set.RangeByScore(100, 10000, exclude : Exclude.Start, order : Order.Descending);

            (await set.RangeByScore(100, 10000, exclude: Exclude.Start, order: Order.Descending)).Is("d", "c");
            (await set.RangeByScore(100, 10000, exclude: Exclude.Stop, order: Order.Descending)).Is("c", "b");

            (await set.RangeByScore(100, 10000, skip: 1)).Is("c", "d");
            (await set.RangeByScore(100, 10000, skip: 2)).Is("d");
            (await set.RangeByScore(100, 10000, take: 2)).Is("b", "c");
            (await set.RangeByScore(100, 10000, skip: 1, take: 1)).Is("c");

            var r = await set.RangeByScoreWithScores(100, 10000);

            r.Select(x => x.Value).Is("b", "c", "d");
            r.Select(x => x.Score).Is(100, 1000, 10000);

            // length
            (await set.Length()).Is(6);
            (await set.Length(100, 10000)).Is(3);
            (await set.Length(100, 10000, Exclude.Both)).Is(1);
        }
        public async Task RangeByValue()
        {
            var set = new RedisSortedSet <string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);

            await set.Add("b", 100);

            await set.Add("c", 1000);

            await set.Add("d", 10000);

            await set.Add("e", 100000);

            await set.Add("f", 1000000);

            (await set.RangeByValue("b", "d", exclude: Exclude.None)).Is("b", "c", "d");
        }
        /// <summary>
        ///     初始化数据库环境、实例化子类中,所有Set属性
        /// </summary>
        public void Initializer()
        {
            if (IsInitializer) { return; }
            _client = new RedisClient(_redisConnection);
            IsInitializer = true;

            Connection = new RedisConnection(_client);
            Hash = new RedisHash(_client);
            Key = new RedisKey(_client);
            List = new RedisList(_client);
            PubSub = new RedisPubSub(_client);
            Script = new RedisScript(_client);
            Server = new RedisServer(_client);
            Set = new RedisSet(_client);
            SortedSet = new RedisSortedSet(_client);
            String = new RedisString(_client);
            Transaction = new RedisTransaction(_client);
            Bit = new RedisBit(_client);
            Expire = new RedisExpire(_client);
            Sort = new RedisSort(_client);
            Number = new RedisNumber(_client);
        }
        public async Task RankScore()
        {
            var set = new RedisSortedSet <string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);

            await set.Add("b", 100);

            await set.Add("c", 1000);

            await set.Add("d", 10000);

            await set.Add("e", 100000);

            await set.Add("f", 1000000);

            (await set.Rank("a")).Is(0);
            (await set.Rank("b")).Is(1);
            (await set.Rank("c")).Is(2);
            (await set.Rank("d")).Is(3);
            (await set.Rank("e")).Is(4);
            (await set.Rank("f")).Is(5);
            (await set.Rank("f", Order.Descending)).Is(0);
            (await set.Rank("e", Order.Descending)).Is(1);
            (await set.Rank("d", Order.Descending)).Is(2);
            (await set.Rank("c", Order.Descending)).Is(3);
            (await set.Rank("b", Order.Descending)).Is(4);
            (await set.Rank("a", Order.Descending)).Is(5);

            (await set.Rank("z")).IsNull();

            (await set.Score("a")).Is(10);
            (await set.Score("b")).Is(100);
            (await set.Score("c")).Is(1000);
            (await set.Score("d")).Is(10000);
            (await set.Score("e")).Is(100000);
            (await set.Score("f")).Is(1000000);
        }
        public async Task Incr()
        {
            var set = new RedisSortedSet <string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);

            (await set.Increment("a", 1)).Is(11);
            (await set.Increment("a", 100)).Is(111);

            (await set.Decrement("a", 100)).Is(11);
            (await set.Decrement("a", 1)).Is(10);

            (await set.Decrement("a", -1)).Is(11);
            (await set.Decrement("a", -100)).Is(111);

            (await set.Increment("a", -100)).Is(11);
            (await set.Increment("a", -1)).Is(10);

            (await set.IncrementLimitByMax("a", 30, 50)).Is(40);
            (await set.IncrementLimitByMax("a", 30, 50)).Is(50);
            (await set.IncrementLimitByMin("a", -30, 5)).Is(20);
            (await set.IncrementLimitByMin("a", -30, 5)).Is(5);
        }
        public async Task SortedSetRangeByRankWithScore()
        {
            var set = new RedisSortedSet <string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);

            await set.Add("d", 10000);

            await set.Add("b", 100);

            await set.Add("f", 1000000);

            await set.Add("e", 100000);

            await set.Add("c", 1000);

            var range = await set.RangeByRankWithScoresAndRank();

            range.Select(x => x.Value).Is("a", "b", "c", "d", "e", "f");
            range.Select(x => x.Score).Is(10, 100, 1000, 10000, 100000, 1000000);
            range.Select(x => x.Rank).Is(0, 1, 2, 3, 4, 5);

            range = await set.RangeByRankWithScoresAndRank(start : 3); // 3 to last

            range.Select(x => x.Rank).Is(3, 4, 5);

            range = await set.RangeByRankWithScoresAndRank(start : 4, stop : 2);

            range.Length.Is(0);

            range = await set.RangeByRankWithScoresAndRank(order : Order.Descending);

            range.Select(x => x.Value).Is("f", "e", "d", "c", "b", "a");
            range.Select(x => x.Score).Is(1000000, 100000, 10000, 1000, 100, 10);
            range.Select(x => x.Rank).Is(0, 1, 2, 3, 4, 5);

            range = await set.RangeByRankWithScoresAndRank(start : 3, order : Order.Descending); // 3 to last

            range.Select(x => x.Value).Is("c", "b", "a");
            range.Select(x => x.Rank).Is(3, 4, 5);

            range = await set.RangeByRankWithScoresAndRank(start : 2, stop : 4, order : Order.Descending); // 2 to 4

            range.Select(x => x.Value).Is("d", "c", "b");
            range.Select(x => x.Rank).Is(2, 3, 4);

            // start < 0

            range = await set.RangeByRankWithScoresAndRank(start : -1, stop : -1, order : Order.Ascending); // last to last

            range.Select(x => x.Value).Is("f");
            range.Select(x => x.Score).Is(1000000);
            range.Select(x => x.Rank).Is(5);

            range = await set.RangeByRankWithScoresAndRank(start : -3, stop : -2, order : Order.Ascending);

            range.Select(x => x.Value).Is("d", "e");
            range.Select(x => x.Rank).Is(3, 4);

            range = await set.RangeByRankWithScoresAndRank(start : -2, stop : 5, order : Order.Descending);

            range.Select(x => x.Value).Is("b", "a");
            range.Select(x => x.Rank).Is(4, 5);



            range = await set.RangeByRankWithScoresAndRank(start : -100, stop : -120, order : Order.Descending);

            range.Length.Is(0);
        }
        public async Task RangeByScore()
        {
            var set = new RedisSortedSet<string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);
            await set.Add("b", 100);
            await set.Add("c", 1000);
            await set.Add("d", 10000);
            await set.Add("e", 100000);
            await set.Add("f", 1000000);

            (await set.RangeByScore(100, 10000, exclude: Exclude.None)).Is("b", "c", "d");
            (await set.RangeByScore(100, 10000, exclude: Exclude.Start)).Is("c", "d");
            (await set.RangeByScore(100, 10000, exclude: Exclude.Stop)).Is("b", "c");
            (await set.RangeByScore(100, 10000, exclude: Exclude.Both)).Is("c");

            (await set.RangeByScore(100, 10000, exclude: Exclude.None, order: Order.Descending)).Is("d", "c", "b");
            var hoge = await set.RangeByScore(100, 10000, exclude: Exclude.Start, order: Order.Descending);
            (await set.RangeByScore(100, 10000, exclude: Exclude.Start, order: Order.Descending)).Is("d", "c");
            (await set.RangeByScore(100, 10000, exclude: Exclude.Stop, order: Order.Descending)).Is("c", "b");

            (await set.RangeByScore(100, 10000, skip: 1)).Is("c", "d");
            (await set.RangeByScore(100, 10000, skip: 2)).Is("d");
            (await set.RangeByScore(100, 10000, take: 2)).Is("b", "c");
            (await set.RangeByScore(100, 10000, skip: 1, take: 1)).Is("c");

            var r = await set.RangeByScoreWithScores(100, 10000);
            r.Select(x => x.Value).Is("b", "c", "d");
            r.Select(x => x.Score).Is(100, 1000, 10000);

            // length
            (await set.Length()).Is(6);
            (await set.Length(100, 10000)).Is(3);
            (await set.Length(100, 10000, Exclude.Both)).Is(1);
        }
        public async Task RangeByValue()
        {
            var set = new RedisSortedSet<string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);
            await set.Add("b", 100);
            await set.Add("c", 1000);
            await set.Add("d", 10000);
            await set.Add("e", 100000);
            await set.Add("f", 1000000);

            (await set.RangeByValue("b", "d", exclude: Exclude.None)).Is("b", "c", "d");
        }
        public async Task Incr()
        {
            var set = new RedisSortedSet<string>(GlobalSettings.Default, "set");
            await set.Delete();

            await set.Add("a", 10);

            (await set.Increment("a", 1)).Is(11);
            (await set.Increment("a", 100)).Is(111);

            (await set.Decrement("a", 100)).Is(11);
            (await set.Decrement("a", 1)).Is(10);

            (await set.Decrement("a", -1)).Is(11);
            (await set.Decrement("a", -100)).Is(111);

            (await set.Increment("a", -100)).Is(11);
            (await set.Increment("a", -1)).Is(10);

            (await set.IncrementLimitByMax("a", 30, 50)).Is(40);
            (await set.IncrementLimitByMax("a", 30, 50)).Is(50);
            (await set.IncrementLimitByMin("a", -30, 5)).Is(20);
            (await set.IncrementLimitByMin("a", -30, 5)).Is(5);
        }
        public async Task RankScore()
        {
            var set = new RedisSortedSet<string>(GlobalSettings.Default, "set");
            await set.Delete();
            await set.Add("a", 10);
            await set.Add("b", 100);
            await set.Add("c", 1000);
            await set.Add("d", 10000);
            await set.Add("e", 100000);
            await set.Add("f", 1000000);

            (await set.Rank("a")).Is(0);
            (await set.Rank("b")).Is(1);
            (await set.Rank("c")).Is(2);
            (await set.Rank("d")).Is(3);
            (await set.Rank("e")).Is(4);
            (await set.Rank("f")).Is(5);
            (await set.Rank("f", Order.Descending)).Is(0);
            (await set.Rank("e", Order.Descending)).Is(1);
            (await set.Rank("d", Order.Descending)).Is(2);
            (await set.Rank("c", Order.Descending)).Is(3);
            (await set.Rank("b", Order.Descending)).Is(4);
            (await set.Rank("a", Order.Descending)).Is(5);

            (await set.Rank("z")).IsNull();

            (await set.Score("a")).Is(10);
            (await set.Score("b")).Is(100);
            (await set.Score("c")).Is(1000);
            (await set.Score("d")).Is(10000);
            (await set.Score("e")).Is(100000);
            (await set.Score("f")).Is(1000000);
        }
        public async Task Remove()
        {
            var set = new RedisSortedSet<string>(GlobalSettings.Default, "set");
            await set.Delete();
            await set.Add("a", 10);
            await set.Add("b", 100);
            await set.Add("c", 1000);
            await set.Add("d", 10000);
            await set.Add("e", 100000);
            await set.Add("f", 1000000);

            var c = await set.Get("c");
            c.Value.Is("c");
            c.Rank.Is(2);
            c.Score.Is(1000);
            (await set.Remove("c")).IsTrue();
            (await set.Remove("c")).IsFalse();
            (await set.Get("c")).IsNull();

            await set.Add("c", 1000);
            (await set.RemoveRangeByRank(2, 3)).Is(2);
            (await set.RangeByRank()).Is("a", "b", "e", "f");

            await set.Delete();
            await set.Add("a", 10);
            await set.Add("b", 100);
            await set.Add("c", 1000);
            await set.Add("d", 10000);
            await set.Add("e", 100000);
            await set.Add("f", 1000000);
            (await set.RemoveRangeByScore(100, 10000)).Is(3);
            (await set.RangeByRank()).Is("a", "e", "f");
        }
示例#22
0
 ///<summary>
 /// 构造方法
 ///</summary>
 public ReadOnlyTimestempSet(IDatabase database, string setKey) : base(database, setKey, RedisType.SortedSet)
 {
     this.SortedSet = new RedisSortedSet <TKey>(database, setKey);
 }