Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
jodydonetti committed Dec 3, 2023
1 parent 71163e0 commit 0908709
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/ZiggyCreatures.FusionCache/FusionCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
Expand Down Expand Up @@ -42,6 +43,10 @@ public partial class FusionCache
private readonly List<IFusionCachePlugin> _plugins;
private AutoRecoveryService _autoRecovery;

private FusionCacheEntryOptions _tryUpdateOptions;
private static readonly MethodInfo __methodInfoTryUpdateMemoryEntryFromDistributedEntryAsyncOpenGeneric = typeof(FusionCache).GetMethod(nameof(TryUpdateMemoryEntryFromDistributedEntryAsync), BindingFlags.NonPublic | BindingFlags.Instance);
private static readonly ConcurrentDictionary<Type, MethodInfo> __methodInfoTryUpdateMemoryEntryFromDistributedEntryAsyncCache = new ConcurrentDictionary<Type, MethodInfo>();

/// <summary>
/// Creates a new <see cref="FusionCache"/> instance.
/// </summary>
Expand Down Expand Up @@ -671,8 +676,6 @@ internal bool MustAwaitBackplaneOperations(FusionCacheEntryOptions options)
return false;
}

private static readonly MethodInfo __methodInfoTryUpdateMemoryEntryFromDistributedEntryAsyncOpenGeneric = typeof(FusionCache).GetMethod(nameof(TryUpdateMemoryEntryFromDistributedEntryAsync), BindingFlags.NonPublic | BindingFlags.Instance);

internal async ValueTask<(bool error, bool isSame, bool hasUpdated)> TryUpdateMemoryEntryFromDistributedEntryUntypedAsync(string operationId, string cacheKey, FusionCacheMemoryEntry memoryEntry)
{
if (_logger?.IsEnabled(LogLevel.Trace) ?? false)
Expand Down Expand Up @@ -706,7 +709,8 @@ internal bool MustAwaitBackplaneOperations(FusionCacheEntryOptions options)
return (true, false, false);
}

var methodInfo = __methodInfoTryUpdateMemoryEntryFromDistributedEntryAsyncOpenGeneric.MakeGenericMethod(memoryEntry.ValueType);
var methodInfo = __methodInfoTryUpdateMemoryEntryFromDistributedEntryAsyncCache.GetOrAdd(memoryEntry.ValueType, x => __methodInfoTryUpdateMemoryEntryFromDistributedEntryAsyncOpenGeneric.MakeGenericMethod(x));

// SIGNATURE PARAMS: string operationId, string cacheKey, DistributedCacheAccessor dca, FusionCacheMemoryEntry memoryEntry
return await ((ValueTask<(bool error, bool isSame, bool hasUpdated)>)methodInfo.Invoke(this, new object[] { operationId, cacheKey, dca, memoryEntry })).ConfigureAwait(false);
}
Expand All @@ -719,7 +723,6 @@ internal bool MustAwaitBackplaneOperations(FusionCacheEntryOptions options)
}
}

private FusionCacheEntryOptions _tryUpdateOptions;
private async ValueTask<(bool error, bool isSame, bool hasUpdated)> TryUpdateMemoryEntryFromDistributedEntryAsync<TValue>(string operationId, string cacheKey, DistributedCacheAccessor dca, FusionCacheMemoryEntry memoryEntry)
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -13,6 +14,17 @@ namespace ZiggyCreatures.Caching.Fusion.Internals.Distributed;

internal sealed partial class DistributedCacheAccessor
{
private readonly IDistributedCache _cache;
private readonly IFusionCacheSerializer _serializer;
private readonly FusionCacheOptions _options;
private readonly ILogger? _logger;
private readonly FusionCacheDistributedEventsHub _events;
private readonly SimpleCircuitBreaker _breaker;
private readonly string _wireFormatToken;

private static readonly MethodInfo __methodInfoSetEntryAsyncOpenGeneric = typeof(DistributedCacheAccessor).GetMethod(nameof(SetEntryAsync), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
private static readonly ConcurrentDictionary<Type, MethodInfo> __methodInfoSetEntryAsyncCache = new ConcurrentDictionary<Type, MethodInfo>();

public DistributedCacheAccessor(IDistributedCache distributedCache, IFusionCacheSerializer serializer, FusionCacheOptions options, ILogger? logger, FusionCacheDistributedEventsHub events)
{
if (distributedCache is null)
Expand Down Expand Up @@ -48,14 +60,6 @@ public DistributedCacheAccessor(IDistributedCache distributedCache, IFusionCache
};
}

private readonly IDistributedCache _cache;
private readonly IFusionCacheSerializer _serializer;
private readonly FusionCacheOptions _options;
private readonly ILogger? _logger;
private readonly FusionCacheDistributedEventsHub _events;
private readonly SimpleCircuitBreaker _breaker;
private readonly string _wireFormatToken;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private string MaybeProcessCacheKey(string key)
{
Expand Down Expand Up @@ -121,16 +125,14 @@ private void ProcessError(string operationId, string key, Exception exc, string
_logger.Log(_options.DistributedCacheErrorsLogLevel, exc, "FUSION [N={CacheName} I={CacheInstanceId}] (O={CacheOperationId} K={CacheKey}): [DC] an error occurred while " + actionDescription, _options.CacheName, _options.InstanceId, operationId, key);
}

private static readonly MethodInfo __methodInfoSetEntryAsyncOpenGeneric = typeof(DistributedCacheAccessor).GetMethod(nameof(SetEntryAsync), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

public async ValueTask<bool> SetEntryUntypedAsync(string operationId, string key, FusionCacheMemoryEntry memoryEntry, FusionCacheEntryOptions options, bool isBackground, CancellationToken token)
{
try
{
if (memoryEntry is null)
return false;

var methodInfo = __methodInfoSetEntryAsyncOpenGeneric.MakeGenericMethod(memoryEntry.ValueType);
var methodInfo = __methodInfoSetEntryAsyncCache.GetOrAdd(memoryEntry.ValueType, x => __methodInfoSetEntryAsyncOpenGeneric.MakeGenericMethod(x));

// SIGNATURE PARAMS: string operationId, string key, IFusionCacheEntry entry, FusionCacheEntryOptions options, bool isBackground, CancellationToken token
return await ((ValueTask<bool>)methodInfo.Invoke(this, new object[] { operationId, key, memoryEntry, options, isBackground, token })).ConfigureAwait(false);
Expand Down

0 comments on commit 0908709

Please sign in to comment.