Created
April 2, 2019 10:02
-
-
Save imzjy/369aefdac45040030462ddae196aaf5d to your computer and use it in GitHub Desktop.
Few lines code for boost the performance by in-memory cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Runtime.Caching; | |
namespace Dev.Lib | |
{ | |
public class CacheHelper | |
{ | |
public static T GetOrSet<T>(string cacheKey, DateTime expiredAt, Func<T> getItemCallback) where T : class | |
{ | |
T item = MemoryCache.Default.Get(cacheKey) as T; | |
if (item == null) | |
{ | |
item = getItemCallback(); | |
MemoryCache.Default.Add(cacheKey, item, expiredAt); | |
} | |
return item; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment