|
1 | 1 | using System.Linq.Dynamic.Core.Util.Cache;
|
| 2 | +using System.Linq.Dynamic.Core.Validation; |
2 | 3 | using System.Linq.Expressions;
|
3 | 4 |
|
4 | 5 | namespace System.Linq.Dynamic.Core.Parser;
|
5 | 6 |
|
6 | 7 | internal class ConstantExpressionHelper
|
7 | 8 | {
|
8 |
| - private readonly ParsingConfig _config; |
9 |
| - |
10 | 9 | // Static shared instance to prevent duplications of the same objects
|
11 |
| - private static ThreadSafeSlidingCache<object, Expression>? _expressions; |
12 |
| - private static ThreadSafeSlidingCache<Expression, string>? _literals; |
| 10 | + private readonly ThreadSafeSlidingCache<object, Expression> _expressions; |
| 11 | + private readonly ThreadSafeSlidingCache<Expression, string> _literals; |
13 | 12 |
|
14 | 13 | public ConstantExpressionHelper(ParsingConfig config)
|
15 | 14 | {
|
16 |
| - _config = config; |
17 |
| - |
18 |
| - } |
| 15 | + var parsingConfig = Check.NotNull(config); |
| 16 | + var cacheConfig = Check.NotNull(parsingConfig.ConstantExpressionCacheConfig); |
19 | 17 |
|
20 |
| - private ThreadSafeSlidingCache<Expression, string> GetLiterals() |
21 |
| - { |
22 |
| - _literals ??= new ThreadSafeSlidingCache<Expression, string>( |
23 |
| - _config.ConstantExpressionSlidingCacheTimeToLive, |
24 |
| - _config.ConstantExpressionSlidingCacheCleanupFrequency, |
25 |
| - _config.ConstantExpressionSlidingCacheMinItemsTrigger |
| 18 | + _literals = new ThreadSafeSlidingCache<Expression, string>( |
| 19 | + cacheConfig.TimeToLive, |
| 20 | + cacheConfig.CleanupFrequency, |
| 21 | + cacheConfig.MinItemsTrigger |
26 | 22 | );
|
27 |
| - return _literals; |
28 |
| - } |
29 | 23 |
|
30 |
| - private ThreadSafeSlidingCache<object, Expression> GetExpression() |
31 |
| - { |
32 |
| - _expressions ??= new ThreadSafeSlidingCache<object, Expression>( |
33 |
| - _config.ConstantExpressionSlidingCacheTimeToLive, |
34 |
| - _config.ConstantExpressionSlidingCacheCleanupFrequency, |
35 |
| - _config.ConstantExpressionSlidingCacheMinItemsTrigger |
| 24 | + _expressions = new ThreadSafeSlidingCache<object, Expression>( |
| 25 | + cacheConfig.TimeToLive, |
| 26 | + cacheConfig.CleanupFrequency, |
| 27 | + cacheConfig.MinItemsTrigger |
36 | 28 | );
|
37 |
| - return _expressions; |
38 | 29 | }
|
39 | 30 |
|
40 |
| - |
41 | 31 | public bool TryGetText(Expression expression, out string? text)
|
42 | 32 | {
|
43 |
| - return GetLiterals().TryGetValue(expression, out text); |
| 33 | + return _literals.TryGetValue(expression, out text); |
44 | 34 | }
|
45 | 35 |
|
46 | 36 | public Expression CreateLiteral(object value, string text)
|
47 | 37 | {
|
48 |
| - if (GetExpression().TryGetValue(value, out var outputValue)) |
| 38 | + if (_expressions.TryGetValue(value, out var outputValue)) |
49 | 39 | {
|
50 | 40 | return outputValue;
|
51 | 41 | }
|
52 | 42 |
|
53 | 43 | var constantExpression = Expression.Constant(value);
|
54 | 44 |
|
55 |
| - GetExpression().AddOrUpdate(value, constantExpression); |
56 |
| - GetLiterals().AddOrUpdate(constantExpression, text); |
| 45 | + _expressions.AddOrUpdate(value, constantExpression); |
| 46 | + _literals.AddOrUpdate(constantExpression, text); |
57 | 47 |
|
58 | 48 | return constantExpression;
|
59 | 49 | }
|
|
0 commit comments