Exemple #1
0
 public async Task <T> GetCacheAsync <T>(string cacheKey) where T : class
 {
     try {
         return((await Cache.StringGetAsync(cacheKey)) as T);
     }
     catch (Exception) {
         throw new CacheException("An error occured while reading the cache.");
     }
 }
Exemple #2
0
        static void Main(string[] args)
        {
            RedisHelper.Initialization(new CSRedis.CSRedisClient("127.0.0.1:6379,database=1,asyncPipeline=true,preheat=100,poolsize=100"));
            cli.Set("TestMGet_null1", "");
            RedisHelper.Set("TestMGet_null1", "");
            sedb.StringSet("TestMGet_string1", String);
            ThreadPool.SetMinThreads(10001, 10001);
            Stopwatch sw    = new Stopwatch();
            var       tasks = new List <Task>();

            cli.FlushAll();


            sw.Reset();
            sw.Start();
            for (var a = 0; a < 100000; a++)
            {
                var tmp = Guid.NewGuid().ToString();
                sedb.StringSet(tmp, String);
                var val = sedb.StringGet(tmp);
                if (val != String)
                {
                    throw new Exception("not equal");
                }
            }
            sw.Stop();
            Console.WriteLine("StackExchange(0-100000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 100000; a++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    sedb.StringSet(tmp, String);
                    var val = sedb.StringGet(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("StackExchange(Task.WaitAll 100000): " + sw.ElapsedMilliseconds + "ms");
            tasks.Clear();

            sw.Reset();
            sw.Start();
            Task.Run(async() =>
            {
                for (var a = 0; a < 100000; a++)
                {
                    var tmp = Guid.NewGuid().ToString();
                    await sedb.StringSetAsync(tmp, String);
                    var val = await sedb.StringGetAsync(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }
            }).Wait();
            sw.Stop();
            Console.WriteLine("StackExchangeAsync(0-100000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 100000; a++)
            {
                tasks.Add(Task.Run(async() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    await sedb.StringSetAsync(tmp, String);
                    var val = await sedb.StringGetAsync(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("StackExchangeAsync(Task.WaitAll 100000): " + sw.ElapsedMilliseconds + "ms\r\n");
            tasks.Clear();


            sw.Reset();
            sw.Start();
            for (var a = 0; a < 100000; a++)
            {
                var tmp = Guid.NewGuid().ToString();
                cli.Set(tmp, String);
                var val = cli.Get(tmp);
                if (val != String)
                {
                    throw new Exception("not equal");
                }
            }
            sw.Stop();
            Console.WriteLine("FreeRedis(0-100000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 100000; a++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    cli.Set(tmp, String);
                    var val = cli.Get(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("FreeRedis(Task.WaitAll 100000): " + sw.ElapsedMilliseconds + "ms");
            tasks.Clear();

            sw.Reset();
            sw.Start();
            Task.Run(async() =>
            {
                for (var a = 0; a < 100000; a++)
                {
                    var tmp = Guid.NewGuid().ToString();
                    await cli.SetAsync(tmp, String);
                    var val = await cli.GetAsync(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }
            }).Wait();
            sw.Stop();
            Console.WriteLine("FreeRedisAsync(0-100000): " + sw.ElapsedMilliseconds + "ms");

            //FreeRedis.Internal.AsyncRedisSocket.sb.Clear();
            //FreeRedis.Internal.AsyncRedisSocket.sw.Start();
            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 100000; a++)
            {
                tasks.Add(Task.Run(async() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    await cli.SetAsync(tmp, String);
                    var val = await cli.GetAsync(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            //var sbstr = FreeRedis.Internal.AsyncRedisSocket.sb.ToString()
            //sbstr = sbstr + sbstr.Split("\r\n").Length + "条消息 ;
            Console.WriteLine("FreeRedisAsync(Task.WaitAll 100000): " + sw.ElapsedMilliseconds + "ms");
            tasks.Clear();

            sw.Reset();
            sw.Start();
            using (var pipe = cli.StartPipe())
            {
                for (var a = 0; a < 100000; a++)
                {
                    var tmp = Guid.NewGuid().ToString();
                    pipe.Set(tmp, String);
                    var val = pipe.Get(tmp);
                }
                var vals = pipe.EndPipe();
                for (var a = 1; a < 100000; a += 2)
                {
                    if (vals[a].ToString() != String)
                    {
                        throw new Exception("not equal");
                    }
                }
            }
            sw.Stop();
            Console.WriteLine("FreeRedisPipeline(0-100000): " + sw.ElapsedMilliseconds + "ms\r\n");

            //sw.Reset();
            //sw.Start();
            //for (var a = 0; a < 100000; a++)
            //    cli.Call(new CommandPacket("SET").Input("TestMGet_string1").InputRaw(String));
            //sw.Stop();
            //Console.WriteLine("FreeRedis2: " + sw.ElapsedMilliseconds + "ms");

            //sw.Reset();
            //sw.Start();
            //for (var a = 0; a < 100000; a++)
            //{
            //    using (var rds = cli.GetTestRedisSocket())
            //    {
            //        var cmd = new CommandPacket("SET").Input("TestMGet_string1").InputRaw(String);
            //        rds.Write(cmd);
            //        cmd.Read<string>();
            //    }
            //}
            //sw.Stop();
            //Console.WriteLine("FreeRedis4: " + sw.ElapsedMilliseconds + "ms");



            sw.Reset();
            sw.Start();
            for (var a = 0; a < 100000; a++)
            {
                var tmp = Guid.NewGuid().ToString();
                RedisHelper.Set(tmp, String);
                var val = RedisHelper.Get(tmp);
                if (val != String)
                {
                    throw new Exception("not equal");
                }
            }
            sw.Stop();
            Console.WriteLine("CSRedisCore(0-100000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 100000; a++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    RedisHelper.Set(tmp, String);
                    var val = RedisHelper.Get(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("CSRedisCore(Task.WaitAll 100000): " + sw.ElapsedMilliseconds + "ms");
            tasks.Clear();

            sw.Reset();
            sw.Start();
            Task.Run(async() =>
            {
                for (var a = 0; a < 100000; a++)
                {
                    var tmp = Guid.NewGuid().ToString();
                    await RedisHelper.SetAsync(tmp, String);
                    var val = await RedisHelper.GetAsync(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }
            }).Wait();
            sw.Stop();
            Console.WriteLine("CSRedisCoreAsync(0-100000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 100000; a++)
            {
                tasks.Add(Task.Run(async() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    await RedisHelper.SetAsync(tmp, String);
                    var val = await RedisHelper.GetAsync(tmp);
                    //if (val != String) throw new Exception("not equal");
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("CSRedisCoreAsync(Task.WaitAll 100000): " + sw.ElapsedMilliseconds + "ms\r\n");
            tasks.Clear();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            RedisHelper.Initialization(new CSRedis.CSRedisClient("127.0.0.1:6379,database=1,asyncPipeline=true,preheat=100,poolsize=100"));
            cli.Set("TestMGet_null1", "");
            RedisHelper.Set("TestMGet_null1", "");
            sedb.StringSet("TestMGet_string1", String);
            ThreadPool.SetMinThreads(1000, 1000);
            Stopwatch sw = new Stopwatch();

            cli.FlushAll();


            sw.Reset();
            sw.Start();
            for (var a = 0; a < 10000; a++)
            {
                var tmp = Guid.NewGuid().ToString();
                sedb.StringSet(tmp, String);
                var val = sedb.StringGet(tmp);
                if (val != String)
                {
                    throw new Exception("not equal");
                }
            }
            sw.Stop();
            Console.WriteLine("StackExchange(0-10000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            var tasks = new List <Task>();

            for (var a = 0; a < 10000; a++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    sedb.StringSet(tmp, String);
                    var val = sedb.StringGet(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("StackExchange(Task.WaitAll 10000): " + sw.ElapsedMilliseconds + "ms");
            tasks.Clear();

            sw.Reset();
            sw.Start();
            Task.Run(async() =>
            {
                for (var a = 0; a < 10000; a++)
                {
                    var tmp = Guid.NewGuid().ToString();
                    await sedb.StringSetAsync(tmp, String);
                    var val = await sedb.StringGetAsync(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }
            }).Wait();
            sw.Stop();
            Console.WriteLine("StackExchangeAsync(0-10000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 10000; a++)
            {
                tasks.Add(Task.Run(async() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    await sedb.StringSetAsync(tmp, String);
                    var val = await sedb.StringGetAsync(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("StackExchangeAsync(Task.WaitAll 10000): " + sw.ElapsedMilliseconds + "ms\r\n");
            tasks.Clear();


            //sw.Reset();
            //sw.Start();
            //using (var local = cli.GetShareClient())
            //{
            //    local.ClientReply(ClientReplyType.off);
            //    for (var a = 0; a < 10000; a++)
            //        local.Set("TestMGet_string1", String);
            //    local.ClientReply(ClientReplyType.on);
            //}
            //sw.Stop();
            //Console.WriteLine("hiredis0: " + sw.ElapsedMilliseconds + "ms");

            //var sw2 = new Stopwatch();
            //sw.Reset();
            //sw.Start();
            //using (var rds = cli.GetTestRedisSocket())
            //{
            //    var strea = rds.Stream;
            //    for (var a = 0; a < 10000; a++)
            //    {
            //        rds.Write(new CommandPacket("SET").Input("TestMGet_string1").InputRaw(String));
            //    }

            //    sw2.Reset();
            //    sw2.Start();
            //    for (var a = 0; a < 10000; a++)
            //    {

            //        strea.ReadByte();

            //        var sb = new StringBuilder();
            //        char c;
            //        bool should_break = false;
            //        while (true)
            //        {
            //            c = (char)strea.ReadByte();
            //            if (c == '\r') // TODO: remove hardcoded
            //                should_break = true;
            //            else if (c == '\n' && should_break)
            //                break;
            //            else
            //            {
            //                sb.Append(c);
            //                should_break = false;
            //            }
            //        }
            //    }
            //    sw2.Stop();
            //}
            //sw.Stop();
            //Console.WriteLine("hiredis1: " + sw.ElapsedMilliseconds + "ms, " + sw2.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            for (var a = 0; a < 10000; a++)
            {
                var tmp = Guid.NewGuid().ToString();
                cli.Set(tmp, String);
                var val = cli.Get(tmp);
                if (val != String)
                {
                    throw new Exception("not equal");
                }
            }
            sw.Stop();
            Console.WriteLine("hiredis(0-10000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 10000; a++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    cli.Set(tmp, String);
                    var val = cli.Get(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("hiredis(Task.WaitAll 10000): " + sw.ElapsedMilliseconds + "ms");
            tasks.Clear();

            //sw.Reset();
            //sw.Start();
            //Task.Run(async () =>
            //{
            //    for (var a = 0; a < 10000; a++)
            //    {
            //        var tmp = Guid.NewGuid().ToString();
            //        await cli.SetAsync(tmp, String);
            //        var val = await cli.GetAsync(tmp);
            //        if (val != String) throw new Exception("not equal");
            //    }
            //}).Wait();
            //sw.Stop();
            //Console.WriteLine("hiredisAsync(0-10000): " + sw.ElapsedMilliseconds + "ms");

            //sw.Reset();
            //sw.Start();
            //tasks = new List<Task>();
            //for (var a = 0; a < 10000; a++)
            //{
            //    tasks.Add(Task.Run(async () =>
            //    {
            //        var tmp = Guid.NewGuid().ToString();
            //        await cli.SetAsync(tmp, String);
            //        var val = await cli.GetAsync(tmp);
            //        if (val != String) throw new Exception("not equal");
            //    }));
            //}
            //Task.WaitAll(tasks.ToArray());
            //sw.Stop();
            //Console.WriteLine("hiredisAsync(Task.WaitAll 10000): " + sw.ElapsedMilliseconds + "ms");
            //tasks.Clear();

            sw.Reset();
            sw.Start();
            using (var pipe = cli.StartPipe())
            {
                for (var a = 0; a < 10000; a++)
                {
                    var tmp = Guid.NewGuid().ToString();
                    pipe.Set(tmp, String);
                    var val = pipe.Get(tmp);
                }
                var vals = pipe.EndPipe();
                for (var a = 1; a < 10000; a += 2)
                {
                    if (vals[a].ToString() != String)
                    {
                        throw new Exception("not equal");
                    }
                }
            }
            sw.Stop();
            Console.WriteLine("hiredisPipeline(0-10000): " + sw.ElapsedMilliseconds + "ms\r\n");

            //sw.Reset();
            //sw.Start();
            //for (var a = 0; a < 10000; a++)
            //    cli.Call(new CommandPacket("SET").Input("TestMGet_string1").InputRaw(String));
            //sw.Stop();
            //Console.WriteLine("hiredis2: " + sw.ElapsedMilliseconds + "ms");

            //sw.Reset();
            //sw.Start();
            //for (var a = 0; a < 10000; a++)
            //{
            //    using (var rds = cli.GetTestRedisSocket())
            //    {
            //        var cmd = new CommandPacket("SET").Input("TestMGet_string1").InputRaw(String);
            //        rds.Write(cmd);
            //        cmd.Read<string>();
            //    }
            //}
            //sw.Stop();
            //Console.WriteLine("hiredis4: " + sw.ElapsedMilliseconds + "ms");



            sw.Reset();
            sw.Start();
            for (var a = 0; a < 10000; a++)
            {
                var tmp = Guid.NewGuid().ToString();
                RedisHelper.Set(tmp, String);
                var val = RedisHelper.Get(tmp);
                if (val != String)
                {
                    throw new Exception("not equal");
                }
            }
            sw.Stop();
            Console.WriteLine("CSRedisCore(0-10000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 10000; a++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    RedisHelper.Set(tmp, String);
                    var val = RedisHelper.Get(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("CSRedisCore(Task.WaitAll 10000): " + sw.ElapsedMilliseconds + "ms");
            tasks.Clear();

            sw.Reset();
            sw.Start();
            Task.Run(async() =>
            {
                for (var a = 0; a < 10000; a++)
                {
                    var tmp = Guid.NewGuid().ToString();
                    await RedisHelper.SetAsync(tmp, String);
                    var val = await RedisHelper.GetAsync(tmp);
                    if (val != String)
                    {
                        throw new Exception("not equal");
                    }
                }
            }).Wait();
            sw.Stop();
            Console.WriteLine("CSRedisCoreAsync(0-10000): " + sw.ElapsedMilliseconds + "ms");

            sw.Reset();
            sw.Start();
            tasks = new List <Task>();
            for (var a = 0; a < 10000; a++)
            {
                tasks.Add(Task.Run(async() =>
                {
                    var tmp = Guid.NewGuid().ToString();
                    await RedisHelper.SetAsync(tmp, String);
                    var val = await RedisHelper.GetAsync(tmp);
                    //if (val != String) throw new Exception("not equal");
                }));
            }
            Task.WaitAll(tasks.ToArray());
            sw.Stop();
            Console.WriteLine("CSRedisCoreAsync(Task.WaitAll 10000): " + sw.ElapsedMilliseconds + "ms\r\n");
            tasks.Clear();
        }