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
11 changes: 6 additions & 5 deletions src/Immediate.Cache.Shared/ApplicationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public abstract class ApplicationCache<TRequest, TResponse>(
IMemoryCache memoryCache,
Owned<IHandler<TRequest, TResponse>> handler
)
where TRequest : class
where TResponse : class
where TRequest : class?
where TResponse : class?
Comment thread
viceroypenguin marked this conversation as resolved.
{
private readonly Lock _lock = new();

Expand Down Expand Up @@ -57,8 +57,6 @@ protected virtual MemoryCacheEntryOptions GetCacheEntryOptions() =>

private CacheValue GetCacheValue(TRequest request)
{
ArgumentNullException.ThrowIfNull(request);

var key = TransformKey(request);

if (!memoryCache.TryGetValue(key, out var result))
Expand All @@ -76,7 +74,10 @@ private CacheValue GetCacheValue(TRequest request)
}
}

return (CacheValue)result!;
if (result is not CacheValue cacheValue)
throw new InvalidOperationException($"An unknown type has been stored as the cache value for key `{key}`; Immediate.Cache is unable to operate.");

return cacheValue;
}

/// <summary>
Expand Down
23 changes: 23 additions & 0 deletions tests/Immediate.Cache.FunctionalTests/NullableTypesCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Immediate.Cache.Shared;
using Immediate.Handlers.Shared;

namespace Immediate.Cache.FunctionalTests;

[CacheFor<NullableTypesHandler>]
public sealed partial class NullableTypesCache
{
protected override string TransformKey(NullableTypesHandler.Query? request) =>
$"NullableTypesHandler(query: {request})";
}

[Handler]
public sealed partial class NullableTypesHandler
{
public sealed record Query;
public sealed record Response;

private async ValueTask<Response?> HandleAsync(Query? _, CancellationToken __)
{
return null;
}
Comment thread
viceroypenguin marked this conversation as resolved.
}