MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.35k stars 456 forks source link

Removing from cache callback doesn't work #187

Closed RahmanM closed 7 years ago

RahmanM commented 7 years ago

Hi, When item is removed from cache the callback doesn't work. The event is never fired? Using the plain Redis it works though see below:

            var manager = CacheFactory.Build<Customer>(settings =>
            {
                settings
                    .WithRedisConfiguration("redis", config =>
                    {
                        config.WithAllowAdmin()
                            .WithDatabase(0)
                            .WithEndpoint("localhost", 6379);
                    })
                    .WithMaxRetries(100)
                    .WithRetryTimeout(50)
                    .WithRedisBackplane("redis")
                    .WithRedisCacheHandle("redis", true);
            });

            // add customer
            manager.Add("customer1", Dumper.GetCustomer());
            Dumper.Print(manager.Get<Customer>("customer1"));

            // add with expiration
            manager.Expire("customer1", ExpirationMode.Absolute, TimeSpan.FromSeconds(5));
            manager.OnRemoveByHandle += Manager_OnRemoveByHandle; // This is not called
            //manager.OnRemove += CacheItem_Removed; // OR this one
            Dumper.Print(manager.Get<Customer>("customer1"));

private static void Manager_OnRemoveByHandle(object sender, CacheManager.Core.Internal.CacheItemRemovedEventArgs e)
        {
            Dumper.Print(e.Key);
        }

        private static void CacheItem_Removed(object sender, CacheManager.Core.Internal.CacheActionEventArgs e)
        {
            Dumper.Print(e.Key);
        }

public static Customer GetCustomer()
    {
        return new Customer()
        {
            Id = 1,
            Name = "rahman",
            Address = new Address() { City = "sydney", Line1 = "address 1", Line2 = "address 2" },
        };
    }

// Using the plain Redis it works though ... This works!!!

      private static void CacheInvalidation()
        {

            IDatabase db = redisConnections.GetDatabase();
            ISubscriber subscriber = redisConnections.GetSubscriber();

            subscriber.Subscribe("__keyspace@0__:*", (channel, value) =>
            {
                Console.WriteLine("Channel=> " + channel);

                if (channel.ToString().IndexOf("__keyspace@0__:") > -1)
                {
                    Console.WriteLine("Item removed from cache:" + channel.ToString().Split(':')[1]);
               }
            }
            );
        }
MichaCo commented 7 years ago

Hi @RahmanM, Sorry for the late reply, I hope you found the answer already. CacheManager only registers to key events if configured explicitly.

Using the following should fix it

.WithRedisConfiguration("redis", config =>
                    {
                        config
                            .WithAllowAdmin()
                            .EnableKeyspaceEvents()   // enables the events
                            .WithDatabase(0)
                            .WithEndpoint("localhost", 6379);
                    })