Increment() public method

public Increment ( string key ) : int
key string
return int
Example #1
0
        public void IncrementWorks()
        {
            Redis redis = new Redis();

            redis.Set("increment", "2");
            int increment = redis.Increment("increment");

            Assert.AreEqual(3, increment);
        }
Example #2
0
        public void Clear_should_ensure_generation_if_another_cache_has_already_incremented_the_generation()
        {
            // Arrange
            var cache = new RedisCache("region", this.ClientManager);

            // Another cache updated its generation (by clearing).
            Redis.Increment(cache.CacheNamespace.GetGenerationKey(), 100);

            // Act
            cache.Clear();

            // Assert
            Assert.Equal(102, cache.CacheNamespace.GetGeneration());
        }
Example #3
0
        public void Put_should_retry_until_generation_matches_the_server()
        {
            // Arrange
            var cache = new RedisCache("region", this.ClientManager);

            // Another client incremented the generation.
            Redis.Increment(cache.CacheNamespace.GetGenerationKey(), 100);

            // Act
            cache.Put(999, new Person("Foo", 10));

            // Assert
            Assert.Equal(cache.CacheNamespace.GetGeneration(), 101);
            var data   = RedisNative.Get(cache.CacheNamespace.GlobalCacheKey(999));
            var person = (Person)serializer.Deserialize(data);

            Assert.Equal("Foo", person.Name);
            Assert.Equal(10, person.Age);
        }
Example #4
0
        public void Remove_should_retry_until_generation_matches_the_server()
        {
            // Arrange
            var cache1 = new RedisCache("region", this.ClientManager);

            // Another client incremented the generation.
            Redis.Increment(cache1.CacheNamespace.GetGenerationKey(), 100);
            var cache2 = new RedisCache("region", this.ClientManager);

            cache2.Put(999, new Person("Foo", 10));

            // Act
            cache1.Remove(999);

            // Assert
            Assert.Equal(101, cache1.CacheNamespace.GetGeneration());
            var result = Redis.GetValue(cache1.CacheNamespace.GlobalCacheKey(999));

            Assert.Null(result);
        }
Example #5
0
        public void Get_should_retry_until_generation_matches_the_server()
        {
            // Arrange
            var cache1 = new RedisCache("region", this.ClientManager);

            // Another client incremented the generation.
            Redis.Increment(cache1.CacheNamespace.GetGenerationKey(), 100);
            var cache2 = new RedisCache("region", this.ClientManager);

            cache2.Put(999, new Person("Foo", 10));

            // Act
            var person = cache1.Get(999) as Person;

            // Assert
            Assert.Equal(101, cache1.CacheNamespace.GetGeneration());
            Assert.NotNull(person);
            Assert.Equal("Foo", person.Name);
            Assert.Equal(10, person.Age);
        }
        public void VerifyPerformanceImprovement()
        {
            int asyncTimer, sync, op = 0, asyncFaF, syncFaF;
            using (var muxer= Config.GetUnsecuredConnection())
            {
                // do these outside the timings, just to ensure the core methods are JITted etc
                for (int db = 0; db < 5; db++)
                {
                    muxer.GetDatabase(db).KeyDeleteAsync("perftest");
                }

                var timer = Stopwatch.StartNew();
                for (int i = 0; i < 100; i++)
                {
                    // want to test multiplex scenario; test each db, but to make it fair we'll
                    // do in batches of 10 on each
                    for (int db = 0; db < 5; db++)
                    {
                        var conn = muxer.GetDatabase(db);
                        for (int j = 0; j < 10; j++)
                            conn.StringIncrementAsync("perftest");
                    }
                }
                asyncFaF = (int)timer.ElapsedMilliseconds;
                Task<RedisValue>[] final = new Task<RedisValue>[5];
                for (int db = 0; db < 5; db++)
                    final[db] = muxer.GetDatabase(db).StringGetAsync("perftest");
                muxer.WaitAll(final);
                timer.Stop();
                asyncTimer = (int)timer.ElapsedMilliseconds;
                Console.WriteLine("async to completion (local): {0}ms", timer.ElapsedMilliseconds);
                for (int db = 0; db < 5; db++)
                    Assert.AreEqual(1000, (long)final[db].Result, "async, db:" + db);
            }

            using (var conn = new Redis(Config.LocalHost, 6379))
            {
                // do these outside the timings, just to ensure the core methods are JITted etc
                for (int db = 0; db < 5; db++)
                {
                    conn.Db = db;
                    conn.Remove("perftest");
                }

                var timer = Stopwatch.StartNew();
                for (int i = 0; i < 100; i++)
                {
                    // want to test multiplex scenario; test each db, but to make it fair we'll
                    // do in batches of 10 on each
                    for (int db = 0; db < 5; db++)
                    {
                        conn.Db = db;
                        op++;
                        for (int j = 0; j < 10; j++)
                        {
                            conn.Increment("perftest");
                            op++;
                        }
                    }
                }
                syncFaF = (int)timer.ElapsedMilliseconds;
                string[] final = new string[5];
                for (int db = 0; db < 5; db++)
                {
                    conn.Db = db;
                    final[db] = Encoding.ASCII.GetString(conn.Get("perftest"));
                }
                timer.Stop();
                sync = (int)timer.ElapsedMilliseconds;
                Console.WriteLine("sync to completion (local): {0}ms", timer.ElapsedMilliseconds);
                for (int db = 0; db < 5; db++)
                    Assert.AreEqual("1000", final[db], "async, db:" + db);
            }
            int effectiveAsync = ((10 * asyncTimer) + 3) / 10;
            int effectiveSync = ((10 * sync) + (op * 3)) / 10;
            Console.WriteLine("async to completion with assumed 0.3ms LAN latency: " + effectiveAsync);
            Console.WriteLine("sync to completion with assumed 0.3ms LAN latency: " + effectiveSync);
            Console.WriteLine("fire-and-forget: {0}ms sync vs {1}ms async ", syncFaF, asyncFaF);
            Assert.Less(effectiveAsync, effectiveSync, "Everything");
            Assert.Less(asyncFaF, syncFaF, "Fire and Forget");
        }
Example #7
0
        public void VerifyPerformanceImprovement()
        {
            int asyncTimer, sync, op = 0, asyncFaF, syncFaF;

            using (var conn = Config.GetUnsecuredConnection())
            {
                // do these outside the timings, just to ensure the core methods are JITted etc
                for (int db = 0; db < 5; db++)
                {
                    conn.Keys.Remove(db, "perftest");
                }

                var timer = Stopwatch.StartNew();
                for (int i = 0; i < 100; i++)
                {
                    // want to test multiplex scenario; test each db, but to make it fair we'll
                    // do in batches of 10 on each
                    for (int db = 0; db < 5; db++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            conn.Strings.Increment(db, "perftest");
                        }
                    }
                }
                asyncFaF = (int)timer.ElapsedMilliseconds;
                Task <string>[] final = new Task <string> [5];
                for (int db = 0; db < 5; db++)
                {
                    final[db] = conn.Strings.GetString(db, "perftest");
                }
                conn.WaitAll(final);
                timer.Stop();
                asyncTimer = (int)timer.ElapsedMilliseconds;
                Console.WriteLine("async to completion (local): {0}ms", timer.ElapsedMilliseconds);
                for (int db = 0; db < 5; db++)
                {
                    Assert.AreEqual("1000", final[db].Result, "async, db:" + db);
                }
            }
            using (var conn = new Redis("127.0.0.1", 6379))
            {
                // do these outside the timings, just to ensure the core methods are JITted etc
                for (int db = 0; db < 5; db++)
                {
                    conn.Db = db;
                    conn.Remove("perftest");
                }

                var timer = Stopwatch.StartNew();
                for (int i = 0; i < 100; i++)
                {
                    // want to test multiplex scenario; test each db, but to make it fair we'll
                    // do in batches of 10 on each
                    for (int db = 0; db < 5; db++)
                    {
                        conn.Db = db;
                        op++;
                        for (int j = 0; j < 10; j++)
                        {
                            conn.Increment("perftest");
                            op++;
                        }
                    }
                }
                syncFaF = (int)timer.ElapsedMilliseconds;
                string[] final = new string[5];
                for (int db = 0; db < 5; db++)
                {
                    conn.Db   = db;
                    final[db] = Encoding.ASCII.GetString(conn.Get("perftest"));
                }
                timer.Stop();
                sync = (int)timer.ElapsedMilliseconds;
                Console.WriteLine("sync to completion (local): {0}ms", timer.ElapsedMilliseconds);
                for (int db = 0; db < 5; db++)
                {
                    Assert.AreEqual("1000", final[db], "async, db:" + db);
                }
            }
            int effectiveAsync = ((10 * asyncTimer) + 3) / 10;
            int effectiveSync  = ((10 * sync) + (op * 3)) / 10;

            Console.WriteLine("async to completion with assumed 0.3ms LAN latency: " + effectiveAsync);
            Console.WriteLine("sync to completion with assumed 0.3ms LAN latency: " + effectiveSync);
            Console.WriteLine("fire-and-forget: {0}ms sync vs {1}ms async ", syncFaF, asyncFaF);
            Assert.Less(effectiveAsync, effectiveSync, "Everything");
            Assert.Less(asyncFaF, syncFaF, "Fire and Forget");
        }
Example #8
0
        static void Main(string[] args)
        {
            //create a default connection with localhost host and 6379 port
            Redis redis = new Redis();

            //set a foo key with bar value
            redis.Set("foo", "bar");

            bool expire = redis.Expire("foo", 300); // timeout expire after 300s

            Console.WriteLine(expire);              //true

            int ttl = redis.TimeToLive("foo");

            Console.WriteLine(ttl); // 300

            bool persist = redis.Persist("foo");

            Console.WriteLine(persist); // true

            bool rename = redis.Rename("foo", "faa");

            Console.WriteLine(rename); //true

            //get the value of foo key
            string foo = redis.GetString("faa");

            Console.WriteLine(foo); //bar

            //delete the foo key
            bool delete = redis.Delete("faa");

            Console.WriteLine(delete); // true

            //set a key increment with 2 as value
            redis.Set("increment", "2");

            //increment the key "increment"
            int increment = redis.Increment("increment");

            Console.WriteLine(increment); // 3

            //Delete increment;
            redis.Delete("increment");

            //push John Doe to list names and return the length of the list
            int length = redis.RPush("names", "John Doe");

            Console.WriteLine(length); // 1;

            //Push Mr X at the first position of names
            redis.LPush("names", "Mr X");

            //Get the length of a list
            int namesLength = redis.LLen("names");

            Console.WriteLine(String.Format("The length of names is : {0}", namesLength));

            //search for the position of a element in a key.
            int position = redis.LPos("names", "John Doe");

            Console.WriteLine(position);

            //delete the list
            redis.Delete("names");

            //new list examples with 3 John & 1 Foo
            redis.RPush("examples", "John");
            redis.RPush("examples", "John");
            redis.RPush("examples", "Foo");
            redis.RPush("examples", "John");

            //delete 2 John in examples
            int removedElements = redis.LRem("examples", 2, "John");

            Console.WriteLine(String.Format("count of removed elements : {0}", removedElements));

            //and get the length of the list after.
            int examplesLength = redis.LLen("examples");

            Console.WriteLine(String.Format("The length of examples : {0}", examplesLength));

            //Dispose
            redis.Dispose();

            //wait until the end
            Console.ReadKey();
        }