Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to SlidingCache<> for Improved Thread Safety and Performance #769

Closed
TWhidden opened this issue Feb 2, 2024 · 8 comments
Closed
Labels

Comments

@TWhidden
Copy link
Contributor

TWhidden commented Feb 2, 2024

In the latest update to the SlidingCache<> implementation, we addressed a critical oversight related to concurrent modifications and access patterns. Specifically, we identified that a cache key could be removed during enumeration, triggering a KeyNotFoundException. To address these concerns and enhance the cache's robustness and efficiency, the following adjustments were made:

  1. Atomic Locking with Interlocked.CompareExchange: We introduced an atomic lock mechanism using Interlocked.CompareExchange to prevent simultaneous entries into the Cleanup method. This enhancement aims to significantly reduce contention and ensure thread-safe operations within the cache cleanup process.

  2. Deferred Cleanup Execution: The CleanupIfNeeded method has been relocated to a finally block following the TryGetValue execution. This will ensure that if PermitExpiredReturns is enabled, there is no race to remove the item while accessing it.

  3. Support for Deterministic Element Caching: A new parameter has been added to SlidingCache<>, enabling the caching of expired but deterministic elements. This feature allows expired objects to remain accessible before the Cleanup process has executed, preventing unnecessary removals and recreations. This capability is particularly beneficial for ConstantExpressions and is enabled by default to optimize performance for predictable data patterns.

  4. Configurable Expired Returns via CacheConfig: To provide greater control over cache behavior, the PermitExpiredReturns setting has been incorporated into the CacheConfig class. This addition allows developers to manage the allowance of expired item returns based on their specific application requirements and caching strategies.

These enhancements are designed to bolster the SlidingCache<>'s performance, reliability, and flexibility, particularly in high-concurrency environments.

TWhidden pushed a commit to TWhidden/System.Linq.Dynamic.Core that referenced this issue Feb 2, 2024
@StefH StefH added the bug label Feb 3, 2024
TWhidden pushed a commit to TWhidden/System.Linq.Dynamic.Core that referenced this issue Feb 3, 2024
TWhidden pushed a commit to TWhidden/System.Linq.Dynamic.Core that referenced this issue Feb 3, 2024
StefH pushed a commit that referenced this issue Feb 3, 2024
…ance (#770)

* #769 - Enhancements to SlidingCache<> for Improved Thread Safety and Performance

* #769 - Rename Config ReturnExpiredItems; Refactor Tests

* #769 - SlidingCache Test Code-Comment Cleanup

---------

Co-authored-by: Travis Whidden <[email protected]>
@StefH StefH closed this as completed Feb 3, 2024
@StefH
Copy link
Collaborator

StefH commented Feb 8, 2024

@TWhidden
Did you have time to verify this new PR?

@TWhidden
Copy link
Contributor Author

TWhidden commented Feb 8, 2024

It's currently running in a beta instance without any known issues. I have not rolled it to prod yet. I'll try to get that out this week or early next week. Sorry for the delay.

@StefH
Copy link
Collaborator

StefH commented Feb 14, 2024

Did you have time to test it?

@TWhidden
Copy link
Contributor Author

I have not pushed to production yet, however I did pull a memory snapshot of our demo/beta build which is an active system. I am working on a release to prod right now. FYI - this is using 9.3.9-preview-02 for reference. So far, I have not had any error popup.
image

@TWhidden
Copy link
Contributor Author

Ok, pushed to production - will follow up in a couple days.

@StefH StefH reopened this Feb 20, 2024
@StefH
Copy link
Collaborator

StefH commented Feb 28, 2024

Hello @TWhidden, what is the status?

@TWhidden
Copy link
Contributor Author

Looks solid. No leaks, no exceptions reported. 9 days running in prod.

image
image

All good!

@StefH
Copy link
Collaborator

StefH commented Feb 28, 2024

Thanks for verifying.
I'll release a new NuGet soon.

@StefH StefH closed this as completed Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants