using System;
|
using System.Collections.Generic;
|
|
namespace IStation.Service
|
{
|
/// <summary>
|
/// 缓存辅助类
|
/// </summary>
|
public class AnalysisParameterCacheHelper
|
{
|
private const string _contentKey = "AnalysisParameterDictKey";
|
|
// 查询缓存键
|
private static string GetCacheKey()
|
{
|
return CacheKeyHelper.GetCacheKey(_contentKey);
|
}
|
|
/// <summary>
|
/// 设置缓存
|
/// </summary>
|
public static void Set(Dictionary<string, List<Dto.AnalysisParameter>> list, int minutes = 30, int randomSeconds = 0)
|
{
|
var cacheKey = GetCacheKey();
|
MemoryCacheHelper.Set(cacheKey, list, minutes * 60 + randomSeconds);
|
}
|
|
/// <summary>
|
/// 查询缓存
|
/// </summary>
|
public static Dictionary<string, List<Dto.AnalysisParameter>> Get()
|
{
|
var cacheKey = GetCacheKey();
|
return MemoryCacheHelper.Get<Dictionary<string, List<Dto.AnalysisParameter>>>(cacheKey);
|
}
|
|
/// <summary>
|
/// 查询设置缓存
|
/// </summary>
|
public static Dictionary<string, List<Dto.AnalysisParameter>> GetSet(Func<Dictionary<string, List<Dto.AnalysisParameter>>> func, int minutes = 30, int randomSeconds = 0)
|
{
|
var cacheKey = GetCacheKey();
|
return MemoryCacheHelper.GetSet(cacheKey, func, minutes * 60 + randomSeconds);
|
}
|
|
/// <summary>
|
/// 移除缓存
|
/// </summary>
|
public static void Remove()
|
{
|
var cacheKey = GetCacheKey();
|
MemoryCacheHelper.Remove(cacheKey);
|
}
|
|
|
}
|
internal class CacheKeyHelper
|
{
|
private const string _cacheKeyPrefix = "*Default_Basic_Prefix*";//缓存键前缀
|
|
/// <summary>
|
/// 查询缓存键
|
/// </summary>
|
public static string GetCacheKey(string keyContent)
|
{
|
return string.Format("{0}_{1}", _cacheKeyPrefix, keyContent);
|
}
|
|
/// <summary>
|
/// 清除全部
|
/// </summary>
|
public static void Clear()
|
{
|
MemoryCacheHelper.Remove((key) =>
|
{
|
return key.StartsWith(_cacheKeyPrefix);
|
});
|
}
|
|
}
|
|
|
}
|