/// <summary> /// Sets the specified key. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> public void SetPublic <T>(string key, T value) { key = MemoryCacheExtensions.PublicCacheKey(key); if (!key.IsNullOrEmpty()) { Context.OfMemory().MemoryCache.Set(key, value); } }
/// <summary> /// Get or set of global cache /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dataRetriever"></param> /// <param name="cacheKey"></param> /// <returns></returns> public CacheValue <T> GetOrSetPublic <T>(Func <T> dataRetriever, string cacheKey) where T : class { cacheKey = MemoryCacheExtensions.PublicCacheKey(cacheKey); TryGetPublic(cacheKey, out T result); if (result != null) { return(new CacheValue <T>(result, true)); } var item = dataRetriever?.Invoke(); if (item != null) { SetPublic(cacheKey, item); return(new CacheValue <T>(item, true)); } return(CacheValue <T> .NoValue); }
/// <summary> /// Try get of global cache /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool TryGetPublic <T>(string key, out T value) { key = MemoryCacheExtensions.PublicCacheKey(key); return(Context.OfMemory().MemoryCache.TryGetValue(key, out value)); }