Skip to content

Commit c8653b1

Browse files
author
Travis Whidden
committed
zzzprojects#764 - PR Refactor Feedback
1 parent 8d9b6bf commit c8653b1

File tree

6 files changed

+13
-17
lines changed

6 files changed

+13
-17
lines changed

src/System.Linq.Dynamic.Core/Parser/ConstantExpressionHelper.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ namespace System.Linq.Dynamic.Core.Parser;
66

77
internal class ConstantExpressionHelper
88
{
9-
// Static shared instance to prevent duplications of the same objects
109
private readonly SlidingCache<object, Expression> _expressions;
1110
private readonly SlidingCache<Expression, string> _literals;
1211

1312
public ConstantExpressionHelper(ParsingConfig config)
1413
{
1514
var parsingConfig = Check.NotNull(config);
16-
var cacheConfig = Check.NotNull(parsingConfig.ConstantExpressionCacheConfig);
15+
var useConfig = parsingConfig.ConstantExpressionCacheConfig ?? new CacheConfig();
1716

18-
_literals = new SlidingCache<Expression, string>(cacheConfig);
19-
_expressions = new SlidingCache<object, Expression>(cacheConfig);
17+
_literals = new SlidingCache<Expression, string>(useConfig);
18+
_expressions = new SlidingCache<object, Expression>(useConfig);
2019
}
2120

2221
public bool TryGetText(Expression expression, out string? text)

src/System.Linq.Dynamic.Core/ParsingConfig.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,5 +239,5 @@ public IQueryableAnalyzer QueryableAnalyzer
239239
/// <summary>
240240
/// Caches constant expressions to enhance performance. Periodic cleanup is performed to manage cache size, governed by this configuration.
241241
/// </summary>
242-
public CacheConfig ConstantExpressionCacheConfig { get; set; } = new CacheConfig();
242+
public CacheConfig? ConstantExpressionCacheConfig { get; set; }
243243
}

src/System.Linq.Dynamic.Core/Util/Cache/CacheConfig.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ public class CacheConfig
1212
/// </summary>
1313
public TimeSpan TimeToLive { get; set; } = TimeSpan.FromMinutes(10);
1414

15-
1615
/// <summary>
1716
/// Configures the minimum number of items required in the constant expression cache before triggering cleanup.
1817
/// This prevents frequent cleanups, especially in caches with few items.
1918
/// A default value of null implies that cleanup is always allowed to run, helping in timely removal of unused cache items.
2019
/// </summary>
21-
public int? MinItemsTrigger { get; set; } = null;
22-
20+
public int? MinItemsTrigger { get; set; }
2321

2422
/// <summary>
2523
/// Sets the frequency for running the cleanup process in the Constant Expression cache.

src/System.Linq.Dynamic.Core/Util/Cache/CacheContainer.cs src/System.Linq.Dynamic.Core/Util/Cache/CacheEntry.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace System.Linq.Dynamic.Core.Util.Cache;
22

3-
internal struct CacheContainer<TValue> where TValue : notnull
3+
internal struct CacheEntry<TValue> where TValue : notnull
44
{
55
public TValue Value { get; }
66

77
public DateTime ExpirationTime { get; }
88

9-
public CacheContainer(TValue value, DateTime expirationTime)
9+
public CacheEntry(TValue value, DateTime expirationTime)
1010
{
1111
Value = value;
1212
ExpirationTime = expirationTime;

src/System.Linq.Dynamic.Core/Util/Cache/SlidingCache.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace System.Linq.Dynamic.Core.Util.Cache;
66

77
internal class SlidingCache<TKey, TValue> where TKey : notnull where TValue : notnull
88
{
9-
private readonly ConcurrentDictionary<TKey, CacheContainer<TValue>> _cache;
9+
private readonly ConcurrentDictionary<TKey, CacheEntry<TValue>> _cache;
1010
private readonly TimeSpan _cleanupFrequency;
1111
private readonly IDateTimeUtils _dateTimeProvider;
1212
private readonly Action _deleteExpiredCachedItemsDelegate;
@@ -32,7 +32,7 @@ public SlidingCache(
3232
long? minCacheItemsBeforeCleanup = null,
3333
IDateTimeUtils? dateTimeProvider = null)
3434
{
35-
_cache = new ConcurrentDictionary<TKey, CacheContainer<TValue>>();
35+
_cache = new ConcurrentDictionary<TKey, CacheEntry<TValue>>();
3636
TimeToLive = timeToLive;
3737
_minCacheItemsBeforeCleanup = minCacheItemsBeforeCleanup;
3838
_cleanupFrequency = cleanupFrequency ?? SlidingCacheConstants.DefaultCleanupFrequency;
@@ -51,7 +51,7 @@ public SlidingCache(
5151
CacheConfig cashConfig,
5252
IDateTimeUtils? dateTimeProvider = null)
5353
{
54-
_cache = new ConcurrentDictionary<TKey, CacheContainer<TValue>>();
54+
_cache = new ConcurrentDictionary<TKey, CacheEntry<TValue>>();
5555
TimeToLive = cashConfig.TimeToLive;
5656
_minCacheItemsBeforeCleanup = cashConfig.MinItemsTrigger;
5757
_cleanupFrequency = cashConfig.CleanupFrequency;
@@ -82,7 +82,7 @@ public void AddOrUpdate(TKey key, TValue value)
8282
Check.NotNull(value);
8383

8484
var expirationTime = _dateTimeProvider.UtcNow.Add(TimeToLive);
85-
_cache[key] = new CacheContainer<TValue>(value, expirationTime);
85+
_cache[key] = new CacheEntry<TValue>(value, expirationTime);
8686

8787
CleanupIfNeeded();
8888
}
@@ -105,7 +105,7 @@ public bool TryGetValue(TKey key, [NotNullWhen(true)] out TValue? value)
105105
{
106106
value = valueAndExpiration.Value;
107107
var newExpire = _dateTimeProvider.UtcNow.Add(TimeToLive);
108-
_cache[key] = new CacheContainer<TValue>(value, newExpire);
108+
_cache[key] = new CacheEntry<TValue>(value, newExpire);
109109
return true;
110110
}
111111

test/System.Linq.Dynamic.Core.Tests/Util/Cache/SlidingCacheTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,4 @@ public void ThreadSafeSlidingCache_TestMinNumberBeforeTests()
147147
// Ensure one item is in the cache
148148
cache.Count.Should().Be(1, $"Expected 1 items in the cache, had {cache.Count}");
149149
}
150-
151-
}
150+
}

0 commit comments

Comments
 (0)