Comments (4)
We could embed the type into the key but that would break the capability to cache sync and retrieve it from the async method feature which is quite convenient in a sync/async environment, so inclined not to and make the developer choose the right key. The behaviour you describe does help the developer to catch situations where they have missused the same key.
Either when you cache a new thing of different type it should:
1 - returns null as now, (will probably become a null ref runtime exception in users code)
2 - Throw an exception from inside lazycache
3 - override the thing in the cache with the new thing of different type (no exception - assumes the developer knew what they were doing - last in wins)
My preference would be option 3?
from lazycache.
Would agree with #3 , @alastairtree , giving the power in the hands of the developer to decide what the intention was rather than throwing an exception
from lazycache.
Thanks for looking at this. It it's helpful I can describe my use-case....
I have an IoT application where multiple record types are associated with each sensor. The sensor Id is the most useful key to use to index these records and it's therefore a convenience to store everything associated with a particular sensor in a single cache. So for sensor "1234" the cache might contain a location-record and a power-record. All the client libraries I'm using are using generics and so storing/fetching by key+type is fairly natural. As it happened, I just wrote some extension methods to get the behaviour I wanted....
/// <summary>
/// Provides wrappers around LazyCache that allow the same key to be used for different object types
/// </summary>
public static class CacheExtension
{
private static string MakeKey<T>(string key)
{
return $"{typeof(T)}" + key;
}
public static void SafeAdd<T>(this IAppCache cache,string key,T val)
{
cache.Add(MakeKey<T>(key),val);
}
public static Task<T> SafeGetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> fetcher)
{
return cache.GetOrAddAsync(MakeKey<T>(key), fetcher);
}
public static T SafeGetOrAdd<T>(this IAppCache cache, string key, Func<T> fetcher)
{
return cache.GetOrAdd(MakeKey<T>(key), fetcher);
}
}
While I'm commenting.... you probably have good design reasons for doing it this way but it would be a nicety (at least for the sync version) to have a GetOrAdd prototype that takes a Func<string,T> so that the nicer method-group syntax could be used....
var foo = cache.GetOrAdd("key", FetchFoo);
Foo FetchFoo(string key) {..}
from lazycache.
This will be fixed in 2.0.3 so that last in wins and if you cache 2 types with the same key, the second type will force the eviction of the first. However unless you have a really good reason, always use different keys for different types.
Thanks
from lazycache.
Related Issues (20)
- Create a sectioned IAppCache
- how do I clean the cache items by wildcard of a key HOT 2
- Add TryGetValueAsync that evaluates GetValueFromAsyncLazy HOT 1
- Cache eviction policy when cache is full? HOT 1
- Absolute expiration does not work v2.4.0 HOT 1
- Implement MemoryCache.Trim() to allow explicit removal
- LongRunning shouldn't run more than once HOT 1
- About Cache availability mechanism HOT 2
- CachingService.TryGetValue returning wrong value HOT 2
- Cannot dispose of cache HOT 3
- How to create separate caches? Not seeing how this is done. HOT 1
- Do you have plans to upgrade to dotnet7 HOT 5
- Question/Proposal on the limit of Cache keying to strings vs. "TKey"
- Is it possible to have some kind of management console to show the list of keys and their expiry?
- Bug in Wiki: Why does the Cancellation Tokens example use two CancellationTokenSource?
- Singleton constraint conflict if IMemoryCache already exists
- Is it possible to set the maximum size limit of the LazyCache as is possible in IMemoryCache by MS? HOT 1
- PostEvictionCallBack causing memory leak
- Returning null still successfully adds entry
- Is this project abandoned? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lazycache.