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

Feature Request: CacheStatsCounterType.AvgSize #248

Closed AccessViolator closed 6 years ago

AccessViolator commented 6 years ago

It would be good to maintain an average size of cached items, per region and global. With this it would be easy to estimate how much memory is taken by a region or whole cache by multiplying average by CacheStatsCounterType.Items.

The calculation should use cumulative moving average formula which is pretty simple: NewAvg = LastAvg + (ItemSize - LastAvg)/Count Where Count is not CacheStatsCounterType.Items, but a cumulative counter used solely for calculation of the average.

Would be good if ItemSize included size of CacheItem's own properties.

To be safe and prevent Count from overflowing it's probably worth implementing auto reset of this stat counter before Count would overflow.

MichaCo commented 6 years ago

I thought about adding (some kind of) size based eviction in general to in-proc caches (not needed for Redis or Memcached). And for that I'd need something like that to calculate it, yup. The cached item size cannot be determined problematically though, so it would be some kind of configuration per cache instance which defines the avg expected size...

That said, maintaining the size of regions will not work in most cases, e.g. for System.Runtime.Caching. That's also why there is no count per region

AccessViolator commented 6 years ago

It's a pity, would be a very nice feature. Closing.

I thought regions supported counts, that's a bummer. This isn't documented, is it?