/// <summary> /// For revocable items , move over all revoke ids in cache index and remove them. /// </summary> private void ItemRemovedCallback(CacheEntryRemovedArguments arguments) { var cachedItem = ((AsyncCacheItem)arguments.CacheItem.Value).CurrentValueTask; if (cachedItem.Status == TaskStatus.RanToCompletion && (cachedItem.Result as IRevocable)?.RevokeKeys != null) { foreach (var revocationKey in ((IRevocable)cachedItem.Result).RevokeKeys) { HashSet <string> cacheKeys; if (RevokeKeyToCacheKeysIndex.TryGetValue(revocationKey, out cacheKeys)) { lock (cacheKeys) { cacheKeys.Remove(arguments.CacheItem.Key); if (!cacheKeys.Any()) { RevokeKeyToCacheKeysIndex.TryRemove(revocationKey, out cacheKeys); } } } } } Items.Meter(arguments.RemovedReason.ToString(), Unit.Items); }
private async Task OnRevoke(string revokeKey) { if (string.IsNullOrEmpty(revokeKey)) { Log.Warn("Error while revoking cache, revokeKey can't be null"); return; } try { HashSet <string> cacheKeys; if (RevokeKeyToCacheKeysIndex.TryGetValue(revokeKey, out cacheKeys)) { lock (cacheKeys) { var arrayOfCacheKeys = cacheKeys.ToArray();// To prevent iteration over modified collection. foreach (var cacheKey in arrayOfCacheKeys) { var removed = (AsyncCacheItem)MemoryCache.Remove(cacheKey); } } Revokes.Meter("Succeeded", Unit.Events).Mark(); } else { Revokes.Meter("Discarded", Unit.Events).Mark(); } } catch (Exception ex) { Revokes.Meter("Failed", Unit.Events).Mark(); Log.Warn("error while revoking cache", exception: ex, unencryptedTags: new { revokeKey }); } }
/// <summary> /// For revocable items , move over all revoke ids in cache index and remove them. /// </summary> private void ItemRemovedCallback(CacheEntryRemovedArguments arguments) { var cacheItem = arguments.CacheItem.Value as AsyncCacheItem; var shouldLog = ShouldLog(cacheItem?.GroupName); if (shouldLog) { Log.Info(x => x("Item removed from cache", unencryptedTags: new { cacheKey = arguments.CacheItem.Key, removeReason = arguments.RemovedReason.ToString(), cacheGroup = cacheItem?.GroupName, cacheData = cacheItem?.LogData })); } var cachedItem = ((AsyncCacheItem)arguments.CacheItem.Value).CurrentValueTask; if (cachedItem.Status == TaskStatus.RanToCompletion && (cachedItem.Result as IRevocable)?.RevokeKeys != null && !MemoryCache.Contains(arguments.CacheItem.Key)) //We want to remove items from reverseIndex only if they are not in MemoryCache { //In MemoryCache.Set flow, we get here, but item is still in cache - so we skip removal foreach (var revokeKey in ((IRevocable)cachedItem.Result).RevokeKeys) { if (RevokeKeyToCacheKeysIndex.TryGetValue(revokeKey, out HashSet <string> cacheKeys)) { lock (cacheKeys) { cacheKeys.Remove(arguments.CacheItem.Key); Log.Info(x => x("RevokeKey removed from reverse index", unencryptedTags: new { cacheKey = arguments.CacheItem.Key, revokeKey = revokeKey, removeReason = arguments.RemovedReason.ToString(), cacheGroup = cacheItem?.GroupName, cacheData = cacheItem?.LogData })); if (!cacheKeys.Any()) { if (RevokeKeyToCacheKeysIndex.TryRemove(revokeKey, out _) && shouldLog) { Log.Info(x => x("Reverse index for cache item was removed", unencryptedTags: new { cacheKey = arguments.CacheItem.Key, removeReason = arguments.RemovedReason.ToString(), cacheGroup = cacheItem?.GroupName, cacheData = cacheItem?.LogData })); } } } } } } Items.Meter(arguments.RemovedReason.ToString(), Unit.Items).Mark(); }
private Task OnRevoke(string revokeKey) { var shouldLog = GetRevokeConfig().LogRequests; if (string.IsNullOrEmpty(revokeKey)) { Log.Warn("Error while revoking cache, revokeKey can't be null"); return(Task.FromResult(false)); } try { if (shouldLog) { Log.Info(x => x("Revoke request received", unencryptedTags: new { revokeKey })); } if (RevokeKeyToCacheKeysIndex.TryGetValue(revokeKey, out HashSet <string> cacheKeys)) { lock (cacheKeys) { var arrayOfCacheKeys = cacheKeys.ToArray();// To prevent iteration over modified collection. if (shouldLog && arrayOfCacheKeys.Length == 0) { Log.Info(x => x("There is no CacheKey to Revoke", unencryptedTags: new { revokeKey })); } foreach (var cacheKey in arrayOfCacheKeys) { if (shouldLog) { Log.Info(x => x("Revoking cacheKey", unencryptedTags: new { revokeKey, cacheKey })); } var unused = (AsyncCacheItem)MemoryCache.Remove(cacheKey); } } Revokes.Meter("Succeeded", Unit.Events).Mark(); } else { if (shouldLog) { Log.Info(x => x("Key is not cached. No revoke is needed", unencryptedTags: new { revokeKey })); } Revokes.Meter("Discarded", Unit.Events).Mark(); } } catch (Exception ex) { Revokes.Meter("Failed", Unit.Events).Mark(); Log.Warn("error while revoking cache", exception: ex, unencryptedTags: new { revokeKey }); } return(Task.FromResult(true)); }
/// <summary> /// For revocable items , move over all revoke ids in cache index and remove them. /// </summary> private void ItemRemovedCallback(CacheEntryRemovedArguments arguments) { var cacheItem = arguments.CacheItem.Value as AsyncCacheItem; var shouldLog = ShouldLog(cacheItem?.GroupName); if (shouldLog) { Log.Info(x => x("Item removed from cache", unencryptedTags: new { cacheKey = arguments.CacheItem.Key, removeReason = arguments.RemovedReason.ToString(), cacheGroup = cacheItem?.GroupName, cacheData = cacheItem?.LogData })); } var cachedItem = ((AsyncCacheItem)arguments.CacheItem.Value).CurrentValueTask; if (cachedItem.Status == TaskStatus.RanToCompletion && (cachedItem.Result as IRevocable)?.RevokeKeys != null) { foreach (var revokeKey in ((IRevocable)cachedItem.Result).RevokeKeys) { if (RevokeKeyToCacheKeysIndex.TryGetValue(revokeKey, out HashSet <string> cacheKeys)) { lock (cacheKeys) { cacheKeys.Remove(arguments.CacheItem.Key); Log.Info(x => x("RevokeKey removed from reverse index", unencryptedTags: new { cacheKey = arguments.CacheItem.Key, revokeKey = revokeKey, removeReason = arguments.RemovedReason.ToString(), cacheGroup = cacheItem?.GroupName, cacheData = cacheItem?.LogData })); if (!cacheKeys.Any()) { if (RevokeKeyToCacheKeysIndex.TryRemove(revokeKey, out _) && shouldLog) { Log.Info(x => x("Reverse index for cache item was removed", unencryptedTags: new { cacheKey = arguments.CacheItem.Key, removeReason = arguments.RemovedReason.ToString(), cacheGroup = cacheItem?.GroupName, cacheData = cacheItem?.LogData })); } } } } } } }