using System;
using Microsoft.Extensions.Caching.Memory;
static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions());
///
/// 获取缓存中的值
///
/// 键
/// 值
public static object GetCacheValue(string key)
{
if (!string.IsNullOrEmpty(key) && Cache.TryGetValue(key, out var val))
{
return val;
}
return default(object);
}
///
/// 设置缓存
///
/// 键
/// 值
public static void SetCacheValue(string key, object value)
{
if (!string.IsNullOrEmpty(key))
{
Cache.Set(key, value, new MemoryCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromHours(1)
});
}
}
本文介绍了一个简单的内存缓存实现方式,使用.NET Core的MemoryCache组件进行数据的存储和读取。提供了设置缓存项及其过期时间的方法,并演示了如何从缓存中获取数据。
1449

被折叠的 条评论
为什么被折叠?



