Skip to content
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
6 changes: 0 additions & 6 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@
<!-- Ignore API doc requirements for test assets -->
<NoWarn Condition="'$(IsTestAssetProject)' == 'true' or '$(IsSampleProject)' == 'true' or '$(IsBenchmarkProject)' == 'true' or
'$(IsMicrobenchmarksProject)' == 'true'">$(NoWarn);CS1591</NoWarn>

<!-- Ignore analyzers that recommend APIs introduced in .NET Core when targeting frameworks that lack those APIs
to avoid issues with multitargeting.
Some projects only build DefaultNetCoreTargetFramework when DotNetBuildFromSource is true so also suppress for source build.
-->
<NoWarn Condition="$(TargetFrameworks.Contains('NetFramework')) or $(TargetFrameworks.Contains('netstandard')) or '$(DotNetBuildFromSource)' == 'true'">$(NoWarn);CA1510;CA1511;CA1512;CA1513</NoWarn>
</PropertyGroup>

<PropertyGroup Label="Resx settings">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@
<Reference Include="Microsoft.Data.SqlClient" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
</ItemGroup>

</Project>
65 changes: 13 additions & 52 deletions src/Caching/SqlServer/src/SqlServerCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Shared;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -80,10 +81,7 @@ public SqlServerCache(IOptions<SqlServerCacheOptions> options)
/// <inheritdoc />
public byte[]? Get(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

var value = _dbOperations.GetCacheItem(key);

Expand All @@ -95,10 +93,7 @@ public SqlServerCache(IOptions<SqlServerCacheOptions> options)
/// <inheritdoc />
public async Task<byte[]?> GetAsync(string key, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

token.ThrowIfCancellationRequested();

Expand All @@ -112,10 +107,7 @@ public SqlServerCache(IOptions<SqlServerCacheOptions> options)
/// <inheritdoc />
public void Refresh(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

_dbOperations.RefreshCacheItem(key);

Expand All @@ -125,10 +117,7 @@ public void Refresh(string key)
/// <inheritdoc />
public async Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

token.ThrowIfCancellationRequested();

Expand All @@ -140,10 +129,7 @@ public void Refresh(string key)
/// <inheritdoc />
public void Remove(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

_dbOperations.DeleteCacheItem(key);

Expand All @@ -153,10 +139,7 @@ public void Remove(string key)
/// <inheritdoc />
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

token.ThrowIfCancellationRequested();

Expand All @@ -168,20 +151,9 @@ public void Remove(string key)
/// <inheritdoc />
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullThrowHelper.ThrowIfNull(key);
ArgumentNullThrowHelper.ThrowIfNull(value);
ArgumentNullThrowHelper.ThrowIfNull(options);

GetOptions(ref options);

Expand All @@ -197,20 +169,9 @@ public async Task SetAsync(
DistributedCacheEntryOptions options,
CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullThrowHelper.ThrowIfNull(key);
ArgumentNullThrowHelper.ThrowIfNull(value);
ArgumentNullThrowHelper.ThrowIfNull(options);

token.ThrowIfCancellationRequested();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.AspNetCore.Shared;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.SqlServer;

Expand All @@ -20,15 +21,8 @@ public static class SqlServerCachingServicesExtensions
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddDistributedSqlServerCache(this IServiceCollection services, Action<SqlServerCacheOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
ArgumentNullThrowHelper.ThrowIfNull(services);
ArgumentNullThrowHelper.ThrowIfNull(setupAction);

services.AddOptions();
AddSqlServerCacheServices(services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
<Reference Include="StackExchange.Redis" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ObjectDisposedThrowHelper.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
</ItemGroup>

</Project>
101 changes: 20 additions & 81 deletions src/Caching/StackExchangeRedis/src/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Shared;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -79,15 +80,8 @@ public RedisCache(IOptions<RedisCacheOptions> optionsAccessor)
/// <param name="logger">The logger.</param>
internal RedisCache(IOptions<RedisCacheOptions> optionsAccessor, ILogger logger)
{
if (optionsAccessor == null)
{
throw new ArgumentNullException(nameof(optionsAccessor));
}

if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
ArgumentNullThrowHelper.ThrowIfNull(optionsAccessor);
ArgumentNullThrowHelper.ThrowIfNull(logger);

_options = optionsAccessor.Value;
_logger = logger;
Expand All @@ -99,21 +93,15 @@ internal RedisCache(IOptions<RedisCacheOptions> optionsAccessor, ILogger logger)
/// <inheritdoc />
public byte[]? Get(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

return GetAndRefresh(key, getData: true);
}

/// <inheritdoc />
public async Task<byte[]?> GetAsync(string key, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

token.ThrowIfCancellationRequested();

Expand All @@ -123,20 +111,9 @@ internal RedisCache(IOptions<RedisCacheOptions> optionsAccessor, ILogger logger)
/// <inheritdoc />
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullThrowHelper.ThrowIfNull(key);
ArgumentNullThrowHelper.ThrowIfNull(value);
ArgumentNullThrowHelper.ThrowIfNull(options);

Connect();

Expand All @@ -157,20 +134,9 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
/// <inheritdoc />
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullThrowHelper.ThrowIfNull(key);
ArgumentNullThrowHelper.ThrowIfNull(value);
ArgumentNullThrowHelper.ThrowIfNull(options);

token.ThrowIfCancellationRequested();

Expand All @@ -194,21 +160,15 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
/// <inheritdoc />
public void Refresh(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

GetAndRefresh(key, getData: false);
}

/// <inheritdoc />
public async Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

token.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -343,10 +303,7 @@ private void TryRegisterProfiler()

private byte[]? GetAndRefresh(string key, bool getData)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

Connect();

Expand Down Expand Up @@ -379,10 +336,7 @@ private void TryRegisterProfiler()

private async Task<byte[]?> GetAndRefreshAsync(string key, bool getData, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

token.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -419,10 +373,7 @@ private void TryRegisterProfiler()
/// <inheritdoc />
public void Remove(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

Connect();

Expand All @@ -433,10 +384,7 @@ public void Remove(string key)
/// <inheritdoc />
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

await ConnectAsync(token).ConfigureAwait(false);
Debug.Assert(_cache is not null);
Expand All @@ -463,10 +411,7 @@ private static void MapMetadata(RedisValue[] results, out DateTimeOffset? absolu

private void Refresh(IDatabase cache, string key, DateTimeOffset? absExpr, TimeSpan? sldExpr)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

// Note Refresh has no effect if there is just an absolute expiration (or neither).
if (sldExpr.HasValue)
Expand All @@ -488,10 +433,7 @@ private void Refresh(IDatabase cache, string key, DateTimeOffset? absExpr, TimeS

private async Task RefreshAsync(IDatabase cache, string key, DateTimeOffset? absExpr, TimeSpan? sldExpr, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullThrowHelper.ThrowIfNull(key);

token.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -564,9 +506,6 @@ public void Dispose()

private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
ObjectDisposedThrowHelper.ThrowIf(_disposed, this);
}
}
Loading