예제 #1
0
파일: EventHandlers.cs 프로젝트: snoww/roki
        private Task UpdateColor(SocketGuildUser before, SocketGuildUser after)
        {
            Task _ = Task.Run(async() =>
            {
                if (after.Id != _client.CurrentUser.Id)
                {
                    return;
                }

                SocketRole currentTopRole = after.Roles.OrderByDescending(r => r.Position).FirstOrDefault();
                if (currentTopRole == null || currentTopRole.IsEveryone)
                {
                    return;
                }

                await _cache.StringSetAsync($"color:{after.Guild.Id}", currentTopRole.Color.RawValue);
            });

            return(Task.CompletedTask);
        }
예제 #2
0
        public async Task SetCacheAsync <T>(string cacheKey, T value, int cacheTimeoutInMinutes = 0)
        {
            try {
                await Cache.StringSetAsync(cacheKey, value.ToString());

                if (cacheTimeoutInMinutes != 0)
                {
                    await Cache.KeyExpireAsync(cacheKey, TimeSpan.FromMinutes(cacheTimeoutInMinutes));
                }
            }
            catch (Exception) {
                throw new CacheException("An error occured while setting the cache.");
            }
        }
예제 #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(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();
        }
예제 #4
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();
        }