Replace cache lockers with production-ready sp_getapplock and SETNX implementations#4
Merged
Merged
Conversation
… lockers SQL Server now uses sp_getapplock/sp_releaseapplock for true atomic session-level locking, eliminating the non-atomic check-then-set race condition in the old DistributedCacheLocker. Redis now uses SET NX PX + compare-and-delete Lua scripts for atomic distributed locking. Memory cache drops the unnecessary SemaphoreSlimLocker since IMemoryCache reference semantics and ConcurrentDictionary already guarantee thread safety. Also fixes a bug in IMemoryCacheExtensions.SetKey where the namespace tracking entry was written to the wrong cache key (individual key instead of namespace).
…ic via Lua scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DistributedCacheLocker(check-then-set race condition) with backend-specific lockers:SqlServerCacheLocker(sp_getapplock) andRedisCacheLocker(SET NX PX + compare-and-delete Lua scripts)SemaphoreSlimLockerfrom the memory cache —IMemoryCachereference semantics andConcurrentDictionaryalready guarantee thread safetyIMemoryCacheExtensions.SetKeywhere the namespace tracking entry was written under the wrong cache key (individual key instead of the namespace key), preventing expiration options from ever being applied to the tracking dictChanges
SqlServerCacheLocker: singlesp_getapplockcall with native SQL Server blocking up toLockTimeout; released viasp_releaseapplockon a heldSqlConnectionRedisCacheLocker: atomicSET NX PXacquire + compare-and-delete release, both single Lua scripts; polls every 100 ms up toLockTimeoutNullCacheLock.IsAcquirednow initializes totrue(wasfalse, silently skipping everyWaitForLockThenAsynccallback when used withNullCacheLocker)MemoryActionCache→NullCacheLock/NullCacheLocker;SqlServerActionCacheFactorynow injectsIOptions<SqlServerCacheOptions>to extract the connection stringDistributedCacheLocker,DistributedCacheLock,SemaphoreSlimLocker,SemaphoreSlimLockTest plan
SqlServerCacheLockerrequires a live SQL Server instance to test locking behaviour end-to-end (covered by integration tests against a real DB)