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

Can we override config expiration settings during Add()? #366

Closed suntereo closed 2 years ago

suntereo commented 2 years ago

Hi Michael!

Thanks again for such a great product.

I'm using CacheManager with L1 memory cache and L2 Redis distributed cache. See sample config below. Let's say for an example that I'm using sliding expiration of 5 minutes for both in the config.

It looks like I might be able to override the mode and expiration of individual entries at the time their added with something like:

CacheManager.Add(new CacheItem(key, val, ExpirationMode.Absolute, TimeSpan.FromMinutes(15));

I'm assuming both the L1 and L2 caches would honor the expiration of items set this way. Is this correct?

I do see L2 entries have the settings in Redis:

pic

But I can't easily see the L2 in-memory cache. Does it do the same?

Thanks!

{
  "maxRetries": 1000,
  "name": "nonExpiring",
  "retryTimeout": 100,
  "updateMode": "Up",
  "loggerFactory": {
    "knownType": "Microsoft"
  },
  "serializer": {
    "knownType": "Json"
  },
  "handles": [
    // L2 settings
    // Seems like can be overridden individually with CacheManager.Add(new CacheItem(key,val,MODE,TIMESPAN))
    {
      "knownType": "Redis",
      "key": "redisConnection",
      "enablePerformanceCounters": false,
      "enableStatistics": false,
      "isBackplaneSource": true,
      "expirationMode": "Sliding",
      "expirationTimeout": "0:5:00"
    },
    // L1 settings
    // only way to configure L1 ExpirationMode and ExpirationTimeout is here (no way to do this in code); see https://github.com/MichaCo/CacheManager/issues/177
    // Can this also be overridden individually with CacheManager.Add(new CacheItem(key,val,MODE,TIMESPAN)) ?? 
    {
      "knownType": "SystemRuntime",
      "enablePerformanceCounters": false,
      "enableStatistics": false,
      "expirationMode": "Sliding",
      "expirationTimeout": "0:5:00",
      "isBackplaneSource": false
    }   
  ]
}
MichaCo commented 2 years ago

If you set the expiration on an item it'll be applied to both cache levels, yes

suntereo commented 2 years ago

Awesome! Thank you Michael!!