using Nop.Core.Configuration; using Nop.Core.Infrastructure; namespace Nop.Core.Caching; /// /// Represents a per request cache manager /// public partial class PerRequestCacheManager : CacheKeyService, IShortTermCacheManager { #region Fields protected readonly ConcurrentTrie _concurrentCollection; #endregion #region Ctor public PerRequestCacheManager(AppSettings appSettings) : base(appSettings) { _concurrentCollection = new ConcurrentTrie(); } #endregion #region Methods /// /// Get a cached item. If it's not in the cache yet, then load and cache it /// /// Type of cached item /// /// Function to load item if it's not in the cache yet /// Initial cache key /// Parameters to create cache key /// /// A task that represents the asynchronous operation /// The task result contains the cached value associated with the specified key /// public async Task GetAsync(Func> acquire, CacheKey cacheKey, params object[] cacheKeyParameters) { var key = cacheKey.Create(CreateCacheKeyParameters, cacheKeyParameters).Key; if (_concurrentCollection.TryGetValue(key, out var data)) return (T)data; var result = await acquire(); if (result != null) _concurrentCollection.Add(key, result); return result; } /// /// Remove items by cache key prefix /// /// Cache key prefix /// Parameters to create cache key prefix public virtual void RemoveByPrefix(string prefix, params object[] prefixParameters) { var keyPrefix = PrepareKeyPrefix(prefix, prefixParameters); _concurrentCollection.Prune(keyPrefix, out _); } /// /// Remove the value with the specified key from the cache /// /// Cache key /// Parameters to create cache key public virtual void Remove(string cacheKey, params object[] cacheKeyParameters) { _concurrentCollection.Remove(PrepareKey(new CacheKey(cacheKey), cacheKeyParameters).Key); } #endregion }