Esempio n. 1
0
 /// <summary>
 /// Invalidates all of the cache entries which are dependent on any of the specified root keys.
 /// </summary>
 /// <param name="cacheKey">Stores information of the computed key of the input LINQ query.</param>
 public void InvalidateCacheDependencies(EFCacheKey cacheKey)
 {
     foreach (var rootCacheKey in cacheKey.CacheDependencies)
     {
         _readerWriterLockProvider.TryWriteLocked(() => _redisDbCache.RemoveByPattern(rootCacheKey));
     }
 }
        /// <summary>
        /// Invalidates all of the cache entries which are dependent on any of the specified root keys.
        /// </summary>
        /// <param name="cacheKey">Stores information of the computed key of the input LINQ query.</param>
        public void InvalidateCacheDependencies(EFCacheKey cacheKey)
        {
            _readerWriterLockProvider.TryWriteLocked(() =>
            {
                foreach (var rootCacheKey in cacheKey.CacheDependencies)
                {
                    if (string.IsNullOrWhiteSpace(rootCacheKey))
                    {
                        continue;
                    }

                    var cachedValue    = _easyCachingProvider.Get <EFCachedData>(cacheKey.KeyHash);
                    var dependencyKeys = _easyCachingProvider.Get <HashSet <string> >(rootCacheKey);
                    if (areRootCacheKeysExpired(cachedValue, dependencyKeys))
                    {
                        _logger.LogDebug(CacheableEventId.QueryResultInvalidated,
                                         $"Invalidated all of the cache entries due to early expiration of a root cache key[{rootCacheKey}].");
                        ClearAllCachedEntries();
                        return;
                    }

                    clearDependencyValues(dependencyKeys);
                    _easyCachingProvider.Remove(rootCacheKey);
                }
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Adds a new item to the cache.
        /// </summary>
        /// <param name="cacheKey">key</param>
        /// <param name="value">value</param>
        /// <param name="cachePolicy">Defines the expiration mode of the cache item.</param>
        public void InsertValue(EFCacheKey cacheKey, EFCachedData value, EFCachePolicy cachePolicy)
        {
            _readerWriterLockProvider.TryWriteLocked(() =>
            {
                if (value == null)
                {
                    value = new EFCachedData {
                        IsNull = true
                    };
                }

                var keyHash = cacheKey.KeyHash;

                foreach (var rootCacheKey in cacheKey.CacheDependencies)
                {
                    var items = _easyCachingProvider.Get <HashSet <string> >(rootCacheKey);
                    if (items.IsNull)
                    {
                        _easyCachingProvider.Set(rootCacheKey, new HashSet <string> {
                            keyHash
                        }, cachePolicy.CacheTimeout);
                    }
                    else
                    {
                        items.Value.Add(keyHash);
                        _easyCachingProvider.Set(rootCacheKey, items.Value, cachePolicy.CacheTimeout);
                    }
                }

                // We don't support Sliding Expiration at this time. -> https://github.com/dotnetcore/EasyCaching/issues/113
                _easyCachingProvider.Set(keyHash, value, cachePolicy.CacheTimeout);
            });
        }
Esempio n. 4
0
 /// <summary>
 /// Invalidates all of the cache entries which are dependent on any of the specified root keys.
 /// </summary>
 /// <param name="cacheKey">Stores information of the computed key of the input LINQ query.</param>
 public void InvalidateCacheDependencies(EFCacheKey cacheKey)
 {
     foreach (var rootCacheKey in cacheKey.CacheDependencies)
     {
         _readerWriterLockProvider.TryWriteLocked(() => _signal.RemoveChangeToken(rootCacheKey));
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Adds a new item to the cache.
        /// </summary>
        /// <param name="cacheKey">key</param>
        /// <param name="value">value</param>
        /// <param name="cachePolicy">Defines the expiration mode of the cache item.</param>
        public void InsertValue(EFCacheKey cacheKey, EFCachedData value, EFCachePolicy cachePolicy)
        {
            _readerWriterLockProvider.TryWriteLocked(() =>
            {
                if (value == null)
                {
                    value = new EFCachedData {
                        IsNull = true
                    };
                }

                var options = new MemoryCacheEntryOptions {
                    Size = 1
                };

                if (cachePolicy.CacheExpirationMode == CacheExpirationMode.Absolute)
                {
                    options.AbsoluteExpirationRelativeToNow = cachePolicy.CacheTimeout;
                }
                else
                {
                    options.SlidingExpiration = cachePolicy.CacheTimeout;
                }

                foreach (var rootCacheKey in cacheKey.CacheDependencies)
                {
                    options.ExpirationTokens.Add(_signal.GetChangeToken(rootCacheKey));
                }

                _memoryCache.Set(cacheKey.KeyHash, value, options);
            });
        }
 /// <summary>
 /// Gets a cached entry by key.
 /// </summary>
 /// <param name="cacheKey">key to find</param>
 /// <returns>cached value</returns>
 /// <param name="cachePolicy">Defines the expiration mode of the cache item.</param>
 public EFCachedData GetValue(EFCacheKey cacheKey, EFCachePolicy cachePolicy)
 {
     return(_readerWriterLockProvider.TryReadLocked(() =>
     {
         var returnValue = _easyCachingProvider.Get <EFCachedData>(cacheKey.KeyHash).Value;
         return returnValue;
     }));
 }
        /// <summary>
        /// Invalidates all of the cache entries which are dependent on any of the specified root keys.
        /// </summary>
        /// <param name="cacheKey">Stores information of the computed key of the input LINQ query.</param>
        public void InvalidateCacheDependencies(EFCacheKey cacheKey)
        {
            if (cacheKey == null)
            {
                throw new ArgumentNullException(nameof(cacheKey));
            }

            foreach (var rootCacheKey in cacheKey.CacheDependencies)
            {
                _readerWriterLockProvider.TryWriteLocked(() => _signal.RemoveChangeToken(rootCacheKey));
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Adds a new item to the cache.
 /// </summary>
 /// <param name="cacheKey">key</param>
 /// <param name="value">value</param>
 /// <param name="cachePolicy">Defines the expiration mode of the cache item.</param>
 public void InsertValue(EFCacheKey cacheKey, EFCachedData value, EFCachePolicy cachePolicy)
 {
     _readerWriterLockProvider.TryWriteLocked(() =>
     {
         if (value == null)
         {
             value = new EFCachedData {
                 IsNull = true
             };
         }
         _redisDbCache.Set(getKey(cacheKey), value, cachePolicy);
     });
 }
Esempio n. 9
0
        private static string getKey(EFCacheKey cacheKey)
        {
            if (cacheKey.CacheDependencies?.Any() != true)
            {
                throw new NullReferenceException(nameof(cacheKey.CacheDependencies));
            }

            if (string.IsNullOrWhiteSpace(cacheKey.KeyHash))
            {
                throw new NullReferenceException(nameof(cacheKey.KeyHash));
            }

            return($"{string.Join("_", cacheKey.CacheDependencies)}_{cacheKey.KeyHash}");
        }
        /// <summary>
        /// Invalidates all of the cache entries which are dependent on any of the specified root keys.
        /// </summary>
        /// <param name="cacheKey">Stores information of the computed key of the input LINQ query.</param>
        public void InvalidateCacheDependencies(EFCacheKey cacheKey)
        {
            _readerWriterLockProvider.TryWriteLocked(() =>
            {
                foreach (var rootCacheKey in cacheKey.CacheDependencies)
                {
                    if (string.IsNullOrWhiteSpace(rootCacheKey))
                    {
                        continue;
                    }

                    clearDependencyValues(rootCacheKey);
                    _easyCachingProvider.Remove(rootCacheKey);
                }
            });
        }
        /// <summary>
        /// Adds a new item to the cache.
        /// </summary>
        /// <param name="cacheKey">key</param>
        /// <param name="value">value</param>
        /// <param name="cachePolicy">Defines the expiration mode of the cache item.</param>
        public void InsertValue(EFCacheKey cacheKey, EFCachedData value, EFCachePolicy cachePolicy)
        {
            _readerWriterLockProvider.TryWriteLocked(() =>
            {
                if (value == null)
                {
                    value = new EFCachedData {
                        IsNull = true
                    };
                }

                var keyHash = cacheKey.KeyHash;

                foreach (var rootCacheKey in cacheKey.CacheDependencies)
                {
                    _dependenciesCacheManager.AddOrUpdate(
                        rootCacheKey,
                        new HashSet <string>(StringComparer.OrdinalIgnoreCase)
                    {
                        keyHash
                    },
                        updateValue: set =>
                    {
                        set.Add(keyHash);
                        return(set);
                    });
                }

                if (cachePolicy == null)
                {
                    _valuesCacheManager.Add(keyHash, value);
                }
                else
                {
                    _valuesCacheManager.Add(new CacheItem <EFCachedData>(
                                                keyHash,
                                                value,
                                                cachePolicy.CacheExpirationMode == CacheExpirationMode.Absolute ? ExpirationMode.Absolute : ExpirationMode.Sliding,
                                                cachePolicy.CacheTimeout));
                }
            });
        }
        /// <summary>
        /// Invalidates all of the cache entries which are dependent on any of the specified root keys.
        /// </summary>
        public bool InvalidateCacheDependencies(string commandText, EFCacheKey cacheKey)
        {
            if (cacheKey is null)
            {
                throw new ArgumentNullException(nameof(cacheKey));
            }

            if (!_sqlCommandsProcessor.IsCrudCommand(commandText))
            {
                _logger.LogDebug($"Skipped invalidating a none-CRUD command[{commandText}].");
                return(false);
            }

            if (shouldSkipCacheInvalidationCommands(commandText))
            {
                _logger.LogDebug($"Skipped invalidating the related cache entries of this query[{commandText}] based on the provided predicate.");
                return(false);
            }

            cacheKey.CacheDependencies.Add($"{_cacheKeyPrefix}{EFCachePolicy.UnknownsCacheDependency}");
            _cacheServiceProvider.InvalidateCacheDependencies(cacheKey);
            _logger.LogDebug(CacheableEventId.QueryResultInvalidated, $"Invalidated [{string.Join(", ", cacheKey.CacheDependencies)}] dependencies.");
            return(true);
        }
Esempio n. 13
0
 /// <summary>
 /// Gets a cached entry by key.
 /// </summary>
 /// <param name="cacheKey">key to find</param>
 /// <returns>cached value</returns>
 /// <param name="cachePolicy">Defines the expiration mode of the cache item.</param>
 public EFCachedData GetValue(EFCacheKey cacheKey, EFCachePolicy cachePolicy)
 {
     return(_readerWriterLockProvider.TryReadLocked(() => _redisDbCache.Get <EFCachedData>(getKey(cacheKey), cachePolicy)));
 }
Esempio n. 14
0
 /// <summary>
 /// Gets a cached entry by key.
 /// </summary>
 /// <param name="cacheKey">key to find</param>
 /// <returns>cached value</returns>
 /// <param name="cachePolicy">Defines the expiration mode of the cache item.</param>
 public EFCachedData GetValue(EFCacheKey cacheKey, EFCachePolicy cachePolicy)
 {
     return(_readerWriterLockProvider.TryReadLocked(() => _valuesCacheManager.Get <EFCachedData>(cacheKey.KeyHash)));
 }