Example #1
0
        static void Test4(RedisConnection redisConnection)
        {
            var mpSerializer = new MsgPackRedisSerializer();

            try
            {
                var db = redisConnection.GetDatabase();
                db.PingAsync().Wait();

                var list = new RedisList <string>(db, "test", mpSerializer);
                list.Clear();

                list.Add("one");
                list.Add("two");
                list.Add("three");

                list[1] = "Ross";


                foreach (var str in list)
                {
                    Console.WriteLine(str);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Example #2
0
        static void SerializerTests(RedisConnection redisConnection)
        {
            try
            {
                var json = File.ReadAllText($"{AppDomain.CurrentDomain.BaseDirectory}sample.json");

                var myData = new MySerializeData
                {
                    Data1 = json,
                    Data2 = json,
                    List  = new List <string>
                    {
                        json,
                        json,
                        json,
                        json
                    }
                };

                var myData2 = new MySerializeData2
                {
                    Data = json,
                    List = new List <TestClass>(),
                };

                for (var i = 0; i < 10; i++)
                {
                    myData2.List.Add(new TestClass
                    {
                        Id   = i,
                        Name = $"Ross: {i}"
                    });
                }

                var jsonSerializer = new JsonRedisSerializer();
                var mpSerializer   = new MsgPackRedisSerializer();

                //var r1 = jsonSerializer.Serialize(myData2);
                //var r2 = mpSerializer.Serialize(myData2);

                //var db = redisConnection.GetDatabase();
                //var list = new XRedisList<MySerializeData>(db, "test", mpSerializer);
                //list.Clear();

                //list.Add(myData);
                //list.Add(myData);
                //list.Add(myData);

                const int Iteration = 1000;

                while (true)
                {
                    Console.WriteLine("Ready...");

                    var key = Console.ReadKey();
                    Console.WriteLine();

                    if (key.KeyChar == 'q')
                    {
                        break;
                    }
                    else if (key.KeyChar == 'c')
                    {
                        Console.Clear();
                    }
                    else if (key.KeyChar == 'j')
                    {
                        var db   = redisConnection.GetDatabase();
                        var list = new RedisList <MySerializeData>(db, "test", jsonSerializer);
                        list.Clear();

                        var sw = new Stopwatch();
                        sw.Start();
                        for (var i = 0; i < Iteration; i++)
                        {
                            list.Add(myData);
                            var d = list[0];
                        }
                        sw.Stop();
                        Console.WriteLine($"{sw.ElapsedMilliseconds} msec for {Iteration} iterations using Json serializer");
                    }
                    else if (key.KeyChar == 'm')
                    {
                        var db   = redisConnection.GetDatabase();
                        var list = new RedisList <MySerializeData>(db, "test", mpSerializer);
                        list.Clear();

                        var sw = new Stopwatch();
                        sw.Start();
                        for (var i = 0; i < Iteration; i++)
                        {
                            list.Add(myData);
                            var d = list[0];
                        }
                        sw.Stop();
                        Console.WriteLine($"{sw.ElapsedMilliseconds} msec for {Iteration} iterations using MsgPack serializer");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Example #3
0
        static public async Task RunAsync()
        {
            // string connectionString = "127.0.0.1:30001,127.0.0.1:30002,127.0.0.1:30003,127.0.0.1:30004,127.0.0.1:30005,127.0.0.1:30006";
            string connectionStrings = "localhost:6379";

            var configuration = new ConfigurationOptions
            {
                AbortOnConnectFail = false
            };

            try
            {
                using (var redisConnection = new RedisConnection(connectionStrings, configuration))
                {
                    var d     = new RedisDictionary <string, Crap>(redisConnection.GetDatabase(), "test", new JsonRedisSerializer()); // new MsgPackRedisSerializer());
                    var dtest = new Dictionary <int, string>();
                    dtest.Add(1, "Ross");
                    dtest.Add(2, "Tammy");

                    var v1 = await GetAsync(d, "age", name => 2);
                    await SetAsync(d, "age", (v1 + 2));

                    v1 = await GetAsync(d, "age", name => 3);

                    var v2 = await GetAsync(d, "dtest", name => dtest);

                    v2 = await GetAsync(d, "dtest", name => dtest);



                    ////var v1 = await GetAsync(d, "age", name => (decimal)2);
                    ////await SetAsync(d, "age", (v1 + 2));
                    ////v1 = await GetAsync(d, "age", name => (decimal)3);

                    //return;

                    var lockAcquisition = new RedisCacheKeyLockAcquisition();
                    var cacheContainer  = new StringRedisCacheContainer(redisConnection, "RedisCache"); // new HashRedisCacheContainer(redisConnection);
                    var cacheSeriizer   = new MsgPackRedisSerializer();                                 // new JsonRedisSerializer();
                    var cache           = new RedisCache(redisConnection, lockAcquisition, cacheContainer, cacheSeriizer);

                    var xxx = await cache.GetAsync("app:app:00000000-0000-0000-0000-0000000000a1", async (name, args) =>
                    {
                        var app = new AppEntity
                        {
                            AppId            = Guid.NewGuid(),
                            CreatedDateTime  = DateTime.Now,
                            ModifiedDateTime = DateTime.Now,
                            Description      = "Hello",
                            Enabled          = true,
                            Name             = "test",
                            TenantId         = Guid.Empty,
                        };

                        await Task.Delay(0);

                        return(new CacheDataInfo <AppEntity>
                        {
                            KeepAlive = TimeSpan.FromHours(1),
                            NewCacheData = app, //"Hello, Ross"
                        });

                        // return (CacheDataInfo<string>)null;
                    });

                    // cache.Remove("test");


                    //CacheThreadTest(redisConnection);
                    //LockThreadTest(redisConnection);
                    //SerializerTests(redisConnection);
                    //Test4(redisConnection);
                    //Test1(redisConnection);
                    //Test3(redisConnection);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }