0%

.Net Core 缓存

参考 .net core 中的缓存内存

1
2
3
4
5
6
7
8
9
10
using Microsoft.Extensions.Caching.Memory;

public class HomeController : Controller
{
private IMemoryCache _cache;

public HomeController(IMemoryCache memoryCache)
{
_cache = memoryCache;
}

CacheKeys API
1
2
3
4
5
6
7
8
9
10
11
12
13
public static class CacheKeys
{
public static string Entry { get { return "_Entry"; } }
public static string CallbackEntry { get { return "_Callback"; } }
public static string CallbackMessage { get { return "_CallbackMessage"; } }
public static string Parent { get { return "_Parent"; } }
public static string Child { get { return "_Child"; } }
public static string DependentMessage { get { return "_DependentMessage"; } }
public static string DependentCTS { get { return "_DependentCTS"; } }
public static string Ticks { get { return "_Ticks"; } }
public static string CancelMsg { get { return "_CancelMsg"; } }
public static string CancelTokenSource { get { return "_CancelTokenSource"; } }
}

缓存一个时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DateTime cacheTiming;

// Look for cache key.
if (!_cache.TryGetValue(CacheKeys.Entry, out cacheEntry))
{
cacheEntry = DateTime.Now;

// Set cache options.
var cacheEntryOptions = new MemoryCacheEntryOptions()
// Keep in cache for this time, reset time if accessed.
.SetSlidingExpiration(TimeSpan.FromSeconds(3));

// Save data in cache.
_cache.Set(CacheKeys.Entry, cacheTiming, cacheEntryOptions);
}

取出缓存
1
var timing = _cache.Get<DateTime?>(CacheKeys.Entry);

可调过期(slide expiration)时间和绝对过期(absolute expiration)时间