Exemple #1
0
 protected override async Task RunClient(String userId, IRedisChannel channel, CancellationToken cancel)
 {
     try
     {
         var userKey = "user_" + userId;
         var result  = await channel.ExecuteAsync("incr @key", new { key = userKey }).ConfigureAwait(false);
     }
     catch (ObjectDisposedException)
     {
         Console.Write("[D]");
     }
     catch (TaskCanceledException)
     {
         Console.Write("[C]");
     }
     catch (AggregateException aex)
     {
         foreach (var ex in aex.InnerExceptions)
         {
             Console.Write("[EX: " + ex.Message + "]");
         }
     }
     catch (Exception ex)
     {
         Console.Write("[EX: " + ex.Message + "]");
     }
 }
        private RemovedKeys UnsubscribeInternal(IRedisChannel channel, IEnumerable <String> keys)
        {
            var removed = new RemovedKeys();
            HashSet <IRedisChannel> subscribed;

            foreach (var key in keys)
            {
                if (_subscribed.TryGetValue(key, out subscribed))
                {
                    subscribed.Remove(channel);
                    if (subscribed.Count == 0)
                    {
                        removed.Add(key);
                        _subscribed.Remove(key);
                    }
                }
            }

            HashSet <String> channelSubscriptions;

            if (_subscriptions.TryGetValue(channel, out channelSubscriptions))
            {
                channelSubscriptions.RemoveWhere(s => keys.Contains(s));
                if (channelSubscriptions.Count == 0)
                {
                    _subscriptions.Remove(channel);
                }
            }

            return(removed);
        }
Exemple #3
0
        protected RedisClient(RedisConnectionStringBuilder connectionString, IRedisSerializer serializer)
        {
            ConnectionString         = connectionString.ToString();
            _connectionStringBuilder = connectionString;
            Serializer = serializer ?? DefaultSerializer;

            var socket = new Socket(_connectionStringBuilder.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            _channel = PrepareRedisChannel(socket);
        }
        internal IEnumerable <String> GetSubscriptions(IRedisChannel channel)
        {
            var subscribedTo = new RemovedKeys();
            HashSet <String> subscriptions;

            if (_subscriptions.TryGetValue(channel, out subscriptions))
            {
                subscribedTo.AddRange(subscriptions);
            }
            return(subscribedTo);
        }
        public SubscriptionOperation(IRedisChannel channel, RESPCommand[] commands, RESPObject[] responses, SubscriptionSplitter subscriptions)
        {
            Contract.Assert(commands.Length == responses.Length, "The number of commands is different than the responses placeholder.");

            _subscriptions = subscriptions;
            _channel       = channel;
            _commands      = commands;
            _responses     = responses;

            for (int i = 0; i < _commands.Length; i++)
            {
                if (_commands[i].IsSubscription)
                {
                    _responses[i] = RESPSimpleString.OK;
                }
            }
        }
Exemple #6
0
        protected override async Task RunClient(String userId, IRedisChannel channel, CancellationToken cancel)
        {
            try
            {
                var userKey = "user_" + userId;

                foreach (var dummy in _data)
                {
                    var result = await channel.ExecuteAsync("hmset @key @data", new { key = userId + "_" + dummy.Id, data = Parameter.SequenceProperties(dummy) }).ConfigureAwait(false);

                    result.ThrowErrorIfAny();
                }

                foreach (var dummy in _data)
                {
                    var result = await channel.ExecuteAsync(@"
                                                    hgetall @key
                                                    del @key",
                                                            new { key = userId + "_" + dummy.Id })
                                 .ConfigureAwait(false);

                    result.ThrowErrorIfAny();
                    var readed = result[0].AsObjectCollation <DummyClass>();
                }
            }
            catch (ObjectDisposedException)
            {
                Console.Write("[D]");
            }
            catch (TaskCanceledException)
            {
                Console.Write("[C]");
            }
            catch (AggregateException aex)
            {
                foreach (var ex in aex.InnerExceptions)
                {
                    Console.Write("[EX: " + ex.Message + "]");
                }
            }
            catch (Exception ex)
            {
                Console.Write("[EX: " + ex.Message + "]");
            }
        }
        internal SubscriptionResponsesTracker RemoveChannel(IRedisChannel channel)
        {
            SubscriptionResponsesTracker tracker = new SubscriptionResponsesTracker();

            var keys = _subscriptions.GetSubscriptions(channel).ToList();

            if (keys.Any())
            {
                GenerateTracker(channel, tracker, "UNSUBSCRIBE", keys);
            }

            keys = _psubscriptions.GetSubscriptions(channel).ToList();
            if (keys.Any())
            {
                GenerateTracker(channel, tracker, "PUNSUBSCRIBE", keys);
            }

            return(tracker);
        }
        internal AddedKeys Subscribe(IRedisChannel channel, IEnumerable <String> keys)
        {
            Contract.Assert(keys.Any(), "Subscribing redis channel to empty list of keys.");

            var added = new AddedKeys();

            HashSet <IRedisChannel> subscribed;

            foreach (var key in keys)
            {
                if (_subscribed.TryGetValue(key, out subscribed))
                {
                    subscribed.Add(channel);
                }
                else
                {
                    subscribed = new HashSet <IRedisChannel>();
                    subscribed.Add(channel);
                    _subscribed.Add(key, subscribed);
                    added.Add(key);
                }
            }

            HashSet <String> channelSubscriptions;

            if (!_subscriptions.TryGetValue(channel, out channelSubscriptions))
            {
                channelSubscriptions = new HashSet <String>(keys);
                _subscriptions.Add(channel, channelSubscriptions);
            }
            else
            {
                foreach (var key in keys)
                {
                    channelSubscriptions.Add(key);
                }
            }


            return(added);
        }
        internal SubscriptionResponsesTracker Aggregate(IRedisChannel channel, IEnumerable <RESPCommand> commands)
        {
            Contract.Assert(commands.Any(), "Trying to track an empty list of commands.");

            var data = new SubscriptionResponsesTracker();

            foreach (var command in commands)
            {
                var operation = command.Header.ToUpperInvariant();
                var keys      = command.Skip(1).Cast <RESPCommandPart>().Select(x => x.Value).ToList();

                if (IsSubscriptionMessage(operation))
                {
                    throw new InvalidOperationException("Redis Messages are not subscription control operations.");
                }

                GenerateTracker(channel, data, operation, keys);
            }

            return(data);
        }
        private void GenerateTracker(IRedisChannel channel, SubscriptionResponsesTracker data, String operation, IList <string> keys)
        {
            switch (operation)
            {
            case "SUBSCRIBE":
                Include(operation, _subscriptions.Subscribe(channel, keys), data.AddSubscription, data.AddCommand);
                break;

            case "PSUBSCRIBE":
                Include(operation, _psubscriptions.Subscribe(channel, keys), data.AddSubscription, data.AddCommand);
                break;

            case "UNSUBSCRIBE":
                Include(operation, _subscriptions.Unsubscribe(channel, keys), data.AddUnsubscription, data.AddCommand);
                break;

            case "PUNSUBSCRIBE":
                Include(operation, _psubscriptions.Unsubscribe(channel, keys), data.AddUnsubscription, data.AddCommand);
                break;

            default: throw new InvalidOperationException("Unrecognized subscription control operation: " + operation);
            }
        }
Exemple #11
0
 public QuestionEditCommandExecuter(IRedisChannel channel)
 {
     _channel = channel;
 }
Exemple #12
0
 public EndSessionCommandExecuter(IRedisChannel channel)
 {
     _channel = channel;
 }
Exemple #13
0
 public AnswerCreateCommandExecuter(IRedisChannel channel)
 {
     _channel = channel;
 }
 public AnswerDeleteFormRequestBuilder(IRedisChannel channel)
 {
     _channel = channel;
 }
 public UsersXMLProcessor(ICommandExecuterMediator mediator, IRedisChannel channel)
 {
     _mediator = mediator;
     _channel  = channel;
 }
Exemple #16
0
 public QuestionDeleteCommandExecutor(IRedisChannel channel)
 {
     _channel = channel;
 }
        internal RemovedKeys Unsubscribe(IRedisChannel channel, IEnumerable <String> keys)
        {
            Contract.Assert(keys.Any(), "Unsubcribing channel from an empty list of keys.");

            return(UnsubscribeInternal(channel, keys));
        }
 public PopularTagsRequestBuilder(IRedisChannel channel)
 {
     _channel = channel;
 }
 public ValidateSessionCommandExecuter(IRedisChannel channel)
 {
     _channel = channel;
 }
 public AnswerRequestBuilder(IRedisChannel channel)
 {
     _channel = channel;
 }
Exemple #21
0
 public RedisClientMessaging(IRedisChannel channel)
 {
     _channel       = channel;
     _notifications = new BufferBlock <RedisNotification>();
     _channel.NotificationHandler = n => _notifications.Post(n);
 }
 public QuestionEditFormRequestBuilder(IRedisChannel channel)
 {
     _channel = channel;
 }
 public UserModelBuilder(IRedisChannel channel)
 {
     _channel = channel;
 }
 public VisitQuestionCommandExecuter(IRedisChannel channel)
 {
     _channel = channel;
 }
 public HomeByTagRequestBuilder(IRedisChannel channel)
 {
     _channel = channel;
 }
Exemple #26
0
 public AuthenticateCommandExecuter(IRedisChannel channel)
 {
     _channel = channel;
 }
 public AnswerEditCommandExecuter(IRedisChannel channel)
 {
     _channel = channel;
 }
 protected abstract Task RunClient(String userId, IRedisChannel channel, CancellationToken cancel);
Exemple #29
0
 public TagSuggestionRequestBuilder(IRedisChannel channel)
 {
     _channel = channel;
 }
 public RemoveChannelOperation(IRedisChannel channel, SubscriptionSplitter subscriptions)
 {
     _subscriptions = subscriptions;
     _channel       = channel;
 }