Code Monkey home page Code Monkey logo

Comments (6)

jodydonetti avatar jodydonetti commented on June 2, 2024 2

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.

jodydonetti avatar jodydonetti commented on June 2, 2024 1

Hi @seantleonard

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.

seantleonard avatar seantleonard commented on June 2, 2024

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.

jasenf avatar jasenf commented on June 2, 2024

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.

jodydonetti avatar jodydonetti commented on June 2, 2024

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.

smthbh avatar smthbh commented on June 2, 2024

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!

That makes sense, thanks!

from fusioncache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.