예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ListeningQueue{T}"/> class.
 /// </summary>
 /// <param name="innerQueue">The inner queue.</param>
 /// <param name="redisMgr">The redis MGR.</param>
 public ListeningQueue(IQueue <T> innerQueue, IDistributedMemoryManager redisMgr)
 {
     InnerQueue = innerQueue;
     Channel    = redisMgr.GetChannel <ListeningQueueMessage>($"channel|{innerQueue.Name}");
     Channel.MessageReceived += ChannelMessageReceived;
     Channel.Subscribe();
 }
예제 #2
0
        public BackgroundTaskController(IDistributedMemoryManager redisMgr, IBackgroundTaskManager backgroundTaskManager)
        {
            _taskManager = backgroundTaskManager;

            Channel = redisMgr.GetChannel <BackgroundTaskControllerMessage>("BackgroundTaskController");
            Channel.MessageReceived += ChannelMessageReceived;
            Channel.Subscribe();
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        public DeferredChannelMessageContextEntry(IDistributedMemoryManager distributedMemoryManager)
        {
            if (distributedMemoryManager == null)
            {
                throw new ArgumentNullException("distributedMemoryManager");
            }

            _distributedMemoryManager = distributedMemoryManager;
        }
예제 #4
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;
            }
        }
예제 #5
0
        // TODO: Handle task expiration

        /// <summary>
        /// Create a Redis backed task manager
        /// </summary>
        /// <param name="memoryManager">The memory manager to use</param>
        /// <param name="taskGroupName">The name of the task group. This is used namespace different groups of tasks.</param>
        /// <param name="taskExpiry">The task expiry.</param>
        public TaskManager(IDistributedMemoryManager memoryManager, string taskGroupName, TimeSpan?taskExpiry = null)
        {
            if (!memoryManager.IsConnected)
            {
                memoryManager.Connect();
            }

            _memoryStore   = memoryManager.GetMemoryStore();
            _taskGroupName = taskGroupName;
            _taskExpiry    = taskExpiry ?? DefaultTaskExpiry;
        }
        private static void FlushChannelMessages <T>(IDistributedMemoryManager distributedMemoryManager, string channelName, DeferredChannelMessage channelMessage)
        {
            if (channelMessage == null || channelMessage.Message == null)
            {
                return;
            }

            using (IChannel <T> channel = distributedMemoryManager.GetChannel <T>(channelName))
                using (new TenantAdministratorContext(channelMessage.TenantId))
                {
                    channel.Publish((T)channelMessage.Message, PublishMethod.Immediate, PublishOptions.FireAndForget);
                }
        }
        public void Remove_NoSuppression( )
        {
            for (int i = 0; i < 1000; i++)
            {
                const string cacheName = "My Precious";

                Tuple <long, string> [] testData =
                {
                    new Tuple <long, string>(144, "foo"),
                    new Tuple <long, string>(96,  "bar")
                };

                string value;

                IDistributedMemoryManager memoryManager = Factory.DistributedMemoryManager;

                using (RedisPubSubCache <long, string> redisPubSubCache1 = new RedisPubSubCache <long, string>(new DictionaryCache <long, string>( ), cacheName, memoryManager))
                    using (RedisPubSubCache <long, string> redisPubSubCache2 = new RedisPubSubCache <long, string>(new DictionaryCache <long, string>( ), cacheName, memoryManager))
                    {
                        foreach (Tuple <long, string> testDatum in testData)
                        {
                            redisPubSubCache1.Add(testDatum.Item1, testDatum.Item2);
                            redisPubSubCache2.Add(testDatum.Item1, testDatum.Item2);
                        }

                        CountdownEvent evt = new CountdownEvent(1);

                        EventHandler <MessageEventArgs <RedisPubSubCacheMessage <long> > > handler = (sender, message) =>
                        {
                            evt.Signal( );
                        };

                        redisPubSubCache2.Channel.MessageReceived += handler;

                        redisPubSubCache1.Remove(testData [0].Item1);

                        // Wait for message
                        evt.Wait(DefaultRedisWaitTime);

                        redisPubSubCache2.Channel.MessageReceived -= handler;

                        Assert.That(redisPubSubCache1.TryGetValue(testData [0].Item1, out value), Is.False, "Item not removed from cache 1");
                        Assert.That(redisPubSubCache2.TryGetValue(testData [0].Item1, out value), Is.False, "Item not removed from cache 2");
                        Assert.That(redisPubSubCache1.TryGetValue(testData [1].Item1, out value), Is.True, "Item 2 removed from cache 1");
                        Assert.That(value, Is.EqualTo(testData [1].Item2), "Item 2 incorrect value in cache 1");
                        Assert.That(redisPubSubCache2.TryGetValue(testData [1].Item1, out value), Is.True, "Item 2 removed from cache 2");
                        Assert.That(value, Is.EqualTo(testData [1].Item2), "Item 2 incorrect value in cache 2");
                    }
            }
        }
예제 #8
0
        /// <summary>
        ///     Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose( )
        {
            if (Channel != null)
            {
                Channel.MessageReceived -= Handler;
                Channel.Dispose( );
                Channel = null;
            }

            if (Manager != null)
            {
                Manager.Dispose( );
                Manager = null;
            }

            if (Context != null)
            {
                Context.Dispose( );
                Context = null;
            }
        }
        public void Clear_Supression()
        {
            const string cacheName = "My Precious";

            Tuple <long, string>[] testData =
            {
                new Tuple <long, string>(26,  "foo"),
                new Tuple <long, string>(986, "bar")
            };
            string value;

            IDistributedMemoryManager memoryManager = Factory.DistributedMemoryManager;

            using (RedisPubSubCache <long, string> redisPubSubCache1 = new RedisPubSubCache <long, string>(
                       new DictionaryCache <long, string>( ), cacheName, memoryManager))
                using (RedisPubSubCache <long, string> redisPubSubCache2 = new RedisPubSubCache <long, string>(
                           new DictionaryCache <long, string>( ), cacheName, memoryManager))
                {
                    foreach (Tuple <long, string> testDatum in testData)
                    {
                        redisPubSubCache1.Add(testDatum.Item1, testDatum.Item2);
                        redisPubSubCache2.Add(testDatum.Item1, testDatum.Item2);
                    }

                    using (new RedisCacheMessageSuppressionContext(cacheName))
                    {
                        redisPubSubCache1.Clear();
                    }

                    Assert.That(redisPubSubCache1.TryGetValue(testData[0].Item1, out value), Is.False, "Item 1 not removed from cache 1");
                    Assert.That(redisPubSubCache2.TryGetValue(testData[0].Item1, out value), Is.True, "Item 1 removed from cache 2");
                    Assert.That(redisPubSubCache1.TryGetValue(testData[1].Item1, out value), Is.False, "Item 2 not removed from cache 1");
                    Assert.That(redisPubSubCache2.TryGetValue(testData[1].Item1, out value), Is.True, "Item 2 removed from cache 2");
                    Assert.That(redisPubSubCache1, Is.Empty, "Cache 1 not empty");
                    Assert.That(redisPubSubCache2, Is.Not.Empty, "Cache 2 not empty");
                }
        }
예제 #10
0
        public void Remove_Suppression()
        {
            const string cacheName = "My Precious";

            Tuple <long, string>[] testData =
            {
                new Tuple <long, string>(144, "foo"),
                new Tuple <long, string>(96,  "bar")
            };
            string value;

            IDistributedMemoryManager memoryManager = Factory.DistributedMemoryManager;

            using (RedisPubSubCache <long, string> redisPubSubCache1 = new RedisPubSubCache <long, string>(
                       new DictionaryCache <long, string>( ), cacheName, memoryManager))
                using (RedisPubSubCache <long, string> redisPubSubCache2 = new RedisPubSubCache <long, string>(
                           new DictionaryCache <long, string>( ), cacheName, memoryManager))
                {
                    foreach (Tuple <long, string> testDatum in testData)
                    {
                        redisPubSubCache1.Add(testDatum.Item1, testDatum.Item2);
                        redisPubSubCache2.Add(testDatum.Item1, testDatum.Item2);
                    }

                    using (new RedisCacheMessageSuppressionContext(cacheName))
                    {
                        redisPubSubCache1.Remove(testData[0].Item1);
                    }

                    Assert.That(redisPubSubCache1.TryGetValue(testData[0].Item1, out value), Is.False, "Item not removed from cache 1");
                    Assert.That(redisPubSubCache2.TryGetValue(testData[0].Item1, out value), Is.True, "Item removed from cache 2");
                    Assert.That(redisPubSubCache1.TryGetValue(testData[1].Item1, out value), Is.True, "Item 2 removed from cache 1");
                    Assert.That(value, Is.EqualTo(testData[1].Item2), "Item 2 incorrect value in cache 1");
                    Assert.That(redisPubSubCache2.TryGetValue(testData[1].Item1, out value), Is.True, "Item 2 removed from cache 2");
                    Assert.That(value, Is.EqualTo(testData[1].Item2), "Item 2 incorrect value in cache 2");
                }
        }
예제 #11
0
        IListeningQueue <string> CreateQueue(IDistributedMemoryManager mgr)
        {
            var q = new InMemoryTestQueue <string>();

            return(new ListeningQueue <string>(q, mgr));
        }