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.35k stars 456 forks source link

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

Open MichaCo opened 7 years ago

MichaCo commented 7 years ago

The only way to configure the memory pressure or limits of the System.Runtime.Caching.MemoryCache based handle is via app/web.config. The implementation already supports named caches, there could be at least overloads for the builder extension methods to define the following three properties...

//Create a name / value pair for properties
var config = new NameValueCollection();
config.Add("pollingInterval", "00:05:00");
config.Add("physicalMemoryLimitPercentage", "0");
config.Add("cacheMemoryLimitMegabytes", "10");

//instantiate cache
var cache = new MemoryCache("CustomCache", config);

API proposal:

.WithSystemRuntimeCacheHandle("CustomCache", pollingInterval: TimeSpan.FromMinutes(5), physicalMemoryLimitPercentage: 0, cacheMemoryLimitMegabytes: 10);
nflash commented 6 years ago

Hi, I'm doing this implementation and I can make a pull request if you like. Instead of the proposed API I was creating a RuntimeMemoryCacheOptions like this:

public class RuntimeMemoryCacheOptions
    {
        public int CacheMemoryLimitMegabytes { get; set; } = 0;
        public int PhysicalMemoryLimitPercentage { get; set; } = 0;
        public TimeSpan PollingInterval { get; set; } = TimeSpan.FromMinutes(2);
    }

and the API would be like this: .WithSystemRuntimeCacheHandle("CustomCache", new RuntimeMemoryCacheOptions(){ PollingInterval = TimeSpan.FromMinutes(5), PhysicalMemoryLimitPercentage = 0, CacheMemoryLimitMegabytes = 10});

The RuntimeMemoryCacheOptions class has a method AsNameValueCollection to convert the configuration to a NameValueCollection required for creating the MemoryCache.

MichaCo commented 6 years ago

@nflash sounds good, I think you'd have to add 2 additional extension methods for adding the handle, basically the same ones which already exist but with the addition of the new options. Make sure the options are totally optional and the old way via web/app config will still work.

But that's only one way to configure it:

Yup, feel free to send a PR :)