Skip to content

Add HybridCache to ATM#143

Closed
Erwinvandervalk wants to merge 2 commits intomainfrom
ev/atm/hybridcache
Closed

Add HybridCache to ATM#143
Erwinvandervalk wants to merge 2 commits intomainfrom
ev/atm/hybridcache

Conversation

@Erwinvandervalk
Copy link
Copy Markdown
Contributor

ATM now uses HybridCache, including the ability to use DI to use a custom instance of HybridCache.

Before, we were writing things into IDistributedCache. Some of our clients want more direct control over where (and how) to write cache entries. For example, you may want to encrypt access tokens before they are written to a distributed cache.

We looked into several approaches for this, such as #134.

However, now that HybridCache is introduced in .net 9 (and it's also usable in .net 8), we can use this functionality as well.

There are some challenges with using HybridCache:

  1. HybridCache doesn't yet support 'GetOrDefault' semantics. [API Proposal]: Add GetAsync to HybridCache dotnet/extensions#5688
  2. HybridCache doesn't yet support setting the cache parameters, which allows you to control the lifetime of cached items on write. [API Proposal]: HybridCache computed tags dotnet/extensions#6089
  3. HybridCache doesn't directly allow you to add multiple instances of HybridCache to your container. [API Proposal]: HybridCache - richer DI configuration dotnet/extensions#6026

There are 2 options:

  1. (first commit). Use GetOrCreate. This has several downsides, IE: we can't cache the token for the lifetime of the token.
  2. (second commit) Use a custom implementation of GetOrDefault. There is a risk of L1 Cache misses, but L2 Cache hits. This PR also tries to compensate for this.

fixes: #50

@Erwinvandervalk Erwinvandervalk added area/foss/atm Issues related to Access Token Management impact/breaking The fix or change will be a breaking one labels Mar 18, 2025
@Erwinvandervalk Erwinvandervalk added this to the atm 4.0.0 milestone Mar 18, 2025
@Erwinvandervalk Erwinvandervalk self-assigned this Mar 18, 2025
@Erwinvandervalk Erwinvandervalk requested review from a team March 18, 2025 09:40
@jodydonetti
Copy link
Copy Markdown

May I suggest to take a look at FusionCache as a different option (creator here)?

It's has the same design, has been battle-tested in production for years and has more features including:

and more, you can see a list in the repo's README.

Specifically about the challenges you highlighted about HybridCache:

  1. HybridCache doesn't yet support 'GetOrDefault' semantics. [API Proposal]: Add GetAsync to HybridCache dotnet/extensions#5688

FusionCache does: it has both GetOrDefault and TryGet.

  1. HybridCache doesn't yet support setting the cache parameters, which allows you to control the lifetime of cached items on write. [API Proposal]: HybridCache computed tags dotnet/extensions#6089

FusionCache does, via Adaptive Caching (also take a look at Conditional Refresh, it may be interesting).

  1. HybridCache doesn't directly allow you to add multiple instances of HybridCache to your container. [API Proposal]: HybridCache - richer DI configuration dotnet/extensions#6026

FusionCache does, via multiple Named Caches, usable both via IFusionCacheProvider (like you're used with IHttpClientFactory) and via keyed services.

On top of this, it's also possible to use FusionCache as an implementation of HybridCache (with more features), including using them both directly in the same app at the same time, also including being able to use multiple HybridCache instances via keyed services (which is something the the Microsoft-provided HybridCache implementation can't do currently).

Hope this helps.

@Erwinvandervalk
Copy link
Copy Markdown
Contributor Author

@jodydonetti Thank you for your suggestion.

Personal Opinion: I'm very impressed with the featureset of FusionCache and if I was building an application, I would certainly consider using FusionCache.

However, Duende.AccessTokenManagement is a library and we try to limit the number of external dependencies whenever possible. While I would love it it if the interface of HybridCache was richer, it's interface is 'good enough' for our current proposal.

I'm considering creating a sample that actually demonstrates how using the FusionCache adapter for HybridCache brings a more capable implementation for our consumers.

@jodydonetti
Copy link
Copy Markdown

Personal Opinion: I'm very impressed with the featureset of FusionCache and if I was building an application, I would certainly consider using FusionCache.

Thanks, appreciate it.

However, Duende.AccessTokenManagement is a library and we try to limit the number of external dependencies whenever possible. While I would love it it if the interface of HybridCache was richer, it's interface is 'good enough' for our current proposal.

Makes sense, I suggested it since you have a couple of specific needs that cannot be covered with HybridCache.
Anyway I get the point 👍

I'm considering creating a sample that actually demonstrates how using the FusionCache adapter for HybridCache brings a more capable implementation for our consumers.

That would be something appreciated! If you need anything let me know.

@Erwinvandervalk Erwinvandervalk deleted the ev/atm/hybridcache branch December 3, 2025 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/foss/atm Issues related to Access Token Management impact/breaking The fix or change will be a breaking one

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider ability to control IDistributedCache instance used

2 participants