alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Trying to use LazyCache and get some cache hit/miss stats. #22

Closed PureKrome closed 5 years ago

PureKrome commented 7 years ago

Hi @alastairtree

I'm trying to leverage LazyCache but to get some stats on the number of hits/misses in some time frame.

So lets say: given a 1 hour window, how many hits/misses.

First, what's the start of the 1 hour window? is it DateTime.Now for the first time this piece of code has ran? (think multiple web requests at the same time, too).

Then, how could we increment two different cache keys? I'm assuming that the miss ends up calling the lazy delegate method, so that's not tooo bad. But I still don't know where I could store a counter for misses that would reset (evicted?) after an hour.

Then to the hits! The GetOrAddAsync will just return something ... a counter? which i then need to thread-safe increment and THEN reset after an hour again.

So - tough problem to solve I think :(

Could you offer any ideas here how LazyCache might be able to leverage some of this?

EDIT: Oh! forgot to mention some reason/scenario also -> at the end of each 60 mins, I was going to send a logging message that reports the cache hit/miss ratio for the last hour. Then reset the hit/miss counters.

So then we can review the logs to see some of the stats.

alastairtree commented 7 years ago

If you are using the default MemoryCache implementation of ObjectCache as the underlying cache store then it already has counters built in for this kind of thing. Documentation is not great but this SO post is a start https://stackoverflow.com/q/20059000/3140853

PureKrome commented 7 years ago

yeah - I read that post yesterday too :(

  1. We're on Azure Services, so can't install anything custom
  2. Not sure if there's programmatic access to those stats?
alastairtree commented 7 years ago

You could try https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/runtime-profiling#reading-and-creating-performance-counters-programmatically Failing that I would inherit a new cache service from CachingService and add some stats/counters of you own.

alastairtree commented 5 years ago

Afraid I don't have to build this into the library as I don't have a need for perf counters but nothing stopping you forking the project and adding your own.