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.33k stars 458 forks source link

Allow null values pattern #301

Closed fabionaspolini closed 3 years ago

fabionaspolini commented 4 years ago

Hi,

Sometimes the null value is a significative value and CacheManaget library does not support this case. I would like manager this behavior. My suggestion is enable/disable this feature by ICacheManager.Configuration.

Exemple:

cacheManager = CacheFactory.Build("name", settings => settings.WithMicrosoftMemoryCacheHandle("handle"));

// New property... default its false for compatibility
cacheManager.Configuration.AllowNull = true;

In my case, I read params from database and store in memory cache by X minute, but if value does not exists in database, I would like stored this key in memory cache with null value, and reduced sql overhead to try to obtain inexistent value. This behavior is planned and supported by Microsoft IMemoryCache for example.

MichaCo commented 3 years ago

Because of the architecture of CacheManager, storing null doesn't work. For memory caches only, yeah that would not be a problem. But, because CacheManager has to support distributed caching with serialization, just passing in null will be impossible to compute if the object cache is being used, because you cannot infer the real type if you just pass in null for an object.

If I would redesign the library with a new API, I'd probably drop the object support and have only one non typed cache with a generic interface. And then, nulls could be supported.

But no, for now that's by design.