Esempio n. 1
0
        private void Subscribe()
        {
            this.redisSubscriper.Subscribe(
                this.channelName,
                (channel, msg) =>
            {
                string messageStr = (string)msg;

                if (messageStr.StartsWith(this.identifier, StringComparison.Ordinal))
                {
                    // do not notify ourself (might be faster than the second method?
                    return;
                }

                var message = BackPlateMessage.Deserialize(messageStr);

                switch (message.Action)
                {
                case BackPlateAction.Clear:
                    this.OnClear();
                    break;

                case BackPlateAction.ClearRegion:
                    this.OnClearRegion(message.Region);
                    break;

                case BackPlateAction.Changed:
                    if (string.IsNullOrWhiteSpace(message.Region))
                    {
                        this.OnChange(message.Key);
                    }
                    else
                    {
                        this.OnChange(message.Key, message.Region);
                    }
                    break;

                case BackPlateAction.Removed:
                    if (string.IsNullOrWhiteSpace(message.Region))
                    {
                        this.OnRemove(message.Key);
                    }
                    else
                    {
                        this.OnRemove(message.Key, message.Region);
                    }
                    break;
                }
            },
                StackRedis.CommandFlags.FireAndForget);
        }
Esempio n. 2
0
        private void PublishMessage(BackPlateMessage message)
        {
            var msg = message.Serialize();

            lock (this.messageLock)
            {
                if (message.Action == BackPlateAction.Clear)
                {
                    Interlocked.Exchange(ref this.skippedMessages, this.messages.Count);
                    this.messages.Clear();
                }

                if (!this.messages.Add(msg))
                {
                    Interlocked.Increment(ref this.skippedMessages);
                    if (this.logger.IsEnabled(LogLevel.Trace))
                    {
                        this.logger.LogTrace("Skipped duplicate message: {0}.", msg);
                    }
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Notifies other cache clients about a removed cache key.
 /// </summary>
 /// <param name="key">The key.</param>
 public override void NotifyRemove(string key)
 {
     this.PublishMessage(BackPlateMessage.ForRemoved(this.identifier, key));
 }
Esempio n. 4
0
 /// <summary>
 /// Notifies other cache clients about a cache clear region call.
 /// </summary>
 /// <param name="region">The region.</param>
 public override void NotifyClearRegion(string region)
 {
     this.PublishMessage(BackPlateMessage.ForClearRegion(this.identifier, region));
 }
Esempio n. 5
0
 /// <summary>
 /// Notifies other cache clients about a cache clear.
 /// </summary>
 public override void NotifyClear()
 {
     this.PublishMessage(BackPlateMessage.ForClear(this.identifier));
 }
Esempio n. 6
0
 /// <summary>
 /// Notifies other cache clients about a changed cache key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="region">The region.</param>
 public override void NotifyChange(string key, string region)
 {
     this.PublishMessage(BackPlateMessage.ForChanged(this.identifier, key, region));
 }
Esempio n. 7
0
 private void PublishMessage(BackPlateMessage message)
 {
     this.Publish(message.Serialize());
 }
Esempio n. 8
0
        private void Subscribe()
        {
            this.redisSubscriper.Subscribe(
                this.channelName,
                (channel, msg) =>
            {
                var fullMessage = ((string)msg).Split(',')
                                  .Where(s => !s.StartsWith(this.identifier, StringComparison.Ordinal))
                                  .ToArray();

                if (fullMessage.Length == 0)
                {
                    // no messages for this instance
                    return;
                }

                if (this.logger.IsEnabled(LogLevel.Information))
                {
                    this.logger.LogInfo("Back-plate got notified with {0} new messages.", fullMessage.Length);
                }

                foreach (var messageStr in fullMessage)
                {
                    var message = BackPlateMessage.Deserialize(messageStr);

                    switch (message.Action)
                    {
                    case BackPlateAction.Clear:
                        this.OnClear();
                        break;

                    case BackPlateAction.ClearRegion:
                        this.OnClearRegion(message.Region);
                        break;

                    case BackPlateAction.Changed:
                        if (string.IsNullOrWhiteSpace(message.Region))
                        {
                            this.OnChange(message.Key);
                        }
                        else
                        {
                            this.OnChange(message.Key, message.Region);
                        }
                        break;

                    case BackPlateAction.Removed:
                        if (string.IsNullOrWhiteSpace(message.Region))
                        {
                            this.OnRemove(message.Key);
                        }
                        else
                        {
                            this.OnRemove(message.Key, message.Region);
                        }
                        break;
                    }
                }
            },
                StackRedis.CommandFlags.FireAndForget);
        }