flask-caching icon indicating copy to clipboard operation
flask-caching copied to clipboard

Run callback after function with cache hit/miss result

Open andyklimczak opened this issue 4 years ago • 6 comments

Hey, I was wondering if there was a mechanism to run a function after the cached function that identifies if the value was in the cache or not. For example if I wanted to log if the request was a cache hit or miss, and send that statistic to a monitoring service, to get better insight if my calls are mostly cached or not.

Wasn't sure if it's in scope for this project, or how else I could determine if the cache had the value or not

andyklimczak avatar Feb 18 '21 19:02 andyklimczak

I already thought about adding some metrics, although nothing specific. Adding callbacks is definitely not the way to do it, though.

I thought about a property on the Flask-Caching object, but what this property should hold is still a big question mark in my head. If thatʼs not feasible we may also, well, cache it under a reserved key, but that would skew the metrics and put extra load on the cache backend.

gergelypolonkai avatar Feb 19 '21 14:02 gergelypolonkai

Flask Signals are built for this.

alanhamlett avatar Mar 05 '21 15:03 alanhamlett

@alanhamlett yes they are, but in the case of caching it might introduce unnecessary overhead. Thatʼs why i said if the use case is collecting metrics, it should be done differently.

gergelypolonkai avatar Mar 06 '21 12:03 gergelypolonkai

Just saw your comment after creating my PR. How about I add a config to enable/disable signals so the user can decide if they don't mind the extra overhead?

alanhamlett avatar Mar 06 '21 16:03 alanhamlett

Thatʼs exactly what i was thinking when i saw your PR ☺

gergelypolonkai avatar Mar 07 '21 15:03 gergelypolonkai

Signals sound like a great solution for this usecase

andyklimczak avatar Oct 20 '21 18:10 andyklimczak