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.34k stars 457 forks source link

CacheManager.Exists is flaky #223

Closed priyanperera closed 6 years ago

priyanperera commented 6 years ago

Hi, I am using the following configuration to build the cache manager.

        var cache = CacheFactory.Build("CacheManager", settings =>
        {
                var serializerSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace, TypeNameHandling = TypeNameHandling.Auto };
                settings = settings.WithUpdateMode(CacheUpdateMode.Up)
                                   .WithGzJsonSerializer(serializerSettings, serializerSettings);

            var part = settings.WithMicrosoftMemoryCacheHandle()
                               .WithExpiration(MapInternalExpirationMode(ExpirationMode), TimeSpan.FromMinutes(ItemTimeout));

            part.And
            .WithRedisConfiguration("Redis", config => config.WithAllowAdmin().WithDatabase(0).WithEndpoint("localhost" , 6379))
            .WithMaxRetries(50)
            .WithRetryTimeout(100)
            .WithRedisBackplane("Redis")
            .WithRedisCacheHandle("Redis")
            .WithExpiration(MapInternalExpirationMode(CacheItemExpirationMode.Sliding), TimeSpan.FromMinutes(30));
        });

Then I use cache.Put(key, value); to set a value in the cache.

Then I retrieve a memory cache handle and a redis cache handle using the following code.

var localHandle = (MemoryCacheHandle)_cache.CacheHandles.First(h => h is MemoryCacheHandle);

var redisHandle = (RedisCacheHandle)_cache.CacheHandles.First(h => h is RedisCacheHandle);

The issue is when I am calling the redisCache.Exists(key). It returns true sometimes and false intermittently which is puzzling since the memory cache handle is consistent all the time. Any pointers as to what I might be doing wrong?

MichaCo commented 6 years ago

Redis might have removed they key. That can be due to your expiration settings or memory pressure?

Exists also doesn't "touch" the key, which means, if you don't do a GET, the sliding expiration window will not get extended and the key expires...

I'm pretty sure the exists method works fine. If you still think there is a bug or an issue, please try to give me an example to easily reproduce the behavior ;)