Skip to content

Instantly share code, notes, and snippets.

@imzjy
Created April 2, 2019 10:02
Show Gist options
  • Save imzjy/369aefdac45040030462ddae196aaf5d to your computer and use it in GitHub Desktop.
Save imzjy/369aefdac45040030462ddae196aaf5d to your computer and use it in GitHub Desktop.
Few lines code for boost the performance by in-memory cache
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