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

How to only store value in CacheManager #294

Closed giangcoi48k closed 4 years ago

giangcoi48k commented 4 years ago

I am intending to use CacheManager for my netcore project. I configured the following

var jss = new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Ignore,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};

const string redisConfigurationKey = "redis";
services.AddSingleton(typeof(ICacheManagerConfiguration),
    new CacheManager.Core.ConfigurationBuilder()
        .WithJsonSerializer(serializationSettings: jss, deserializationSettings: jss)
        .WithUpdateMode(CacheUpdateMode.Up)
        .WithRedisConfiguration(redisConfigurationKey, config =>
        {
            config.WithAllowAdmin()
                .WithDatabase(0)
                .WithEndpoint("localhost", 6379);
        })
        .WithMaxRetries(100)
        .WithRetryTimeout(50)
        .WithRedisCacheHandle(redisConfigurationKey)
        .WithExpiration(ExpirationMode.Absolute, TimeSpan.FromMinutes(10))
        .Build());

services.AddSingleton(typeof(ICacheManager<>), typeof(BaseCacheManager<>));

It work well. But when I looked into redis, there are redundant keys in hash, like timeout, version, defaultExpiration,... For example, I puted one key test like _cacheManager.Put("Test", "Test Value");, the value I got: image I think it's a waste of memory if there are lots of keys with small value. So, is there any way only store value and key, and ignore the redundant keys? Sorry for my bad English

MichaCo commented 4 years ago

Nope that's how CM works today. The meta data is needed to allow timeouts per cache key and support sliding expiration and all those things. It is a bit of an overhead but I think it's a good trade-off.

ndrwrbgs commented 4 years ago

@giangcoi48k I'd gander that if the overhead is too much, you're going to be needing to have fine-grained control over your cache implementation anyway (custom data types, eviction mechanisms etc). But I would personally try it and only decide it's too much if performance tests say so.