Skip to content
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

fix: Fix Issues-617 #619

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Masa.BuildingBlocks.Caching;

public class CacheEntry<T> : CacheEntryOptions<T>
public class CacheEntry<T> : CacheEntryOptionsBase
{
public T? Value { get; }

Expand All @@ -12,9 +12,9 @@ public CacheEntry(T? value)
Value = value;
}

public CacheEntry(T value, DateTimeOffset absoluteExpiration) : this(value)
public CacheEntry(T? value, DateTimeOffset absoluteExpiration) : this(value)
=> AbsoluteExpiration = absoluteExpiration;

public CacheEntry(T value, TimeSpan absoluteExpirationRelativeToNow) : this(value)
public CacheEntry(T? value, TimeSpan absoluteExpirationRelativeToNow) : this(value)
=> AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Masa.BuildingBlocks.Caching;

internal class DefaultDistributedCacheClient : DefaultCacheClient, IManualDistributedCacheClient
internal sealed class DefaultDistributedCacheClient : DefaultCacheClient, IManualDistributedCacheClient
{
private readonly IManualDistributedCacheClient _cacheClient;
public DefaultDistributedCacheClient(IManualDistributedCacheClient cacheClient) : base(cacheClient) => _cacheClient = cacheClient;
Expand Down Expand Up @@ -155,13 +155,7 @@ public Task<long> KeyExpireAsync<T>(IEnumerable<string> keys, CacheEntryOptions?
#pragma warning disable S3881
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
//don't need to be released
}
#pragma warning restore S3881
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Masa.BuildingBlocks.Caching;

internal class DefaultMultilevelCacheClient : DefaultCacheClient, IManualMultilevelCacheClient
internal sealed class DefaultMultilevelCacheClient : DefaultCacheClient, IManualMultilevelCacheClient
{
private readonly IManualMultilevelCacheClient _cacheClient;

Expand Down Expand Up @@ -115,13 +115,7 @@ public Task SetListAsync<T>(Dictionary<string, T?> keyValues, CombinedCacheEntry
#pragma warning disable S3881
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
//don't need to be released
}
#pragma warning restore S3881
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

[assembly: InternalsVisibleTo("Masa.Contrib.Caching.Distributed.StackExchangeRedis")]
[assembly: InternalsVisibleTo("Masa.Contrib.Caching.MultilevelCache")]

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.Caching;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,66 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace
namespace Masa.BuildingBlocks.Caching
{
public class CacheEntryOptions
{
private TimeSpan? _absoluteExpirationRelativeToNow;
private TimeSpan? _slidingExpiration;

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

/// <summary>
/// Gets or sets an absolute expiration time, relative to now.
/// When coexisting with AbsoluteExpiration, use AbsoluteExpirationRelativeToNow first
/// </summary>
public TimeSpan? AbsoluteExpirationRelativeToNow
{
get => _absoluteExpirationRelativeToNow;
set
{
if (value <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(
nameof(AbsoluteExpirationRelativeToNow),
value,
"The relative expiration value must be positive.");
}
public class CacheEntryOptions
{
private TimeSpan? _absoluteExpirationRelativeToNow;
private TimeSpan? _slidingExpiration;

_absoluteExpirationRelativeToNow = value;
}
}
/// <summary>
/// Gets or sets an absolute expiration date for the cache entry.
/// When coexisting with AbsoluteExpirationRelativeToNow, use AbsoluteExpirationRelativeToNow first
/// </summary>
public DateTimeOffset? AbsoluteExpiration { get; set; }

/// <summary>
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed.
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
/// </summary>
public TimeSpan? SlidingExpiration
/// <summary>
/// Gets or sets an absolute expiration time, relative to now.
/// When coexisting with AbsoluteExpiration, use AbsoluteExpirationRelativeToNow first
/// </summary>
public TimeSpan? AbsoluteExpirationRelativeToNow
{
get => _absoluteExpirationRelativeToNow;
set
{
get => _slidingExpiration;
set
if (value <= TimeSpan.Zero)
{
if (value <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(
nameof(SlidingExpiration),
value,
"The sliding expiration value must be positive.");
}
_slidingExpiration = value;
throw new ArgumentOutOfRangeException(
nameof(AbsoluteExpirationRelativeToNow),
value,
"The relative expiration value must be positive.");
}
}

public CacheEntryOptions() { }

public CacheEntryOptions(DateTimeOffset? absoluteExpiration)
=> AbsoluteExpiration = absoluteExpiration;

public CacheEntryOptions(TimeSpan? absoluteExpirationRelativeToNow)
=> AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
_absoluteExpirationRelativeToNow = value;
}
}

public class CacheEntryOptions<T> : CacheEntryOptions
/// <summary>
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed.
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
/// </summary>
public TimeSpan? SlidingExpiration
{
public Action<T?>? ValueChanged { get; set; }

public CacheEntryOptions()
get => _slidingExpiration;
set
{
if (value <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(
nameof(SlidingExpiration),
value,
"The sliding expiration value must be positive.");
}
_slidingExpiration = value;
}
}

public CacheEntryOptions(DateTimeOffset? absoluteExpiration) : base(absoluteExpiration)
{
}
public CacheEntryOptions() { }

public CacheEntryOptions(TimeSpan? absoluteExpirationRelativeToNow) : base(absoluteExpirationRelativeToNow)
{
public CacheEntryOptions(DateTimeOffset? absoluteExpiration)
=> AbsoluteExpiration = absoluteExpiration;

}
}
public CacheEntryOptions(TimeSpan? absoluteExpirationRelativeToNow)
=> AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.Caching;

public abstract class CacheEntryOptionsBase
{
#region Set the expiration time to ensure that the writing method remains unchanged

/// <summary>
/// Gets or sets an absolute expiration date for the cache entry.
/// When coexisting with AbsoluteExpirationRelativeToNow, use AbsoluteExpirationRelativeToNow first
/// </summary>
public DateTimeOffset? AbsoluteExpiration
{
get => CacheOptions?.AbsoluteExpiration;
set
{
CacheOptions ??= new CacheEntryOptions();
CacheOptions.AbsoluteExpiration = value;
}
}

/// <summary>
/// Gets or sets an absolute expiration time, relative to now.
/// When coexisting with AbsoluteExpiration, use AbsoluteExpirationRelativeToNow first
/// </summary>
public TimeSpan? AbsoluteExpirationRelativeToNow
{
get => CacheOptions?.AbsoluteExpirationRelativeToNow;
set
{
CacheOptions ??= new CacheEntryOptions();
CacheOptions.AbsoluteExpirationRelativeToNow = value;
}
}

/// <summary>
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed.
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
/// </summary>
public TimeSpan? SlidingExpiration
{
get => CacheOptions?.SlidingExpiration;
set
{
CacheOptions ??= new CacheEntryOptions();
CacheOptions.SlidingExpiration = value;
}
}

#endregion

public CacheEntryOptions? CacheOptions { get; set; }

protected CacheEntryOptionsBase()
{
CacheOptions = null;
}

protected CacheEntryOptionsBase(DateTimeOffset? absoluteExpiration) : this()
{
AbsoluteExpiration = absoluteExpiration;
}

protected CacheEntryOptionsBase(TimeSpan? absoluteExpirationRelativeToNow) : this()
{
AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private async Task<List<DataCacheModel<T>>> GetListAsync<T>(List<string> keys)
if (cacheEntry.Value == null)
return default;

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

await SetCoreAsync(key, cacheEntry.Value, cacheEntry).ConfigureAwait(false);
await SetCoreAsync(key, cacheEntry.Value, cacheEntry.CacheOptions).ConfigureAwait(false);
return cacheEntry.Value;
}).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ protected RedisCacheClientBase(

private RedisCacheClientBase(
CacheOptions globalCacheOptions,
CacheEntryOptions expiredEntryOptions,
CacheEntryOptions globalExpiredOptions,
JsonSerializerOptions? jsonSerializerOptions)
{
_globalCacheOptions = globalCacheOptions;
_globalCacheEntryOptions = new CacheEntryOptions
{
AbsoluteExpiration = expiredEntryOptions.AbsoluteExpiration,
AbsoluteExpirationRelativeToNow = expiredEntryOptions.AbsoluteExpirationRelativeToNow,
SlidingExpiration = expiredEntryOptions.SlidingExpiration
AbsoluteExpiration = globalExpiredOptions.AbsoluteExpiration,
AbsoluteExpirationRelativeToNow = globalExpiredOptions.AbsoluteExpirationRelativeToNow,
SlidingExpiration = globalExpiredOptions.SlidingExpiration
};
GlobalJsonSerializerOptions = jsonSerializerOptions ?? new JsonSerializerOptions().EnableDynamicTypes();
}
Expand Down
Loading