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
1 change: 0 additions & 1 deletion samples/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi" Version="5.3.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.10" />
Expand Down
5 changes: 4 additions & 1 deletion src/AzureCosmos/AzureCosmosCache.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Microsoft.Azure.Cosmos;
using ActionCache.Common;
Expand Down Expand Up @@ -40,6 +41,7 @@ public AzureCosmosActionCache(Container cache, ActionCacheContext<NullCacheLock>
/// </summary>
/// <param name="key">The key of the cache entry.</param>
/// <returns>The cached value or null if not found.</returns>
#pragma warning disable CS8609
public override async Task<TValue> GetAsync<TValue>(string key)
{
try
Expand Down Expand Up @@ -76,13 +78,14 @@ await Cache.ReplaceItemAsync(
return default!;
}
}
#pragma warning restore CS8609

/// <summary>
/// Asynchronously sets a value in the cache.
/// </summary>
/// <param name="key">The cache key to set the value for.</param>
/// <param name="value">The value to set in the cache.</param>
public override Task SetAsync<TValue>(string key, TValue value)
public override Task SetAsync<TValue>(string key, [AllowNull] TValue value)
{
var (absoluteExpiration, slidingExpiration, ttl) = EntryOptions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ internal static bool TryGetKey(this EndpointFilterInvocationContext context, out
{
var parameters = endpoint.RequestDelegate?.Method.GetParameters() ?? [];
var actionArguments = parameters?
.Zip(context.Arguments, (parameter, argument) => (parameter.Name, argument))?
.ToDictionary();
.Zip(context.Arguments, (parameter, argument) => (parameter.Name, argument))
.Where(pair => pair.Name is not null)
.ToDictionary(pair => pair.Name!, pair => pair.argument);

key = new ActionCacheKeyBuilder()
.WithRouteValues(context.HttpContext.GetRouteData().Values)
.WithActionArguments(actionArguments)
.WithActionArguments(actionArguments ?? [])
.Build();

if (string.IsNullOrWhiteSpace(key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private class EndpointFilterInvocationContextSource
/// <param name="context">The filter invocation context.</param>
/// <param name="attribute">When this method returns, contains the attribute if found; otherwise, <c>null</c>.</param>
/// <returns><c>true</c> if the attribute was found; otherwise, <c>false</c>.</returns>
internal bool TryGetValue<T>(EndpointFilterInvocationContext context, out T? attribute) where T : Attribute
internal bool TryGetValue<T>(EndpointFilterInvocationContext context, [NotNullWhen(true)] out T? attribute) where T : Attribute
{
var endpoint = context.HttpContext.GetEndpoint();
if (endpoint is null)
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Keys/ActionCacheKeyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ActionCacheKeyBuilder WithRouteValues(RouteValueDictionary routeValues)
/// </summary>
/// <param name="actionArguments">Arguments for the action.</param>
/// <returns>Returns itself for chaining.</returns>
public ActionCacheKeyBuilder WithActionArguments(IDictionary<string, object?> actionArguments)
public ActionCacheKeyBuilder WithActionArguments(IDictionary<string, object?>? actionArguments)
{
if (actionArguments is null) return this;
KeyComponents.ActionArguments = actionArguments.ToDictionary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ JsonSerializer serializer
foreach (var property in obj.Properties())
{
var value = ConvertToken(property.Value, serializer);
result.Add(property.Name, value);
result.Add(property.Name, value!);
}

return result;
Expand Down
5 changes: 4 additions & 1 deletion src/Memory/MemoryActionCache.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using ActionCache.Common.Caching;
using ActionCache.Common.Concurrency.Locks;
using ActionCache.Memory.Extensions.Internal;
Expand Down Expand Up @@ -55,15 +56,17 @@ private MemoryCacheEntryOptions CreateEntryOptions() =>
/// </summary>
/// <param name="key">The key of the cache entry.</param>
/// <returns>The cached value or null if not found.</returns>
#pragma warning disable CS8609, CS8619
public override Task<TValue> GetAsync<TValue>(string key) =>
Task.FromResult(Cache.Get<TValue>(Namespace.Create(key)));
#pragma warning restore CS8609, CS8619

/// <summary>
/// Asynchronously sets a value in the cache.
/// </summary>
/// <param name="key">The cache key to set the value for.</param>
/// <param name="value">The value to set in the cache.</param>
public override Task SetAsync<TValue>(string key, TValue value)
public override Task SetAsync<TValue>(string key, [AllowNull] TValue value)
{
var entryOptions = CreateEntryOptions();
Cache.Set(Namespace.Create(key), value, entryOptions);
Expand Down
7 changes: 5 additions & 2 deletions src/Redis/RedisActionCache.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using ActionCache.Common;
using ActionCache.Common.Caching;
using ActionCache.Common.Concurrency.Locks;
Expand Down Expand Up @@ -75,7 +76,7 @@ public override async Task RemoveAsync()
/// </summary>
/// <param name="key">The key of the item to set in the cache.</param>
/// <param name="value">The value of the item to set in the cache.</param>
public override async Task SetAsync<TValue>(string key, TValue value)
public override async Task SetAsync<TValue>(string key, [AllowNull] TValue value)
{
RedisValue redisValue = CacheJsonSerializer.Serialize(value);

Expand Down Expand Up @@ -111,6 +112,7 @@ await Cache.HashSetAsync(Namespace.Create(key),
/// </summary>
/// <param name="key">The key of the item to retrieve from the cache.</param>
/// <returns>The cached item if found, otherwise default.</returns>
#pragma warning disable CS8609
public override async Task<TValue> GetAsync<TValue>(string key)
{
var namespaceKey = Namespace.Create(key);
Expand All @@ -127,7 +129,7 @@ public override async Task<TValue> GetAsync<TValue>(string key)
await Cache.SortedSetRemoveAsync((string)Namespace, key);
return default!;
}

if (ActionCacheEntryOptions.HasSlidingExpiration(hashEntries.GetSlidingExpiration()))
{
await Cache.KeyExpireAsync(namespaceKey, TimeSpan.FromMilliseconds(hashEntries.GetSlidingExpiration()), CommandFlags.FireAndForget);
Expand All @@ -143,6 +145,7 @@ public override async Task<TValue> GetAsync<TValue>(string key)
return CacheJsonSerializer.Deserialize<TValue>(jsonValue)!;
}
}
#pragma warning restore CS8609

/// <inheritdoc/>
public override async Task<IEnumerable<string>> GetKeysAsync()
Expand Down
5 changes: 4 additions & 1 deletion src/SqlServer/SqlServerActionCache.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using ActionCache.Common.Caching;
using ActionCache.SqlServer.Concurrency.Locks;
using ActionCache.Common.Serialization;
Expand Down Expand Up @@ -45,6 +46,7 @@ private DistributedCacheEntryOptions CreateEntryOptions() =>
/// <typeparam name="TValue">The type of the cached value.</typeparam>
/// <param name="key">The key of the cache entry.</param>
/// <returns>The cached value or the default value of the type if not found.</returns>
#pragma warning disable CS8609
public override async Task<TValue> GetAsync<TValue>(string key)
{
var json = await Cache.GetStringAsync(Namespace.Create(key));
Expand All @@ -57,14 +59,15 @@ public override async Task<TValue> GetAsync<TValue>(string key)
return CacheJsonSerializer.Deserialize<TValue>(json)!;
}
}
#pragma warning restore CS8609

/// <summary>
/// Asynchronously sets a value in the cache.
/// </summary>
/// <typeparam name="TValue">The type of the value to set in the cache.</typeparam>
/// <param name="key">The cache key to set the value for.</param>
/// <param name="value">The value to set in the cache.</param>
public override async Task SetAsync<TValue>(string key, TValue value)
public override async Task SetAsync<TValue>(string key, [AllowNull] TValue value)
{
var entryOptions = CreateEntryOptions();
await Cache.SetStringAsync(Namespace.Create(key), CacheJsonSerializer.Serialize(value), entryOptions);
Expand Down
34 changes: 16 additions & 18 deletions test/Integration/ActionCache/Test_ActionCache_CacheStatus_Add.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
using System.Reflection;
using ActionCache;
using ActionCache.Common.Enums;
using ActionCache.Common.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;

[TestFixture]
public class Test_ActionCache_CacheStatus_Add
{
TestServer Server;
WebApplication App;
HttpClient Client;

[SetUp]
public void Setup()
public async Task Setup()
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddMvc();
services.AddActionCache(options => options.UseRedisCache("127.0.0.1:6379"));
})
.Configure(app =>
{
app.UseHttpsRedirection();
app.UseRouting();
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseTestServer();
builder.Services.AddMvc()
.AddApplicationPart(Assembly.GetExecutingAssembly());
builder.Services.AddActionCache(options => options.UseRedisCache("127.0.0.1:6379"));

app.UseEndpoints(options => options.MapControllers());
});
App = builder.Build();
App.UseHttpsRedirection();
App.UseRouting();
App.UseEndpoints(options => options.MapControllers());

Check warning on line 27 in test/Integration/ActionCache/Test_ActionCache_CacheStatus_Add.cs

View workflow job for this annotation

GitHub Actions / Integration Tests

Suggest using top level route registrations instead of UseEndpoints (https://aka.ms/aspnet/analyzers)

Server = new TestServer(builder);
Client = Server.CreateClient();
await App.StartAsync();
Client = App.GetTestServer().CreateClient();
}

[Test]
Expand Down Expand Up @@ -64,8 +61,9 @@
[TearDown]
public async Task TearDown()
{
var cacheFactory = Server.Services.GetRequiredService<IActionCacheFactory>();
var cacheFactory = App.Services.GetRequiredService<IActionCacheFactory>();
var cache = cacheFactory.Create("Users");
await cache!.RemoveAsync();
await App.StopAsync();
}
}
38 changes: 18 additions & 20 deletions test/Integration/ActionCache/Test_ActionCache_CacheStatus_Evict.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
using System.Reflection;
using ActionCache;
using ActionCache.Common.Enums;
using ActionCache.Common.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;

[TestFixture]
public class Test_ActionCache_Eviction
{
TestServer Server;
WebApplication App;
HttpClient Client;

[SetUp]
public void Setup()
public async Task Setup()
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddMvc();
services.AddActionCache(options => options.UseRedisCache("127.0.0.1:6379"));
})
.Configure(app =>
{
app.UseHttpsRedirection();
app.UseRouting();

app.UseEndpoints(options => options.MapControllers());
});

Server = new TestServer(builder);
Client = Server.CreateClient();
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseTestServer();
builder.Services.AddMvc()
.AddApplicationPart(Assembly.GetExecutingAssembly());
builder.Services.AddActionCache(options => options.UseRedisCache("127.0.0.1:6379"));

App = builder.Build();
App.UseHttpsRedirection();
App.UseRouting();
App.UseEndpoints(options => options.MapControllers());

Check warning on line 27 in test/Integration/ActionCache/Test_ActionCache_CacheStatus_Evict.cs

View workflow job for this annotation

GitHub Actions / Integration Tests

Suggest using top level route registrations instead of UseEndpoints (https://aka.ms/aspnet/analyzers)

await App.StartAsync();
Client = App.GetTestServer().CreateClient();
}

[Test]
Expand All @@ -57,8 +54,9 @@
[TearDown]
public async Task TearDown()
{
var cacheFactory = Server.Services.GetRequiredService<IActionCacheFactory>();
var cacheFactory = App.Services.GetRequiredService<IActionCacheFactory>();
var cache = cacheFactory.Create("Users");
await cache!.RemoveAsync();
await App.StopAsync();
}
}
38 changes: 18 additions & 20 deletions test/Integration/ActionCache/Test_ActionCache_CacheStatus_Hit.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
using System.Reflection;
using ActionCache;
using ActionCache.Common.Enums;
using ActionCache.Common.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;

[TestFixture]
public class Test_ActionCache_CacheStatus_Hit
{
TestServer Server;
WebApplication App;
HttpClient Client;

[SetUp]
public void Setup()
public async Task Setup()
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddMvc();
services.AddActionCache(options => options.UseRedisCache("127.0.0.1:6379"));
})
.Configure(app =>
{
app.UseHttpsRedirection();
app.UseRouting();

app.UseEndpoints(options => options.MapControllers());
});

Server = new TestServer(builder);
Client = Server.CreateClient();
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseTestServer();
builder.Services.AddMvc()
.AddApplicationPart(Assembly.GetExecutingAssembly());
builder.Services.AddActionCache(options => options.UseRedisCache("127.0.0.1:6379"));

App = builder.Build();
App.UseHttpsRedirection();
App.UseRouting();
App.UseEndpoints(options => options.MapControllers());

Check warning on line 27 in test/Integration/ActionCache/Test_ActionCache_CacheStatus_Hit.cs

View workflow job for this annotation

GitHub Actions / Integration Tests

Suggest using top level route registrations instead of UseEndpoints (https://aka.ms/aspnet/analyzers)

await App.StartAsync();
Client = App.GetTestServer().CreateClient();
}

[Test]
Expand All @@ -50,8 +47,9 @@
[TearDown]
public async Task TearDown()
{
var cacheFactory = Server.Services.GetRequiredService<IActionCacheFactory>();
var cacheFactory = App.Services.GetRequiredService<IActionCacheFactory>();
var cache = cacheFactory.Create("Users");
await cache!.RemoveAsync();
await App.StopAsync();
}
}
Loading
Loading