Exemple #1
0
        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 });
            }
        }
Exemple #2
0
        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));
        }
        private Task OnRevoke(string revokeKey)
        {
            if (revokeKey == null)
            {
                throw new ArgumentNullException();
            }

            RecentRevokesCache.RegisterRevokeKey(revokeKey, DateTime.UtcNow);

            var revokeApplied = false;

            if (RevokeKeyToCacheItemsIndex.TryGetValue(revokeKey, out var _))
            {
                lock (RevokeKeyToCacheItemsIndex)
                    if (RevokeKeyToCacheItemsIndex.TryGetValue(revokeKey, out var hash))
                    {
                        foreach (var item in hash)
                        {
                            item.IsRevoked = true;
                        }

                        revokeApplied = true;
                    }
            }

            if (revokeApplied)
            {
                Revokes.Meter("Succeeded", Unit.Events).Mark();
                Log.Debug(x => x("Revoke applied", unencryptedTags: new { revokeKey }));
            }
            else
            {
                Revokes.Meter("Discarded", Unit.Events).Mark();
            }

            return(Task.CompletedTask);
        }