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
        /// <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);
        }
Exemple #3
0
        /// <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();
        }
Exemple #4
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));
        }
Exemple #5
0
        /// <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
                                    }));
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #6
0
        private Task <object> GetOrAdd(string key, Func <Task <object> > factory, CacheItemPolicyEx policy, string[] metricsKeys, Type taskResultType)
        {
            Func <bool, Task <object> > wrappedFactory = async removeOnException =>
            {
                try
                {
                    var result = await factory().ConfigureAwait(false);

                    //Can happen if item removed before task is completed
                    if (MemoryCache.Contains(key))
                    {
                        var revocableResult = result as IRevocable;
                        if (revocableResult?.RevokeKeys != null)
                        {
                            foreach (var revokeKey in revocableResult.RevokeKeys)
                            {
                                var cacheKeys = RevokeKeyToCacheKeysIndex.GetOrAdd(revokeKey, k => new HashSet <string>());

                                lock (cacheKeys)
                                {
                                    cacheKeys.Add(key);
                                }
                            }
                        }
                    }
                    AwaitingResult.Decrement(metricsKeys);
                    return(result);
                }
                catch
                {
                    if (removeOnException)
                    {
                        MemoryCache.Remove(key); // Do not cache exceptions.
                    }
                    AwaitingResult.Decrement(metricsKeys);
                    Failed.Mark(metricsKeys);
                    throw;
                }
            };

            var newItem = new AsyncCacheItem();

            Task <object> resultTask;

            // Taking a lock on the newItem in case it actually becomes the item in the cache (if no item with that key
            // existed). For another thread, it will be returned into the existingItem variable and will block on the
            // second lock, preventing concurrent mutation of the same object.
            lock (newItem.Lock)
            {
                if (typeof(IRevocable).IsAssignableFrom(taskResultType))
                {
                    policy.RemovedCallback += ItemRemovedCallback;
                }

                // Surprisingly, when using MemoryCache.AddOrGetExisting() where the item doesn't exist in the cache,
                // null is returned.
                var existingItem = (AsyncCacheItem)MemoryCache.AddOrGetExisting(key, newItem, policy);

                if (existingItem == null)
                {
                    Misses.Mark(metricsKeys);
                    AwaitingResult.Increment(metricsKeys);
                    newItem.CurrentValueTask = wrappedFactory(true);
                    newItem.NextRefreshTime  = DateTime.UtcNow + policy.RefreshTime;
                    resultTask = newItem.CurrentValueTask;
                }
                else
                {
                    // This lock makes sure we're not mutating the same object as was added to the cache by an earlier
                    // thread (which was the first to add from 'newItem', for subsequent threads it will be 'existingItem').
                    lock (existingItem.Lock)
                    {
                        resultTask = existingItem.CurrentValueTask;

                        // Start refresh if an existing refresh ins't in progress and we've passed the next refresh time.
                        if (existingItem.RefreshTask == null && DateTime.UtcNow >= existingItem.NextRefreshTime)
                        {
                            existingItem.RefreshTask = ((Func <Task>)(async() =>
                            {
                                try
                                {
                                    var getNewValue = wrappedFactory(false);
                                    await getNewValue.ConfigureAwait(false);
                                    existingItem.CurrentValueTask = getNewValue;
                                    existingItem.NextRefreshTime = DateTime.UtcNow + policy.RefreshTime;
                                    existingItem.RefreshTask = null;
                                    MemoryCache.Set(new CacheItem(key, existingItem), policy);
                                }
                                catch
                                {
                                    existingItem.NextRefreshTime = DateTime.UtcNow + policy.FailedRefreshDelay;
                                    existingItem.RefreshTask = null;
                                }
                            })).Invoke();
                        }
                    }

                    if (resultTask.GetAwaiter().IsCompleted)
                    {
                        Hits.Mark(metricsKeys);
                    }
                    else
                    {
                        JoinedTeam.Mark(metricsKeys);
                    }
                }
            }

            return(resultTask);
        }
Exemple #7
0
        private Task <object> GetOrAdd(string key, Func <Task <object> > factory, CacheItemPolicyEx policy, string groupName, string logData, string[] metricsKeys, Type taskResultType)
        {
            var shouldLog = ShouldLog(groupName);

            async Task <object> WrappedFactory(bool removeOnException)
            {
                try
                {
                    if (shouldLog)
                    {
                        Log.Info(x => x("Cache item is waiting for value to be resolved", unencryptedTags: new
                        {
                            cacheKey   = key,
                            cacheGroup = groupName,
                            cacheData  = logData
                        }));
                    }

                    var result = await factory().ConfigureAwait(false);

                    if (shouldLog)
                    {
                        Log.Info(x => x("Cache item value is resolved", unencryptedTags: new
                        {
                            cacheKey   = key,
                            cacheGroup = groupName,
                            cacheData  = logData,
                            value      = GetValueForLogging(result)
                        }));
                    }
                    //Can happen if item removed before task is completed
                    if (MemoryCache.Contains(key))
                    {
                        var revocableResult = result as IRevocable;
                        if (revocableResult?.RevokeKeys != null)
                        {
                            foreach (var revokeKey in revocableResult.RevokeKeys)
                            {
                                var cacheKeys = RevokeKeyToCacheKeysIndex.GetOrAdd(revokeKey, k => new HashSet <string>());

                                lock (cacheKeys)
                                {
                                    cacheKeys.Add(key);
                                }
                                Log.Info(x => x("RevokeKey added to reverse index", unencryptedTags: new
                                {
                                    revokeKey  = revokeKey,
                                    cacheKey   = key,
                                    cacheGroup = groupName,
                                    cacheData  = logData
                                }));
                            }
                        }
                    }

                    return(result);
                }
                catch (Exception exception)
                {
                    Log.Info(x => x("Error resolving value for cache item", unencryptedTags: new
                    {
                        cacheKey   = key,
                        cacheGroup = groupName,
                        cacheData  = logData,
                        removeOnException,
                        errorMessage = exception.Message
                    }));

                    if (removeOnException)
                    {
                        MemoryCache.Remove(key); // Do not cache exceptions.
                    }
                    throw;
                }
            }

            var newItem = shouldLog ?
                          new AsyncCacheItem {
                GroupName = string.Intern(groupName), LogData = logData
            } :
            new AsyncCacheItem();      // if log is not needed, then do not cache unnecessary details which will blow up the memory

            Task <object> resultTask;

            // Taking a lock on the newItem in case it actually becomes the item in the cache (if no item with that key
            // existed). For another thread, it will be returned into the existingItem variable and will block on the
            // second lock, preventing concurrent mutation of the same object.
            lock (newItem.Lock)
            {
                if (typeof(IRevocable).IsAssignableFrom(taskResultType))
                {
                    policy.RemovedCallback += ItemRemovedCallback;
                }

                // Surprisingly, when using MemoryCache.AddOrGetExisting() where the item doesn't exist in the cache,
                // null is returned.
                var existingItem = (AsyncCacheItem)MemoryCache.AddOrGetExisting(key, newItem, policy);

                if (existingItem == null)
                {
                    newItem.CurrentValueTask = WrappedFactory(true);
                    newItem.NextRefreshTime  = DateTime.UtcNow + policy.RefreshTime;
                    resultTask = newItem.CurrentValueTask;
                    if (shouldLog)
                    {
                        Log.Info(x => x("Item added to cache", unencryptedTags: new
                        {
                            cacheKey   = key,
                            cacheGroup = groupName,
                            cacheData  = logData
                        }));
                    }
                }
                else
                {
                    // This lock makes sure we're not mutating the same object as was added to the cache by an earlier
                    // thread (which was the first to add from 'newItem', for subsequent threads it will be 'existingItem').
                    lock (existingItem.Lock)
                    {
                        resultTask = existingItem.CurrentValueTask;

                        // Start refresh if an existing refresh ins't in progress and we've passed the next refresh time.
                        if (existingItem.RefreshTask?.IsCompleted != false && DateTime.UtcNow >= existingItem.NextRefreshTime)
                        {
                            existingItem.RefreshTask = ((Func <Task>)(async() =>
                            {
                                try
                                {
                                    var getNewValue = WrappedFactory(false);
                                    await getNewValue.ConfigureAwait(false);
                                    existingItem.CurrentValueTask = getNewValue;
                                    existingItem.NextRefreshTime = DateTime.UtcNow + policy.RefreshTime;
                                    MemoryCache.Set(new CacheItem(key, existingItem), policy);
                                }
                                catch
                                {
                                    existingItem.NextRefreshTime = DateTime.UtcNow + policy.FailedRefreshDelay;
                                }
                            })).Invoke();
                        }
                    }
                }
            }

            return(resultTask);
        }