There is currently an issue with the GetOrAdd API. In some scenarios, the generic overloads are ambiguous and lead to very odd error behavior which is not easy to spot.
See #152 for more details
Example:
The following code actually compiles because ICacheManager<object> is used
_cache = CacheFactory.Build(c => c.WithDictionaryHandle());
var a = _cache.GetOrAdd("key", (k) => new CacheItem<int>(k, 1));
The factory method of GetOrAdd returns CacheItem<int> and not CacheItem<object> which is a user error.
CacheItem<int> is be treated as object value because it doesn't match T = object and the TCacheValue GetOrAdd(string key, Func<string, TCacheValue> valueFactory) overload will be used.
Now, the GetOrAdd call would cache the CacheItem<int> instead of just the value 1.
And it is not very obvious why this is.
Options to prevent this behavior:
- Remove the
GetOrAdd overloads with factories taking CacheItem<T>. There are still TryGetOrAdd overloads which are more explicit and have the same functionality
- Rename those overloads to
GetOrAddCacheItem
Both would be breaking changes although I don't think it would have a very big impact and should be fine.
There is currently an issue with the
GetOrAddAPI. In some scenarios, the generic overloads are ambiguous and lead to very odd error behavior which is not easy to spot.See #152 for more details
Example:
The following code actually compiles because
ICacheManager<object>is usedThe factory method of
GetOrAddreturnsCacheItem<int>and notCacheItem<object>which is a user error.CacheItem<int>is be treated asobjectvalue because it doesn't matchT=objectand theTCacheValue GetOrAdd(string key, Func<string, TCacheValue> valueFactory)overload will be used.Now, the
GetOrAddcall would cache theCacheItem<int>instead of just the value1.And it is not very obvious why this is.
Options to prevent this behavior:
GetOrAddoverloads with factories takingCacheItem<T>. There are stillTryGetOrAddoverloads which are more explicit and have the same functionalityGetOrAddCacheItemBoth would be breaking changes although I don't think it would have a very big impact and should be fine.