MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.34k stars 457 forks source link

Add configuration by code for System.Runtime.Caching memory options #177 #228

Closed nflash closed 4 years ago

nflash commented 6 years ago

In response to #177 the RuntimeMemoryCacheOptions class was created to allow configuring the System.Runtime.Caching.MemoryCache.

All overloads of the extension method WithSystemRuntimeCacheHandle with a RuntimeMemoryCacheOptions parameter also requires the instanceName parameter because as I understand it, it is not possible to change the Default cache settings.

But that's only one way to configure it:

  • Not sure if we have to add it to the old web/app configuration system as you can already configure it there anyways.
  • Configuration via MS.Extensions.Configuration, like json files. I can figure that one out later though.

Configuration with web/app configuration system still works as before and I think is the only way to configure the default cache.

For the MS.Extensions.Configuration I think I would need some guidance.

nflash commented 6 years ago

I had made the change to throw an exception when trying to set options for the Default cache. I have also added some more tests, including a test expecting the exception.

For unit testing, can you add/change one of the shared cache configurations used by many other tests to use the new way of configuring this cache handle? For example, change this one https://github.com/MichaCo/CacheManager/blob/dev/test/CacheManager.Tests/TestCacheManagers.cs#L332

What kind of change do you suggest? Something like this?

public static ICacheManager<object> WithMemoryAndDictionaryHandles
            => CacheFactory.FromConfiguration<object>(
                BaseConfiguration
                    .Builder
                        .WithSystemRuntimeCacheHandle()
                            .EnableStatistics()
                        .And.WithSystemRuntimeCacheHandle("LimitedCacheHandle", new SystemRuntimeCaching.RuntimeMemoryCacheOptions() { PhysicalMemoryLimitPercentage = 20, CacheMemoryLimitMegabytes = 200 })
                            .EnableStatistics()
                            .WithExpiration(ExpirationMode.Absolute, TimeSpan.FromSeconds(1000))
                        .And.WithDictionaryHandle()
                            .EnableStatistics()
                        .And.WithDictionaryHandle()
                            .EnableStatistics()
                            .WithExpiration(ExpirationMode.Absolute, TimeSpan.FromSeconds(1000))
                    .Build());

I'm asking because I don't want to break any test by, for instance, setting a cache too small or by changing the number of cache handles

MichaCo commented 6 years ago

Great! Re. unit test. Yeah, that shouldn't break anything, that change should be good enough