duheng
2025-03-28 9b673ca69c30616b5e5807d665ed8efa2720e1c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
namespace PBS.WinFrmUI.Hydro
{
    /// <summary>
    /// 缓存辅助类
    /// </summary>
    public class AnalysisParameterCacheHelper
    {
        private const string _contentKey = "AnalysisParameterDictKey";
 
        // 查询缓存键
        private static string GetCacheKey(long Id)
        {
            return CacheKeyHelper.GetCacheKey(_contentKey+ Id);
        }
 
        /// <summary>
        /// 设置缓存
        /// </summary>
        public static void Set(long Id,Dictionary<long, List<AnalysisParameter>> list, int minutes = 30, int randomSeconds = 0)
        {
            var cacheKey = GetCacheKey(Id);
            MemoryCacheHelper.Set(cacheKey, list, minutes * 60 + randomSeconds);
        }
 
        /// <summary>
        /// 查询缓存
        /// </summary>
        public static Dictionary<long, List<AnalysisParameter>> Get(long Id)
        {
            var cacheKey = GetCacheKey(Id);
            return MemoryCacheHelper.Get<Dictionary<long, List<AnalysisParameter>>>(cacheKey);
        }
 
        /// <summary>
        /// 查询设置缓存
        /// </summary>
        public static async Task<Dictionary<long, List<AnalysisParameter>>> GetSet(long Id, Func<Task<Dictionary<long, List<AnalysisParameter>>>> func, int minutes = 30, int randomSeconds = 0)
        {
            var cacheKey = GetCacheKey(Id);
            return await MemoryCacheHelper.GetSet(cacheKey, func, minutes * 60 + randomSeconds);
 
            
        }
 
 
    }
 
 
    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);
            });
        }
 
    }
}