Esempio n. 1
0
        /// <summary>
        /// Clean up.
        /// </summary>
        private void Disposing(bool disposing)
        {
            InnerCache.ItemsRemoved -= InnerCache_ItemsRemoved;

            if (Channel != null)
            {
                try
                {
                    Channel.MessageReceived -= Channel_MessageReceived;
                    Channel.Dispose();
                    Channel = null;
                }
                catch (Exception)
                {
                    // Do nothing
                }
            }

            if (PerTenantChannel != null)
            {
                try
                {
                    PerTenantChannel.MessageReceived -= PerTenantChannel_MessageReceived;
                    PerTenantChannel.Dispose( );
                    PerTenantChannel = null;
                }
                catch (Exception)
                {
                    // Do nothing
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisPubSubCache{TKey, TValue}" /> class.
        /// </summary>
        /// <param name="innerCache">The inner cache this wraps. This cannot be null.</param>
        /// <param name="cacheName">The cache name. This cannot be null, empty or whitespace.</param>
        /// <param name="memoryManager">The <see cref="IDistributedMemoryManager" /> used to communicate to Redis. This cannot be null.</param>
        /// <param name="isolateTenants">if set to <c>true</c> [isolate tenants].</param>
        /// <exception cref="System.ArgumentNullException">innerCache
        /// or
        /// cacheName
        /// or
        /// memoryManager</exception>
        /// <exception cref="ArgumentNullException">Neither <paramref name="innerCache" />, <paramref name="memoryManager" />
        /// can be null. <paramref name="cacheName" /> cannot be null,
        /// empty or whitespace.</exception>
        public RedisPubSubCache(
            ICache <TKey, TValue> innerCache,
            string cacheName,
            IDistributedMemoryManager memoryManager,
            bool isolateTenants = false
            )
        {
            if (innerCache == null)
            {
                throw new ArgumentNullException("innerCache");
            }
            if (string.IsNullOrWhiteSpace(cacheName))
            {
                throw new ArgumentNullException("cacheName");
            }
            if (memoryManager == null)
            {
                throw new ArgumentNullException("memoryManager");
            }

            Name           = cacheName;
            IsolateTenants = isolateTenants;

            try
            {
                InnerCache = innerCache;
                InnerCache.ItemsRemoved += InnerCache_ItemsRemoved;

                // Listen to invalidation messages
                MemoryManager = memoryManager;
                if (!MemoryManager.IsConnected)
                {
                    MemoryManager.Connect();
                }

                if (IsolateTenants)
                {
                    PerTenantChannel = memoryManager.GetChannel <RedisPubSubPerTenantCacheMessage <TKey> >(RedisPubSubCacheHelpers.GetChannelName(Name));
                    PerTenantChannel.MessageReceived += PerTenantChannel_MessageReceived;
                    PerTenantChannel.Subscribe( );
                }
                else
                {
                    Channel = memoryManager.GetChannel <RedisPubSubCacheMessage <TKey> >(RedisPubSubCacheHelpers.GetChannelName(Name));
                    Channel.MessageReceived += Channel_MessageReceived;
                    Channel.Subscribe( );
                }
            }
            catch (Exception)
            {
                Disposing(false);
                throw;
            }
        }