-
-
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 all 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
45 changes: 28 additions & 17 deletions
45
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,40 @@ | ||
| using System.Collections.Concurrent; | ||
| using System.Linq.Dynamic.Core.Util.Cache; | ||
| using System.Linq.Dynamic.Core.Validation; | ||
| 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 SlidingCache<object, Expression> _expressions; | ||
| private readonly SlidingCache<Expression, string> _literals; | ||
|
|
||
| public ConstantExpressionHelper(ParsingConfig config) | ||
| { | ||
| var parsingConfig = Check.NotNull(config); | ||
| var useConfig = parsingConfig.ConstantExpressionCacheConfig ?? new CacheConfig(); | ||
|
|
||
| _literals = new SlidingCache<Expression, string>(useConfig); | ||
| _expressions = new SlidingCache<object, Expression>(useConfig); | ||
| } | ||
|
|
||
| public bool TryGetText(Expression expression, out string? text) | ||
| { | ||
| private static readonly ConcurrentDictionary<object, Expression> Expressions = new(); | ||
| private static readonly ConcurrentDictionary<Expression, string> Literals = new(); | ||
| return _literals.TryGetValue(expression, out text); | ||
| } | ||
|
|
||
| public static bool TryGetText(Expression expression, out string? text) | ||
| public Expression CreateLiteral(object value, string text) | ||
| { | ||
| if (_expressions.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); | ||
| } | ||
| _expressions.AddOrUpdate(value, constantExpression); | ||
| _literals.AddOrUpdate(constantExpression, text); | ||
|
|
||
| return Expressions[value]; | ||
| } | ||
| return constantExpression; | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/System.Linq.Dynamic.Core/Parser/ConstantExpressionHelperFactory.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,20 @@ | ||
| namespace System.Linq.Dynamic.Core.Parser; | ||
|
|
||
| internal static class ConstantExpressionHelperFactory | ||
| { | ||
| private static readonly object Lock = new(); | ||
| private static ConstantExpressionHelper? _instance; | ||
|
|
||
| public static ConstantExpressionHelper GetInstance(ParsingConfig config) | ||
| { | ||
| if (_instance == null) | ||
| { | ||
| lock (Lock) | ||
| { | ||
| _instance ??= new ConstantExpressionHelper(config); | ||
| } | ||
| } | ||
|
|
||
| return _instance; | ||
| } | ||
| } |
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,27 @@ | ||
| namespace System.Linq.Dynamic.Core.Util.Cache; | ||
|
|
||
| /// <summary> | ||
| /// Cache Configuration Options | ||
| /// </summary> | ||
| public class CacheConfig | ||
| { | ||
| /// <summary> | ||
| /// Sets a Time-To-Live (TTL) for items in the constant expression cache to prevent uncontrolled growth. | ||
| /// Items not accessed within this TTL will be expired, allowing garbage collection to reclaim the memory. | ||
| /// Default is 10 minutes. | ||
| /// </summary> | ||
| public TimeSpan TimeToLive { get; set; } = TimeSpan.FromMinutes(10); | ||
|
|
||
| /// <summary> | ||
| /// Configures the minimum number of items required in the constant expression cache before triggering cleanup. | ||
| /// This prevents frequent cleanups, especially in caches with few items. | ||
| /// A default value of null implies that cleanup is always allowed to run, helping in timely removal of unused cache items. | ||
| /// </summary> | ||
| public int? MinItemsTrigger { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Sets the frequency for running the cleanup process in the Constant Expression cache. | ||
| /// By default, cleanup occurs every 10 minutes. | ||
| /// </summary> | ||
| public TimeSpan CleanupFrequency { get; set; } = TimeSpan.FromMinutes(10); | ||
| } | ||
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 CacheEntry<TValue> where TValue : notnull | ||
| { | ||
| public TValue Value { get; } | ||
|
|
||
| public DateTime ExpirationTime { get; } | ||
|
|
||
| public CacheEntry(TValue value, DateTime expirationTime) | ||
| { | ||
| Value = value; | ||
| ExpirationTime = expirationTime; | ||
| } | ||
| } |
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.