Comments (5)
Can you give an example of what you mean as not sure I follow? What sort of syntax would you like? If you just wanted to use the cached item type as a key you could easily write an extension method that used the types of the existing generic parameter to generate a string based on namespace and type name for example.
from lazycache.
Right now I am using Concurrent Dictionary in which the key is GenericType:
ConcurrentDictionary<RequestIdentity, Action<IDbCommand, object>>
.
I am looking for something same like this IAppCache.Add<T1, T2>(T1 key, T2 item)
from lazycache.
The problem with adding this to LazyCache is it means that you cannot cache 2 objects of the same type, as the second one would replace the first. This might lead to unexpected issues for an inexperienced user. Why not just add an extension method in your project that converted from type to string key by reflecting on the name, something like var key = typeof(T1).FullName;
. Some extension methods for inspiration are in https://github.com/alastairtree/LazyCache/blob/feat/netcore2/LazyCache/AppCacheExtenions.cs
from lazycache.
@alastairtree: I can understand the problem that we can have by adding that feature in the library. However, we did some custom code on our end to bypass that issue. We basically implemented a multi-layer cache that uses internally a ConcurrentDictionary of ConcurrentDictionary. The key of the first dictionary is a string and then they key of the other dictionary is generic. The problem with that solution is that it's very limited, we don't have all the features of MemoryCache, which enables the developer to manipulate the cache based on the time and memory.
It will be really great if we can enhance the LazyCache and add the requested feature in it. I know it will be hard, since LazyCache is coded over the MemoryCache which does not support generic keys. A lot of people already complained about that limitation in the past.
from lazycache.
I think you could solve this with a simple extension method like the below that uses the type name as the key? I would not build it into LazyCache as may confuse new developers but no reason not to add it into your project? You could also tweak it to use a different type param as the key like in your example.
namespace LazyCache
{
public static class AppCacheExtenions
{
public static T GetOrAdd<T>(this IAppCache cache, Func<T> addItemFactory)
{
if (cache == null) throw new ArgumentNullException(nameof(cache));
var key = typeof(T).FullName;
return cache.GetOrAdd(key, addItemFactory);
}
}
}
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.