Closed Jordan-Osborn closed 5 months ago
Hi @Jordan-Osborn , your expectations are right, mostly: let me explain.
Some distributed cache operations will, in fact, run in the background when the AllowBackgroundDistributedCacheOperations
option is set to true
, but the catch is that this is not possible for all operations. Why?
There are 2 different classes of operations:
Set
/Remove
/etc)TryGet
/GetOrDefault
/etc)Of course GetOrSet
is a mix of them, being composed of both a "get phase" and, maybe, a "set phase".
When you think about it, for "set operations" FusionCache is not waiting for a return value, and so they can be executed in a fire-and-forget way.
The same cannot be said for "get operations", where FusionCache is asking for some data, and so it cannot be executed in a fire-and-forget way. It would be like asking a barman for a drink and immediately going away 😅.
The test you should do is with a Set operation: in that case you should see very similar timings.
Hope this helps, let me know how it goes.
Oh, also: a tangential thing I'd like to mention is that you can also set 2 additional options:
DistributedCacheSoftTimeout
DistributedCacheHardTimeout
They act like the corresponding soft/hard timeouts for the factory (FactorySoftTimeout
and FactoryHardTimeout
), meaning they'll be used when there is VS there is not a fallback value.
By setting it you are basically saying to FusionCache "hey, if it takes too much to check in the distributed cache, just forget about it and go to the database".
Watch out though that by setting them to a value which is too low may mean that the distributed cache will basically never have time to answer with a value.
Hope this helps, let me know.
I'm closing this for now: in case you need it I'll re-open.
AllowBackgroundDistributedCacheOperations serialization and cache operations seem to be happening in the foreground
Enabling AllowBackgroundDistributedCacheOperations in the fusion cache options doesn't seem to run distributed cache operations in the backround.
I'm running with a second level sqlite disk cache, and see requests being slowed down due to waiting on serialization/setting of the distributed cache layer (this works out to an appreciable fraction of total request time, as it's caching a relatively large data set).
I traced the callstack to here: https://github.com/ZiggyCreatures/FusionCache/blob/d1a36c287e40b160396672bf0905c8eb1d0a573b/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs#L72
It looks like serialization is not being run in the background and the passed in isBackground flag is only being used for setting descriptions.
Is there something I'm missing here or is this a bug?
To Reproduce
The code below
Expected behavior
Enabling AllowBackgroundDistributedCacheOperations should offload significant work to the background (serialisation/setting 2nd level cache), I'd expect the queries above to take a similar amount of time.
Versions
I've encountered this issue on: