You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.6 KiB
58 lines
1.6 KiB
using System;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
namespace Common.Helper.Cache
|
|
{
|
|
/// <summary>
|
|
/// 缓存框架
|
|
/// </summary>
|
|
public class CacheHelper
|
|
{
|
|
private readonly IMemoryCache _memoryCache;
|
|
|
|
/// <summary>
|
|
/// 缓存框架
|
|
/// </summary>
|
|
/// <param name="memoryCache"></param>
|
|
public CacheHelper(IMemoryCache memoryCache)
|
|
{
|
|
_memoryCache = memoryCache;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 存缓存
|
|
/// </summary>
|
|
/// <param name="key">关键词</param>
|
|
/// <param name="value">值</param>
|
|
public void Set(string key, string value)
|
|
{
|
|
var cacheOptions = new MemoryCacheEntryOptions()
|
|
.SetSlidingExpiration(TimeSpan.FromMinutes(5));
|
|
_memoryCache.Set(key, value,cacheOptions);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 存缓存 带时效
|
|
/// </summary>
|
|
/// <param name="key">关键词</param>
|
|
/// <param name="value">值</param>
|
|
/// <param name="aging">时效</param>
|
|
public void SetAging(string key, string value,int aging)
|
|
{
|
|
var cacheOptions = new MemoryCacheEntryOptions()
|
|
.SetSlidingExpiration(TimeSpan.FromMinutes(aging));
|
|
_memoryCache.Set(key, value, cacheOptions);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取缓存值
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public string Get(string key)
|
|
{
|
|
return _memoryCache.TryGetValue(key, out string value) ? value:"";
|
|
}
|
|
}
|
|
}
|
|
|