/// <summary> /// Removes the specified key. /// </summary> /// <param name="key">The key.</param> public void Remove(string key) { key = MemoryCacheExtensions.TenantCacheKey(key); if (!key.IsNullOrEmpty()) { Context.OfMemory().MemoryCache.Remove(key); RemoveCacheKey(key); } }
/// <summary> /// Sets the specified key. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> public void Set(string key, object value) { key = MemoryCacheExtensions.TenantCacheKey(key); if (!key.IsNullOrEmpty()) { Context.OfMemory().MemoryCache.Set(key, value); AddCacheKey(key); } }
/// <summary> /// Sets the specified key. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="expire">The expire.</param> public void Set <T>(string key, T value, TimeSpan expire) { key = MemoryCacheExtensions.TenantCacheKey(key); if (!key.IsNullOrEmpty()) { Context.OfMemory().MemoryCache.Set(key, value, expire); AddCacheKey(key); } }
/// <summary> /// Removes the cache key. /// </summary> /// <param name="key">The key.</param> private void RemoveCacheKey(string key) { key = MemoryCacheExtensions.TenantCacheKey(key); TryGet(AllMemoryObjectCacheKey, out List <string> cacheList); if (cacheList == null) { cacheList = new List <string>(); } if (cacheList.Contains(key)) { cacheList.Remove(key); Set(AllMemoryObjectCacheKey, cacheList); } }
/// <summary> /// Get the specified cacheKey, dataRetriever and expiration. /// 获取并设置缓存 /// </summary> /// <param name="cacheKey">Cache key.</param> /// <param name="dataRetriever">Data retriever.</param> /// <param name="expiration">Expiration.</param> public CacheValue <T> GetOrSet <T>(Func <T> dataRetriever, string cacheKey, TimeSpan expiration) where T : class { cacheKey = MemoryCacheExtensions.TenantCacheKey(cacheKey); TryGet(cacheKey, out T result); if (result != null) { return(new CacheValue <T>(result, true)); } var item = dataRetriever?.Invoke(); if (item != null) { Set(cacheKey, item, expiration); return(new CacheValue <T>(item, true)); } return(CacheValue <T> .NoValue); }
/// <summary> /// Gets the specified key. /// </summary> /// <param name="key">The key.</param> public T Get <T>(string key) { key = MemoryCacheExtensions.TenantCacheKey(key); return(Context.OfMemory().MemoryCache.Get <T>(key)); }
/// <summary> /// Tries the get. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> public bool TryGet <T>(string key, out T value) { key = MemoryCacheExtensions.TenantCacheKey(key); return(Context.OfMemory().MemoryCache.TryGetValue(key, out value)); }