Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to set the maximum size limit of the LazyCache as is possible in IMemoryCache by MS? #188

Open
DanielTulpWE opened this issue May 1, 2023 · 1 comment

Comments

@DanielTulpWE
Copy link

DanielTulpWE commented May 1, 2023

In MemoryCache by Microsoft we can do things like

public class MyMemoryCache
{
    public MemoryCache Cache { get; } = new MemoryCache(
        new MemoryCacheOptions
        {
            SizeLimit = 1024
        });
}

or

Action<MemoryCacheOptions> action = options =>
            {
                options.SizeLimit = 1024;
            };
services.AddMemoryCache(action);

Is there a way to limit the total size of the LazyCache?

If not, this should be your next feature to implement in my opinion.

This is why it is important

@DanielTulpWE
Copy link
Author

DanielTulpWE commented May 7, 2023

I have figured out a way to do it, not entirely sure yet that LazyCache will use this configuration, maybe @alastairtree can confirm this

/// <summary>
/// Our caching implementation
/// </summary>
public class OurLazyCaching : CachingService
{
      /// <summary>
      /// Our custom caching options, configure in appsettings?
      /// </summary>
      private static readonly IOptions<MemoryCacheOptions> _options = new MemoryCacheOptions
      {
          SizeLimit = 1024 // unitless size
      };

      /// <summary>
      /// Constructor that applies our custom caching options
      /// </summary>
      public OurLazyCaching() : 
          base(new Lazy<ICacheProvider>(() =>
                  new MemoryCacheProvider(new MemoryCache(_options))
                  )
              )
      {
      }
}

And than use this in the denpendency injection
services.AddSingleton<IAppCache, OurLazyCaching>();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant