-
-
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 8 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
65 changes: 48 additions & 17 deletions
65
src/System.Linq.Dynamic.Core/Parser/ConstantExpressionHelper.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 |
|---|---|---|
| @@ -1,29 +1,60 @@ | ||
| using System.Collections.Concurrent; | ||
| using System.Linq.Dynamic.Core.Util.Cache; | ||
| using System.Linq.Expressions; | ||
|
|
||
| namespace System.Linq.Dynamic.Core.Parser | ||
| namespace System.Linq.Dynamic.Core.Parser; | ||
|
|
||
| internal class ConstantExpressionHelper | ||
| { | ||
| internal static class ConstantExpressionHelper | ||
| private readonly ParsingConfig _config; | ||
|
|
||
| // Static shared instance to prevent duplications of the same objects | ||
| private static ThreadSafeSlidingCache<object, Expression>? _expressions; | ||
|
TWhidden marked this conversation as resolved.
Outdated
|
||
| private static ThreadSafeSlidingCache<Expression, string>? _literals; | ||
|
|
||
| public ConstantExpressionHelper(ParsingConfig config) | ||
| { | ||
| private static readonly ConcurrentDictionary<object, Expression> Expressions = new(); | ||
| private static readonly ConcurrentDictionary<Expression, string> Literals = new(); | ||
| _config = config; | ||
|
TWhidden marked this conversation as resolved.
Outdated
|
||
|
|
||
| } | ||
|
|
||
| public static bool TryGetText(Expression expression, out string? text) | ||
| private ThreadSafeSlidingCache<Expression, string> GetLiterals() | ||
| { | ||
| _literals ??= new ThreadSafeSlidingCache<Expression, string>( | ||
| _config.ConstantExpressionSlidingCacheTimeToLive, | ||
| _config.ConstantExpressionSlidingCacheCleanupFrequency, | ||
| _config.ConstantExpressionSlidingCacheMinItemsTrigger | ||
| ); | ||
| return _literals; | ||
| } | ||
|
|
||
| private ThreadSafeSlidingCache<object, Expression> GetExpression() | ||
| { | ||
| _expressions ??= new ThreadSafeSlidingCache<object, Expression>( | ||
| _config.ConstantExpressionSlidingCacheTimeToLive, | ||
| _config.ConstantExpressionSlidingCacheCleanupFrequency, | ||
| _config.ConstantExpressionSlidingCacheMinItemsTrigger | ||
| ); | ||
| return _expressions; | ||
| } | ||
|
|
||
|
TWhidden marked this conversation as resolved.
Outdated
|
||
|
|
||
| public bool TryGetText(Expression expression, out string? text) | ||
| { | ||
| return GetLiterals().TryGetValue(expression, out text); | ||
| } | ||
|
|
||
| public Expression CreateLiteral(object value, string text) | ||
| { | ||
| if (GetExpression().TryGetValue(value, out var outputValue)) | ||
| { | ||
| return Literals.TryGetValue(expression, out text); | ||
| return outputValue; | ||
| } | ||
|
|
||
| public static Expression CreateLiteral(object value, string text) | ||
| { | ||
| if (!Expressions.ContainsKey(value)) | ||
| { | ||
| ConstantExpression constantExpression = Expression.Constant(value); | ||
| var constantExpression = Expression.Constant(value); | ||
|
|
||
| Expressions.TryAdd(value, constantExpression); | ||
| Literals.TryAdd(constantExpression, text); | ||
| } | ||
| GetExpression().AddOrUpdate(value, constantExpression); | ||
| GetLiterals().AddOrUpdate(constantExpression, text); | ||
|
|
||
| return Expressions[value]; | ||
| } | ||
| return constantExpression; | ||
| } | ||
| } | ||
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
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
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
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
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,14 @@ | ||
| namespace System.Linq.Dynamic.Core.Util.Cache; | ||
|
|
||
| internal struct CacheContainer<TValue> where TValue : notnull | ||
|
TWhidden marked this conversation as resolved.
Outdated
|
||
| { | ||
| public CacheContainer(TValue value, DateTime expirationTime) | ||
| { | ||
| Value = value; | ||
| ExpirationTime = expirationTime; | ||
| } | ||
|
|
||
| public TValue Value { get; } | ||
|
TWhidden marked this conversation as resolved.
Outdated
|
||
|
|
||
| public DateTime ExpirationTime { get; } | ||
| } | ||
Oops, something went wrong.
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.