-
-
Notifications
You must be signed in to change notification settings - Fork 245
Introduce Sliding Cache to Constant Expression Helper #765
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4ce4385
#764 - Introduce Sliding Cache to Constant Expression Helper
1d3bf0a
#764 Additional Tests, Missing TTL Update
3d09d49
#764 - Rename T1,T2 to TKey, TValue
579085e
#764 - Set cleanup time prior to enumeration to prevent multiple clea…
a3aede1
#764 - Code Review Changes implemented; ThreadSafeSlidingCache Tests …
bda7622
#764 - Move DefaultCleanupFrequency to internal static non generic class
72a6906
#764 - Dropped Preprocessor Directive for TTL in ConstantExpressionHe…
c965066
#764 - Move ConstantExpressionHelper to Instance. Refactor feedback f…
cc8482a
#764 - ConstantExpressionHelper Singleton Factory; Code Review Resolu…
8d9b6bf
#764 - Refactor Naming for SlidingCache;
c8653b1
#764 - PR Refactor Feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
80 changes: 80 additions & 0 deletions
80
src/System.Linq.Dynamic.Core/Parser/ThreadSafeSlidingCache.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| using System.Collections.Concurrent; | ||
|
|
||
| namespace System.Linq.Dynamic.Core.Parser | ||
|
StefH marked this conversation as resolved.
Outdated
|
||
| { | ||
| internal class ThreadSafeSlidingCache<T1, T2> where T1 : notnull where T2 : notnull | ||
|
TWhidden marked this conversation as resolved.
Outdated
|
||
| { | ||
| private readonly ConcurrentDictionary<T1, (T2 Value, DateTime ExpirationTime)> _cache; | ||
| private readonly TimeSpan _timeToLive; | ||
| private readonly TimeSpan _cleanupFrequency; | ||
| private DateTime _lastCleanupTime = DateTime.MinValue; | ||
|
|
||
| public ThreadSafeSlidingCache(TimeSpan timeToLive, TimeSpan? cleanupFrequency = null) | ||
|
StefH marked this conversation as resolved.
Outdated
StefH marked this conversation as resolved.
Outdated
|
||
| { | ||
| _cache = new ConcurrentDictionary<T1, (T2, DateTime)>(); | ||
| _timeToLive = timeToLive; | ||
| _cleanupFrequency = cleanupFrequency ?? TimeSpan.FromSeconds(10); | ||
|
StefH marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public TimeSpan TimeToLive => _timeToLive; | ||
|
|
||
| public void AddOrUpdate(T1 key, T2 value) | ||
| { | ||
| if (key == null) throw new ArgumentNullException(nameof(key)); | ||
|
StefH marked this conversation as resolved.
Outdated
|
||
| if (value == null) throw new ArgumentNullException(nameof(value)); | ||
|
StefH marked this conversation as resolved.
Outdated
|
||
|
|
||
| var expirationTime = DateTime.UtcNow.Add(_timeToLive); | ||
| _cache[key] = (value, expirationTime); | ||
|
|
||
| CleanupIfNeeded(); | ||
|
StefH marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public bool TryGetValue(T1 key, out T2 value) | ||
| { | ||
| if (key == null) throw new ArgumentNullException(nameof(key)); | ||
|
|
||
| CleanupIfNeeded(); | ||
|
|
||
| if (_cache.TryGetValue(key, out var valueAndExpiration)) | ||
| { | ||
| if (DateTime.UtcNow <= valueAndExpiration.ExpirationTime) | ||
| { | ||
| value = valueAndExpiration.Value; | ||
| _cache[key] = (value, DateTime.UtcNow.Add(_timeToLive)); | ||
| return true; | ||
| } | ||
| else | ||
| { | ||
| // Remove expired item | ||
| _cache.TryRemove(key, out _); | ||
| } | ||
| } | ||
|
|
||
| value = default!; | ||
| return false; | ||
| } | ||
|
|
||
| public bool Remove(T1 key) | ||
| { | ||
| if (key == null) throw new ArgumentNullException(nameof(key)); | ||
| var removed = _cache.TryRemove(key, out _); | ||
| CleanupIfNeeded(); | ||
| return removed; | ||
| } | ||
|
|
||
| private void CleanupIfNeeded() | ||
| { | ||
| if (DateTime.UtcNow - _lastCleanupTime > _cleanupFrequency) | ||
| { | ||
| foreach (var key in _cache.Keys) | ||
| { | ||
| if (DateTime.UtcNow > _cache[key].ExpirationTime) | ||
| { | ||
| _cache.TryRemove(key, out _); | ||
| } | ||
| } | ||
| _lastCleanupTime = DateTime.UtcNow; | ||
| } | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
test/System.Linq.Dynamic.Core.Tests/Util/ConstantExpressionHelperTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using System.Linq.Dynamic.Core.Parser; | ||
|
StefH marked this conversation as resolved.
Outdated
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace System.Linq.Dynamic.Core.Tests | ||
| { | ||
| public partial class EntitiesTests | ||
| { | ||
| [Fact] | ||
| public async Task TestConstantExpressionLeak() | ||
| { | ||
| //Arrange | ||
| PopulateTestData(1, 0); | ||
|
|
||
| var populateExpression = _context.Blogs.All("BlogId > 2000"); | ||
|
|
||
| var expressions = ConstantExpressionHelper.Expressions; | ||
|
|
||
| // Should contain | ||
| if (!expressions.TryGetValue(2000, out _)) | ||
| { | ||
| Assert.Fail("Cache was missing constant expression for 2000"); | ||
| } | ||
|
|
||
| // wait half the expiry time | ||
| await Task.Delay(TimeSpan.FromSeconds(ConstantExpressionHelper.Expressions.TimeToLive.TotalSeconds/2)); | ||
| if (!expressions.TryGetValue(2000, out _)) | ||
| { | ||
| Assert.Fail("Cache was missing constant expression for 2000 (1)"); | ||
| } | ||
|
|
||
| // wait another half the expiry time, plus one second | ||
| await Task.Delay(TimeSpan.FromSeconds((ConstantExpressionHelper.Expressions.TimeToLive.TotalSeconds / 2)+1)); | ||
| if (!expressions.TryGetValue(2000, out _)) | ||
| { | ||
| Assert.Fail("Cache was missing constant expression for 2000 (2)"); | ||
| } | ||
|
|
||
| // Wait for the slide cache to expire, check on second later | ||
| await Task.Delay(ConstantExpressionHelper.Expressions.TimeToLive.Add(TimeSpan.FromSeconds(1))); | ||
|
|
||
| if (expressions.TryGetValue(2000, out _)) | ||
| { | ||
| Assert.Fail("Expected constant to be expired 2000"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.