using System;
|
using System.Collections.Generic;
|
using Microsoft.Extensions.Caching.Memory;
|
|
namespace IStation
|
{
|
/// <summary>
|
/// 内存缓存辅助类
|
/// </summary>
|
public class MemoryCacheHelper
|
{
|
//缓存对象
|
private static SharedMemoryCache MemoryCache
|
{
|
get
|
{
|
return SharedMemoryCache.Instance;
|
}
|
}
|
|
/// <summary>
|
/// 设置缓存
|
/// </summary>
|
/// <param name="key"></param>
|
/// <param name="obj"></param>
|
/// <param name="second">多少秒后移除缓存项</param>
|
public static void Set(string key, object obj, int? seconds = 300)
|
{
|
MemoryCache.Set(key, obj, seconds);
|
}
|
|
/// <summary>
|
/// 获取缓存
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="key"></param>
|
/// <returns></returns>
|
public static T Get<T>(string key)
|
{
|
return MemoryCache.Get<T>(key);
|
}
|
|
/// <summary>
|
/// 获取和设置
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="key"></param>
|
/// <param name="func"></param>
|
/// <returns></returns>
|
public static T GetSet<T>(string key, Func<T> func, int? seconds)
|
{
|
return MemoryCache.GetSet<T>(key, func, seconds);
|
}
|
|
/// <summary>
|
/// 移除缓存
|
/// </summary>
|
/// <param name="key"></param>
|
public static void Remove(string key)
|
{
|
MemoryCache.Remove(key);
|
}
|
|
/// <summary>
|
/// 移除符合条件的缓存
|
/// </summary>
|
/// <param name="keyFunction"></param>
|
public static void Remove(Func<string, bool> keyFunction)
|
{
|
MemoryCache.Remove(keyFunction);
|
}
|
|
/// <summary>
|
/// 移除符合条件的缓存
|
/// </summary>
|
/// <param name="keyFunction"></param>
|
public static int Remove(IEnumerable<string> keys)
|
{
|
return MemoryCache.Remove(keys);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="key"></param>
|
/// <returns></returns>
|
public static int Remove_StartWith(string key)
|
{
|
return MemoryCache.Remove_StartWith(key);
|
}
|
}
|
}
|