Comments (6)
Hi @smthbh and thanks for using FusionCache!
If you are talking about the size of the memory cache (either because you are not using the distributed cache or because you are interested only about that part), it is already possible: since FusionCache works on top of IMemoryCache
and IDistributedCache
, you can configure them however you want.
Normally, when creating a FusionCache instance, if you don't pass anything a new MemoryCache
instance will be created for you, to be used as the 1st level (memory): you can however specify your own instance of MemoryCache
configured however you want. If you are using the builder you can simply use WithMemoryCache(...)
and pass it your instance, and in turn create it with the MemoryCacheOptions
you prefer, including the SizeLimit
. Then, when working with FusionCache, you can specify a Size
for each entry just like you already can do with MemoryCache
, and all will work.
Hope this helps!
from fusioncache.
A cache entry's size [...] and supply that with the 'size' in the cache entry options for FusionCache.
Exactly!
One question I was about to open up an issue for, directly related to this, is how to provide a size value for the result returned by the factory method provided to
fusionCache.GetOrSet()
Good question, and the answer is Adaptive Caching, which is fancy name for when you change some of the entry's options inside of the factory, while it is running.
Let me know if this has helped you.
from fusioncache.
I've seen generic guidance from those Microsoft Docs and StackOverflow that determining cache size is up to the developer creating the cache entries. There are historic GitHub issues on .net's repo by the maintainers saying they moved away from directly calculating memory size of cache entries due to inaccurance/complexity:
dotnet/runtime#48567 (comment)
I don't think this matters. The whole point of this implementation is to be a lightweight concurrent dictionary with expiration that doesn't try to respond to memory pressure and start kicking out entries. That's the only reason I see to be concerned about having a single cache. The for it to have a global process wide view of the state. That is already not the case today with .NET Core. Applications have different caches with different policies. I think this is no different.
A cache entry's size is currently a unitless/arbitrary number supplied when adding entries to the cache. I can, for example, try to estimate determining the size of a cache entry by calculating the size of the string (char count * bytes [and null terminator]) and supply that with the 'size' in the cache entry options for FusionCache. An example of this size estimation can be found in Asp.Net's own implementation of ResponseCacheMiddleware
's CacheEntryHelpers
https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/ResponseCaching/src/CacheEntry/CacheEntryHelpers.cs
One question I was about to open up an issue for, directly related to this, is how to provide a size value for the result returned by the factory method provided to fusionCache.GetOrSet()
And also any guidance for how FusionCache responds to memory pressure or why we shouldn't be concerned with it.
from fusioncache.
I'm not quite sure if FusionCache does this, but for most in-memory cache's they are simply storing a reference to the object, they aren't wasting time doing any serialization, so there is no way to estimate object size.
If you are serializing to some kind of string or byte array, then this could be accomplished easily, but you would never really want that for the in-memory cache.
Best you could do is add a parameter and let the implementor send over the estimated size of an object. Maybe support a default extension method like .GetSizeForFusionCache() that the cache looks for and if the object implements it let it calculate itself.
from fusioncache.
Hi @jasenf
I'm not quite sure if FusionCache does this, but for most in-memory cache's they are simply storing a reference to the object, they aren't wasting time doing any serialization, so there is no way to estimate object size.
Yep, totally: serialization only happens when talking to the 2nd level (via the IDistributedCache
interface). When the distributed level is not involved, no serialization at all.
If you are serializing to some kind of string or byte array, then this could be accomplished easily, but you would never really want that for the in-memory cache.
Agree.
from fusioncache.
Hi @smthbh and thanks for using FusionCache!
If you are talking about the size of the memory cache (either because you are not using the distributed cache or because you are interested only about that part), it is already possible: since FusionCache works on top of
IMemoryCache
andIDistributedCache
, you can configure them however you want.Normally, when creating a FusionCache instance, if you don't pass anything a new
MemoryCache
instance will be created for you, to be used as the 1st level (memory): you can however specify your own instance ofMemoryCache
configured however you want. If you are using the builder you can simply useWithMemoryCache(...)
and pass it your instance, and in turn create it with theMemoryCacheOptions
you prefer, including theSizeLimit
. Then, when working with FusionCache, you can specify aSize
for each entry just like you already can do withMemoryCache
, and all will work.Hope this helps!
That makes sense, thanks!
from fusioncache.
Related Issues (20)
- [FEATURE] 🪞 Remove all usage of reflection HOT 6
- [FEATURE] Better detection of incoherent `CacheName`s options HOT 4
- [QUESTION] Can I use FusionCache only for getting data from a 2nd level (distributed) without InMemory part? HOT 5
- [FEATURE] 📢 Add `IgnoreIncomingBackplaneNotifications` options HOT 2
- [FEATURE] Improve nullable for GetOrSet with non-null factory HOT 8
- Explicitly tell the Factory that it failed HOT 9
- [FEATURE] Combination of eager refresh with distributed cache duration. HOT 5
- [FEATURE] Add a method for list all caches in IFusionCacheProvider (Ex: for dispose purposes) HOT 4
- Possibility of creation of a count variable in distributed cache HOT 2
- [FEATURE] Post factory executed event with possibility to change the cache settings before it be inserted in cache HOT 3
- Syncing different cloud instances HOT 5
- [BUG] Value is stored for longer than FailSafeMaxDuration HOT 7
- [BUG] AllowBackgroundDistributedCacheOperations serialization and cache operations seem to be happening in the foreground HOT 3
- best way to store FusionCacheEntryOptions in iConfiguration HOT 3
- Nullable `Size` option HOT 3
- [BUG] Deadlock caused by cancellation of eager refresh HOT 8
- [BUG] Slow Performance of FusionCache compared to Microsoft.Extensions.Caching.Memory.MemoryCache HOT 4
- [BUG] FailSafe max duration is somehow ignored HOT 7
- [FEATURE] Optimise dependencies by adding TFM HOT 9
- 💡 Add this project to awesome-italia-opensource HOT 2
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 fusioncache.