Пример #1
0
        static async Task Main(string[] args)
        {
            var a = await client.ConnectAsync();

            //await ThreadOnly();
            Console.WriteLine("---------------");
            await TaskWall();

            Console.WriteLine("---------------");
            ParallelVoid();
            Console.ReadKey();
        }
Пример #2
0
        private IRedisClient CreateClient()
        {
            var cli = RedisClient.ConnectAsync(connectionString: ConnectionString).Result;

            cli.FlushDbAsync().Wait();
            return(cli);
        }
        public static async Task <RedisClient> GetRedisClientAsync()
        {
            // NOTE: as an optimization for frequent accesses, we check to see if the redis client exists before locking on its sync object.
            if (_redisClient == null)
            {
                await _redisClientSyncLock.WaitAsync();

                try
                {
                    // NOTE: if the previous caller was blocked on .WaitAsync() because the singleton was being created, once it is unblocked this code block will be skipped.
                    if (_redisClient == null)
                    {
                        RedisClient redisClient = new RedisClient();
                        await redisClient.ConnectAsync(REDIS_SERVER_HOSTNAME);

                        await redisClient.EnablePipelineAsync();

                        await redisClient.SelectAsync(REDIS_DATABASE_INDEX_OAUTH2SERVICE);

                        _redisClient = redisClient;
                    }
                }
                finally
                {
                    _redisClientSyncLock.Release();
                }
            }
            return(_redisClient);
        }
        public async Task Init(IPEndPoint endpoint, CancellationToken cancel)
        {
            var options = new RedisClientOptions();

            options.MultiplexPool.CommandConnections  = 1;
            options.MultiplexPool.SubscriptionOptions = 1;
            _client = new RedisClient(endpoint, options);

            await _client.ConnectAsync(cancel).ConfigureAwait(false);

            using (var channel = _client.CreateChannel())
            {
                channel.Dispatch("flushdb");
                var data = Enumerable.Range(0, 10).Select(i => i.ToString("0000000000"));
                await channel.ExecuteAsync(@"
                              sadd @key @data
                              sadd @key2 @data
                              set @key3 'hi this is a test'"
                                           , new
                {
                    key  = "testkeyRC",
                    key2 = "testkeyRC2",
                    key3 = "testkeyRC3",
                    data
                }).ConfigureAwait(false);
            }
        }
Пример #5
0
        /// <summary>
        /// 得到一个Redis客户端实例
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns>Redis客户端实例</returns>
        public static IRedisClient GetRedisClient(string ip, int port)
        {
            var task = RedisClient.ConnectAsync(ip, port);

            task.Wait();
            return(task.Result);
        }
Пример #6
0
        internal static async Task <RedisClient> CreateNewRedisClientAsync()
        {
            RedisClient redisClient = new RedisClient();
            await redisClient.ConnectAsync(REDIS_SERVER_HOSTNAME);

            await redisClient.EnablePipelineAsync();

            await redisClient.SelectAsync(REDIS_DATABASE_INDEX_EVENTSERVICE);

            return(redisClient);
        }
        public void Init()
        {
            var options = GetOptions();

            Client  = new RedisClient(RedisInstance.Endpoint, options);
            _cancel = new CancellationTokenSource();
            Task.Run(() => Client.ConnectAsync(_cancel.Token));
            using (var channel = Client.CreateChannel())
            {
                channel.Execute("FLUSHALL");
            }
            Log("Test Initialized.");
        }
        public async Task Init(IPEndPoint endpoint, CancellationToken cancel)
        {
            var options = new RedisClientOptions();

            options.MultiplexPool.CommandConnections  = 1;
            options.MultiplexPool.SubscriptionOptions = 1;
            _client = new RedisClient(endpoint, options);

            await _client.ConnectAsync(cancel).ConfigureAwait(false);

            using (var channel = _client.CreateChannel())
            {
                channel.Dispatch("flushdb");
                await channel.ExecuteAsync("incr @key", new { key = "whatever" }).ConfigureAwait(false);
            }
        }
Пример #9
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                throw new ArgumentNullException("connectionString");
            }
            var client = RedisClient.ConnectAsync(args[0], int.Parse(args[1])).Result;

            using (client)
            {
                while (true)
                {
                    try
                    {
                        Console.Write("> ");
                        var cmd = "";

                        while (true)
                        {
                            cmd += Console.ReadLine();
                            if (cmd.EndsWith("`"))
                            {
                                cmd = cmd.TrimEnd('`');
                                continue;
                            }
                            break;
                        }

                        var arguments = SplitCmd(cmd);

                        var parsedCmd = ParseCmd(arguments);

                        if (IsQuit(parsedCmd))
                        {
                            break;
                        }
                        var result = client.ExecuteAsync(parsedCmd.Name, parsedCmd.Arguments).Result;

                        OutResult(result);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
Пример #10
0
 private static void Connect()
 {
     _client = RedisClient.ConnectAsync(connectionString).Result;
 }
 public void Connect(RedisConnectionStringBuilder connectionString)
 {
     _client = RedisClient.ConnectAsync(connectionString.EndPoint, connectionString.DbIndex).Result;
 }
Пример #12
0
        public Tuple <Int64, TimeSpan> Test(Int32 threads, Int32 loops)
        {
            var options = new RedisClientOptions()
            {
                Logger = new ConsoleLogger(false)
            };

            options.MultiplexPool.CommandConnections  = 1;
            options.MultiplexPool.SubscriptionOptions = 1;
            using (var client = new RedisClient(_endpoint, options))
            {
                client.ConnectAsync(CancellationToken.None).Wait();

                using (var channel = client.CreateChannel())
                    channel.Execute("flushall");

                var cancel   = new CancellationTokenSource();
                var taskList = new List <Thread>();

                var total    = threads * loops;
                var progress = 0;
                var bars     = 0;

                var sw = new Stopwatch();
                sw.Start();
                for (int threadId = 0; threadId < threads; threadId++)
                {
                    var ts = new ThreadStart(() =>
                    {
                        try
                        {
                            for (int loop = 0; loop < loops; loop++)
                            {
                                using (var channel = client.CreateChannel())
                                    RunClient(threadId.ToString() + "_" + loop.ToString(), channel, cancel.Token).Wait();

                                var p          = Interlocked.Increment(ref progress);
                                var percentage = (Int32)((p * 100D) / total);
                                while (bars < percentage)
                                {
                                    Interlocked.Increment(ref bars);
                                    Console.Write("|");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.Write("[{0}]", ex.GetType());
                        }
                    });

                    var thread = new Thread(ts);
                    thread.Start();
                    taskList.Add(thread);
                }

                foreach (var t in taskList)
                {
                    t.Join();
                }
                sw.Stop();

                cancel.Cancel();

                return(new Tuple <Int64, TimeSpan>(progress, sw.Elapsed));
            }
        }
Пример #13
0
        /// <summary>
        /// 得到一个Redis客户端实例(异步)
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns>Redis客户端实例</returns>
        public static async Task <IRedisClient> GetRedisClientAsync(string ip, int port)
        {
            var client = await RedisClient.ConnectAsync(ip, port);

            return(client);
        }
        public static void Configure(Container container, IPEndPoint endpoint, Boolean withLogging)
        {
            var options = new RedisClientOptions()
            {
                //PingTimeout = TimeSpan.FromSeconds(1),
                //PreventPingIfActive = false,
                Logger = withLogging? new RedisLogger() : null
            };

            options.Procedures.LoadFromAssembly(Assembly.GetExecutingAssembly());

            var client = new RedisClient(endpoint, options);

            var connect = client.ConnectAsync(CancellationToken.None);

            container.RegisterSingleton <RedisClient>(client);
            container.Register <IRedisChannel>(() => client.CreateChannel(), Lifestyle.Scoped);

            container.Register <IMessaging>(() => new RedisClientMessaging(client.CreateChannel()));

            container.Register <ICommandExecuter <VisitQuestionCommand, VisitQuestionCommandResult>, VisitQuestionCommandExecuter>();

            container.Register <ICommandExecuter <AnswerEditCommand, AnswerEditCommandResult>, AnswerEditCommandExecuter>();
            container.Register <ICommandExecuter <AnswerDeleteCommand, AnswerDeleteCommandResult>, AnswerDeleteCommandExecuter>();
            container.Register <ICommandExecuter <AnswerVoteCommand, AnswerVoteCommandResult>, AnswerVoteCommandExecuter>();
            container.Register <ICommandExecuter <AnswerCreateCommand, AnswerCreateCommandResult>, AnswerCreateCommandExecuter>();

            container.Register <ICommandExecuter <QuestionDeleteCommand, QuestionDeleteCommandResult>, QuestionDeleteCommandExecutor>();
            container.Register <ICommandExecuter <QuestionCloseCommand, QuestionCloseCommandResult>, QuestionCloseCommandExecuter>();
            container.Register <ICommandExecuter <QuestionEditCommand, QuestionEditCommandResult>, QuestionEditCommandExecuter>();
            container.Register <ICommandExecuter <QuestionVoteCommand, QuestionVoteCommandResult>, QuestionVoteCommandExecuter>();
            container.Register <ICommandExecuter <QuestionCreateCommand, QuestionCreateCommandResult>, QuestionCreateCommandExecuter>();

            container.Register <ICommandExecuter <EndSessionCommand, EndSessionCommandResult>, EndSessionCommandExecuter>();
            container.Register <ICommandExecuter <ValidateSessionCommand, ValidateSessionCommandResult>, ValidateSessionCommandExecuter>();
            container.Register <ICommandExecuter <AuthenticateCommand, AuthenticateCommandResult>, AuthenticateCommandExecuter>();

            container.Register <IModelBuilder <UserModelRequest, UserModel>, UserModelBuilder>();
            container.Register <IModelBuilder <UserInboxRequest, UserInboxModel>, UserInboxModelBuilder>();

            container.Register <IModelBuilder <AnswerRequest, AnswerViewModel>, AnswerRequestBuilder>();
            container.Register <IModelBuilder <AnswerEditFormRequest, AnswerEditFormViewModel>, AnswerEditFormRequestBuilder>();
            container.Register <IModelBuilder <AnswerDeleteFormRequest, AnswerDeleteFormViewModel>, AnswerDeleteFormRequestBuilder>();

            container.Register <IModelBuilder <HomeByTagRequest, HomeByTagViewModel>, HomeByTagRequestBuilder>();
            container.Register <IModelBuilder <HomeRequest, HomeViewModel>, HomeRequestBuilder>();

            container.Register <IModelBuilder <QuestionDeleteFormRequest, QuestionDeleteFormViewModel>, QuestionDeleteFormRequestBuilder>();
            container.Register <IModelBuilder <QuestionCloseFormRequest, QuestionCloseFormViewModel>, QuestionCloseFormRequestBuilder>();
            container.Register <IModelBuilder <QuestionEditFormRequest, QuestionEditFormViewModel>, QuestionEditFormRequestBuilder>();
            container.Register <IModelBuilder <QuestionRequest, QuestionViewModel>, QuestionRequestBuilder>();

            container.Register <IModelBuilder <TagSuggestionRequest, TagSuggestionsModel>, TagSuggestionRequestBuilder>();
            container.Register <IModelBuilder <PopularTagsRequest, PopularTagsViewModel>, PopularTagsRequestBuilder>();

            Registration registration = container.GetRegistration(typeof(IMessaging)).Registration;

            registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent, "Manually disposed");

            connect.Wait();
        }