ZiggyCreatures / FusionCache

FusionCache is an easy to use, fast and robust hybrid cache with advanced resiliency features.
MIT License
1.87k stars 96 forks source link

[FEATURE] Activity hit/miss tags #303

Closed dotTrench closed 2 months ago

dotTrench commented 2 months ago

Problem

Currently the tags for an activity does not contain any tags indicating whether a cache lookup resulted in a miss or a hit. This would probably be applicable to the: TryGet, GetOrDefault, GetOrSet, MemoryGet, DistributedGet activities defined in Activities.Names.

Solution

Add tags indicating whether a cache to the activity before it's disposed. e.g. "fusioncache.operation.hit": true/false

Alternatives

The current workarounds I can figure out to indicate whether a cache hit occurs on an activity that I can figure out is to attach an event handler to the FusionCache instance and enrich the ongoing or a wrapping activity from there. e.g.

cache.Event.Hit += (_, _) => Activity.Current?.SetTag("cache.hit", true);
cache.Event.Miss += (_, _) => Activity.Current?.SetTag("cache.hit", false);

however this would also require me to set EnableSyncEventHandlersExecution to true to ensure that the activity is not disposed before setting the tag.

Additional context

I'm not really sure how this would interact with FailSafe as this is not something I personally use, but probably keep this in consideration.

jodydonetti commented 2 months ago

Hi @dotTrench , I think you are right. Let me think about it a moment and will get back to you. Thanks!

jodydonetti commented 2 months ago

I'm implementing this, will update later with some news about it. Thanks again!

jodydonetti commented 2 months ago

Hi @dotTrench , I've added support for this, and 1 extra tag:

I was already using "fusioncache.hit" somewhere else, so I preferred uniformity, and also both are very general concepts (hit/miss and stale/fresh).

Will release the next version very soon, probably tomorrow.

dotTrench commented 2 months ago

Thank you, that's great man! The tags makes sense.

I really appreciate the work you've put into this library, it has saved me a lot of time and pain!

jodydonetti commented 2 months ago

Hi, v1.4.0 has been released 🥳