.net core cacheHelper

时间:2019-06-12
本文章向大家介绍.net core cacheHelper,主要包括.net core cacheHelper使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

using System;
using Microsoft.Extensions.Caching.Memory;

static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions());

/// <summary>
/// 获取缓存中的值
/// </summary>
/// <param name="key">键</param>
/// <returns>值</returns>
public static object GetCacheValue(string key)
{
if (!string.IsNullOrEmpty(key) && Cache.TryGetValue(key, out var val))
{
return val;
}
return default(object);
}

/// <summary>
/// 设置缓存
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
public static void SetCacheValue(string key, object value)
{
if (!string.IsNullOrEmpty(key))
{
Cache.Set(key, value, new MemoryCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromHours(1)
});
}
}

原文地址:https://www.cnblogs.com/ccgxf/p/11009908.html