private async ValueTask FlagEvictionAsync(string cacheKey) { lock (LockObj) { FlaggedEvictions.Add(cacheKey); } await Subscriber.PublishAsync(EvictionChannel, cacheKey, CommandFlags.FireAndForget); }
/// <inheritdoc/> public void Register(ICacheStack cacheStack) { if (IsRegistered) { throw new InvalidOperationException($"{nameof(RedisRemoteEvictionExtension)} can only be registered to one {nameof(ICacheStack)}"); } IsRegistered = true; Subscriber.Subscribe(EvictionChannel) .OnMessage(async(channelMessage) => { string cacheKey = channelMessage.Message; var shouldEvictLocally = false; lock (LockObj) { shouldEvictLocally = FlaggedEvictions.Remove(cacheKey) == false; } if (shouldEvictLocally) { for (var i = 0; i < EvictFromLayers.Length; i++) { await EvictFromLayers[i].EvictAsync(cacheKey); } } }); Subscriber.Subscribe(FlushChannel) .OnMessage(async(channelMessage) => { var shouldFlushLocally = false; lock (LockObj) { shouldFlushLocally = !HasFlushTriggered; HasFlushTriggered = false; } if (shouldFlushLocally) { for (var i = 0; i < EvictFromLayers.Length; i++) { await EvictFromLayers[i].FlushAsync(); } } }); }