/// <summary>
        /// Remove all items from the cache.
        /// </summary>
        public void Clear()
        {
            if (!RedisCacheMessageSuppressionContext.IsSet(Name))
            {
                if (IsolateTenants)
                {
                    var message = new RedisPubSubPerTenantCacheMessage <TKey>(new PerTenantCacheMessage <RedisPubSubCacheMessage <TKey> >(new RedisPubSubCacheMessage <TKey>(RedisPubSubCacheMessageAction.Clear)));

                    PerTenantChannel.Publish(message,
                                             PublishOptions.FireAndForget,
                                             true,
                                             RedisPubSubCacheHelpers.PerTenantMergeAction
                                             );
                }
                else
                {
                    Channel.Publish(
                        new RedisPubSubCacheMessage <TKey>(RedisPubSubCacheMessageAction.Clear),
                        PublishOptions.FireAndForget,
                        true,
                        RedisPubSubCacheHelpers.MergeAction
                        );
                }
            }

            using (new RedisCacheMessageSuppressionContext(Name))
            {
                InnerCache.Clear();
            }
        }
        /// <summary>
        /// Called when an entry is removed from <see cref="InnerCache"/>.
        /// </summary>
        /// <param name="sender">
        /// The object that raised the message.
        /// </param>
        /// <param name="e">
        /// Event-specific arguments.
        /// </param>
        private void InnerCache_ItemsRemoved(object sender, ItemsRemovedEventArgs <TKey> e)
        {
            Interlocked.Increment(ref MessagesSent);
            if (!RedisCacheMessageSuppressionContext.IsSet(Name))
            {
                if (IsolateTenants)
                {
                    var message = new RedisPubSubPerTenantCacheMessage <TKey>(new PerTenantCacheMessage <RedisPubSubCacheMessage <TKey> >(new RedisPubSubCacheMessage <TKey>(RedisPubSubCacheMessageAction.Remove, e.Items)));

                    PerTenantChannel.Publish(
                        message,
                        PublishOptions.FireAndForget,
                        true,
                        RedisPubSubCacheHelpers.PerTenantMergeAction
                        );
                }
                else
                {
                    Channel.Publish(
                        new RedisPubSubCacheMessage <TKey>(RedisPubSubCacheMessageAction.Remove, e.Items),
                        PublishOptions.FireAndForget,
                        true,
                        RedisPubSubCacheHelpers.MergeAction
                        );
                }
            }
        }
        /// <summary>
        /// Merges two per-tenant messages.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <param name="existingMessage">The existing message.</param>
        /// <param name="newMessage">The new message.</param>
        /// <exception cref="System.ArgumentNullException">
        /// existingMessage
        /// or
        /// newMessage
        /// </exception>
        public static void PerTenantMergeAction <TKey>(RedisPubSubPerTenantCacheMessage <TKey> existingMessage, RedisPubSubPerTenantCacheMessage <TKey> newMessage)
        {
            if (existingMessage == null)
            {
                throw new ArgumentNullException("existingMessage");
            }
            if (newMessage == null)
            {
                throw new ArgumentNullException("newMessage");
            }

            foreach (var tenantMessage in newMessage.Keys)
            {
                var existingPerTenantMessage = existingMessage.Keys.FirstOrDefault(key => key.TenantId == tenantMessage.TenantId);

                if (existingPerTenantMessage == null)
                {
                    existingMessage.Keys.Add(tenantMessage);
                }
                else
                {
                    if (existingPerTenantMessage.Key.Action == RedisPubSubCacheMessageAction.Clear || tenantMessage.Key.Action == RedisPubSubCacheMessageAction.Clear)
                    {
                        existingPerTenantMessage.Key.Action = RedisPubSubCacheMessageAction.Clear;
                        existingPerTenantMessage.Key.Keys   = new TKey [0];
                    }
                    else
                    {
                        existingPerTenantMessage.Key.Action = RedisPubSubCacheMessageAction.Remove;
                        existingPerTenantMessage.Key.Keys   = existingPerTenantMessage.Key.Keys.Union(tenantMessage.Key.Keys).ToArray( );
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        ///     Tests equality.
        /// </summary>
        /// <param name="other">The other.</param>
        /// <returns></returns>
        public bool Equals(RedisPubSubPerTenantCacheMessage <TKey> other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }

            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return(Keys.SequenceEqual(other.Keys));
        }