Skip to content

Commit 1fcbedd

Browse files
authored
fix: Fix Issues-617 (#619)
* fix: Fixed Issues-617 * chore: Fix code smell
1 parent 2adb30d commit 1fcbedd

File tree

16 files changed

+233
-108
lines changed

16 files changed

+233
-108
lines changed

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/CacheEntry.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Masa.BuildingBlocks.Caching;
55

6-
public class CacheEntry<T> : CacheEntryOptions<T>
6+
public class CacheEntry<T> : CacheEntryOptionsBase
77
{
88
public T? Value { get; }
99

@@ -12,9 +12,9 @@ public CacheEntry(T? value)
1212
Value = value;
1313
}
1414

15-
public CacheEntry(T value, DateTimeOffset absoluteExpiration) : this(value)
15+
public CacheEntry(T? value, DateTimeOffset absoluteExpiration) : this(value)
1616
=> AbsoluteExpiration = absoluteExpiration;
1717

18-
public CacheEntry(T value, TimeSpan absoluteExpirationRelativeToNow) : this(value)
18+
public CacheEntry(T? value, TimeSpan absoluteExpirationRelativeToNow) : this(value)
1919
=> AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
2020
}

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/DefaultDistributedCacheClient.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Masa.BuildingBlocks.Caching;
99

10-
internal class DefaultDistributedCacheClient : DefaultCacheClient, IManualDistributedCacheClient
10+
internal sealed class DefaultDistributedCacheClient : DefaultCacheClient, IManualDistributedCacheClient
1111
{
1212
private readonly IManualDistributedCacheClient _cacheClient;
1313
public DefaultDistributedCacheClient(IManualDistributedCacheClient cacheClient) : base(cacheClient) => _cacheClient = cacheClient;
@@ -155,13 +155,7 @@ public Task<long> KeyExpireAsync<T>(IEnumerable<string> keys, CacheEntryOptions?
155155
#pragma warning disable S3881
156156
public void Dispose()
157157
{
158-
Dispose(true);
159158
GC.SuppressFinalize(this);
160159
}
161-
162-
protected virtual void Dispose(bool disposing)
163-
{
164-
//don't need to be released
165-
}
166160
#pragma warning restore S3881
167161
}

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/DefaultMultilevelCacheClient.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Masa.BuildingBlocks.Caching;
99

10-
internal class DefaultMultilevelCacheClient : DefaultCacheClient, IManualMultilevelCacheClient
10+
internal sealed class DefaultMultilevelCacheClient : DefaultCacheClient, IManualMultilevelCacheClient
1111
{
1212
private readonly IManualMultilevelCacheClient _cacheClient;
1313

@@ -115,13 +115,7 @@ public Task SetListAsync<T>(Dictionary<string, T?> keyValues, CombinedCacheEntry
115115
#pragma warning disable S3881
116116
public void Dispose()
117117
{
118-
Dispose(true);
119118
GC.SuppressFinalize(this);
120119
}
121-
122-
protected virtual void Dispose(bool disposing)
123-
{
124-
//don't need to be released
125-
}
126120
#pragma warning restore S3881
127121
}

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/ServiceCollectionExtensions.Core.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
[assembly: InternalsVisibleTo("Masa.Contrib.Caching.Distributed.StackExchangeRedis")]
55
[assembly: InternalsVisibleTo("Masa.Contrib.Caching.MultilevelCache")]
6+
67
// ReSharper disable once CheckNamespace
78

89
namespace Masa.BuildingBlocks.Caching;

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Options/CacheEntryOptions.cs

+46-64
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,66 @@
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

44
// ReSharper disable once CheckNamespace
5-
namespace Masa.BuildingBlocks.Caching
6-
{
7-
public class CacheEntryOptions
8-
{
9-
private TimeSpan? _absoluteExpirationRelativeToNow;
10-
private TimeSpan? _slidingExpiration;
115

12-
/// <summary>
13-
/// Gets or sets an absolute expiration date for the cache entry.
14-
/// When coexisting with AbsoluteExpirationRelativeToNow, use AbsoluteExpirationRelativeToNow first
15-
/// </summary>
16-
public DateTimeOffset? AbsoluteExpiration { get; set; }
6+
namespace Masa.BuildingBlocks.Caching;
177

18-
/// <summary>
19-
/// Gets or sets an absolute expiration time, relative to now.
20-
/// When coexisting with AbsoluteExpiration, use AbsoluteExpirationRelativeToNow first
21-
/// </summary>
22-
public TimeSpan? AbsoluteExpirationRelativeToNow
23-
{
24-
get => _absoluteExpirationRelativeToNow;
25-
set
26-
{
27-
if (value <= TimeSpan.Zero)
28-
{
29-
throw new ArgumentOutOfRangeException(
30-
nameof(AbsoluteExpirationRelativeToNow),
31-
value,
32-
"The relative expiration value must be positive.");
33-
}
8+
public class CacheEntryOptions
9+
{
10+
private TimeSpan? _absoluteExpirationRelativeToNow;
11+
private TimeSpan? _slidingExpiration;
3412

35-
_absoluteExpirationRelativeToNow = value;
36-
}
37-
}
13+
/// <summary>
14+
/// Gets or sets an absolute expiration date for the cache entry.
15+
/// When coexisting with AbsoluteExpirationRelativeToNow, use AbsoluteExpirationRelativeToNow first
16+
/// </summary>
17+
public DateTimeOffset? AbsoluteExpiration { get; set; }
3818

39-
/// <summary>
40-
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed.
41-
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
42-
/// </summary>
43-
public TimeSpan? SlidingExpiration
19+
/// <summary>
20+
/// Gets or sets an absolute expiration time, relative to now.
21+
/// When coexisting with AbsoluteExpiration, use AbsoluteExpirationRelativeToNow first
22+
/// </summary>
23+
public TimeSpan? AbsoluteExpirationRelativeToNow
24+
{
25+
get => _absoluteExpirationRelativeToNow;
26+
set
4427
{
45-
get => _slidingExpiration;
46-
set
28+
if (value <= TimeSpan.Zero)
4729
{
48-
if (value <= TimeSpan.Zero)
49-
{
50-
throw new ArgumentOutOfRangeException(
51-
nameof(SlidingExpiration),
52-
value,
53-
"The sliding expiration value must be positive.");
54-
}
55-
_slidingExpiration = value;
30+
throw new ArgumentOutOfRangeException(
31+
nameof(AbsoluteExpirationRelativeToNow),
32+
value,
33+
"The relative expiration value must be positive.");
5634
}
57-
}
58-
59-
public CacheEntryOptions() { }
60-
61-
public CacheEntryOptions(DateTimeOffset? absoluteExpiration)
62-
=> AbsoluteExpiration = absoluteExpiration;
6335

64-
public CacheEntryOptions(TimeSpan? absoluteExpirationRelativeToNow)
65-
=> AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
36+
_absoluteExpirationRelativeToNow = value;
37+
}
6638
}
6739

68-
public class CacheEntryOptions<T> : CacheEntryOptions
40+
/// <summary>
41+
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed.
42+
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
43+
/// </summary>
44+
public TimeSpan? SlidingExpiration
6945
{
70-
public Action<T?>? ValueChanged { get; set; }
71-
72-
public CacheEntryOptions()
46+
get => _slidingExpiration;
47+
set
7348
{
49+
if (value <= TimeSpan.Zero)
50+
{
51+
throw new ArgumentOutOfRangeException(
52+
nameof(SlidingExpiration),
53+
value,
54+
"The sliding expiration value must be positive.");
55+
}
56+
_slidingExpiration = value;
7457
}
58+
}
7559

76-
public CacheEntryOptions(DateTimeOffset? absoluteExpiration) : base(absoluteExpiration)
77-
{
78-
}
60+
public CacheEntryOptions() { }
7961

80-
public CacheEntryOptions(TimeSpan? absoluteExpirationRelativeToNow) : base(absoluteExpirationRelativeToNow)
81-
{
62+
public CacheEntryOptions(DateTimeOffset? absoluteExpiration)
63+
=> AbsoluteExpiration = absoluteExpiration;
8264

83-
}
84-
}
65+
public CacheEntryOptions(TimeSpan? absoluteExpirationRelativeToNow)
66+
=> AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
8567
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Masa.BuildingBlocks.Caching;
7+
8+
public abstract class CacheEntryOptionsBase
9+
{
10+
#region Set the expiration time to ensure that the writing method remains unchanged
11+
12+
/// <summary>
13+
/// Gets or sets an absolute expiration date for the cache entry.
14+
/// When coexisting with AbsoluteExpirationRelativeToNow, use AbsoluteExpirationRelativeToNow first
15+
/// </summary>
16+
public DateTimeOffset? AbsoluteExpiration
17+
{
18+
get => CacheOptions?.AbsoluteExpiration;
19+
set
20+
{
21+
CacheOptions ??= new CacheEntryOptions();
22+
CacheOptions.AbsoluteExpiration = value;
23+
}
24+
}
25+
26+
/// <summary>
27+
/// Gets or sets an absolute expiration time, relative to now.
28+
/// When coexisting with AbsoluteExpiration, use AbsoluteExpirationRelativeToNow first
29+
/// </summary>
30+
public TimeSpan? AbsoluteExpirationRelativeToNow
31+
{
32+
get => CacheOptions?.AbsoluteExpirationRelativeToNow;
33+
set
34+
{
35+
CacheOptions ??= new CacheEntryOptions();
36+
CacheOptions.AbsoluteExpirationRelativeToNow = value;
37+
}
38+
}
39+
40+
/// <summary>
41+
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed.
42+
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
43+
/// </summary>
44+
public TimeSpan? SlidingExpiration
45+
{
46+
get => CacheOptions?.SlidingExpiration;
47+
set
48+
{
49+
CacheOptions ??= new CacheEntryOptions();
50+
CacheOptions.SlidingExpiration = value;
51+
}
52+
}
53+
54+
#endregion
55+
56+
public CacheEntryOptions? CacheOptions { get; set; }
57+
58+
protected CacheEntryOptionsBase()
59+
{
60+
CacheOptions = null;
61+
}
62+
63+
protected CacheEntryOptionsBase(DateTimeOffset? absoluteExpiration) : this()
64+
{
65+
AbsoluteExpiration = absoluteExpiration;
66+
}
67+
68+
protected CacheEntryOptionsBase(TimeSpan? absoluteExpirationRelativeToNow) : this()
69+
{
70+
AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
71+
}
72+
}

src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private async Task<List<DataCacheModel<T>>> GetListAsync<T>(List<string> keys)
9898
if (cacheEntry.Value == null)
9999
return default;
100100

101-
SetCoreAsync(key, cacheEntry.Value, cacheEntry).ConfigureAwait(false).GetAwaiter().GetResult();
101+
SetCoreAsync(key, cacheEntry.Value, cacheEntry.CacheOptions).ConfigureAwait(false).GetAwaiter().GetResult();
102102
return cacheEntry.Value;
103103
});
104104
}
@@ -120,7 +120,7 @@ private async Task<List<DataCacheModel<T>>> GetListAsync<T>(List<string> keys)
120120
if (cacheEntry.Value == null)
121121
return default;
122122

123-
await SetCoreAsync(key, cacheEntry.Value, cacheEntry).ConfigureAwait(false);
123+
await SetCoreAsync(key, cacheEntry.Value, cacheEntry.CacheOptions).ConfigureAwait(false);
124124
return cacheEntry.Value;
125125
}).ConfigureAwait(false);
126126
}

src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClientBase.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ protected RedisCacheClientBase(
3838

3939
private RedisCacheClientBase(
4040
CacheOptions globalCacheOptions,
41-
CacheEntryOptions expiredEntryOptions,
41+
CacheEntryOptions globalExpiredOptions,
4242
JsonSerializerOptions? jsonSerializerOptions)
4343
{
4444
_globalCacheOptions = globalCacheOptions;
4545
_globalCacheEntryOptions = new CacheEntryOptions
4646
{
47-
AbsoluteExpiration = expiredEntryOptions.AbsoluteExpiration,
48-
AbsoluteExpirationRelativeToNow = expiredEntryOptions.AbsoluteExpirationRelativeToNow,
49-
SlidingExpiration = expiredEntryOptions.SlidingExpiration
47+
AbsoluteExpiration = globalExpiredOptions.AbsoluteExpiration,
48+
AbsoluteExpirationRelativeToNow = globalExpiredOptions.AbsoluteExpirationRelativeToNow,
49+
SlidingExpiration = globalExpiredOptions.SlidingExpiration
5050
};
5151
GlobalJsonSerializerOptions = jsonSerializerOptions ?? new JsonSerializerOptions().EnableDynamicTypes();
5252
}

0 commit comments

Comments
 (0)