username@email.com
2025-04-27 15eb82df2d6ec539e9d4245bfe08d531e8eb6379
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CommonToolsCore
{
    public class CacheHelperNetCore
    {
        public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
 
        /// <summary>
        /// 缓存绝对过期时间
        /// </summary>
        ///<param name="key">Cache键值</param>
        ///<param name="value">给Cache[key]赋的值</param>
        ///<param name="minute">minute分钟后绝对过期</param>
        public static void CacheInsertAddMinutes(string key, object value, int minute)
        {
            if (value == null) return;
            _memoryCache.Set(key, value, new MemoryCacheEntryOptions()
                    .SetAbsoluteExpiration(TimeSpan.FromMinutes(minute)));
        }
 
 
        /// <summary>
        /// 缓存相对过期,最后一次访问后minute分钟后过期
        /// </summary>
        ///<param name="key">Cache键值</param>
        ///<param name="value">给Cache[key]赋的值</param>
        ///<param name="minute">滑动过期分钟</param>
        public static void CacheInsertFromMinutes(string key, object value, int minute)
        {
            if (value == null) return;
            _memoryCache.Set(key, value, new MemoryCacheEntryOptions()
                    .SetSlidingExpiration(TimeSpan.FromMinutes(minute)));
        }
 
 
        /// <summary>
        ///以key键值,把value赋给Cache[key].如果不主动清空,会一直保存在内存中.
        /// </summary>
        ///<param name="key">Cache键值</param>
        ///<param name="value">给Cache[key]赋的值</param>
        public static void CacheInsert(string key, object value)
        {
            _memoryCache.Set(key, value);
        }
 
        /// <summary>
        ///清除Cache[key]的值
        /// </summary>
        ///<param name="key"></param>
        public static void CacheNull(string key)
        {
            _memoryCache.Remove(key);
        }
 
        /// <summary>
        ///根据key值,返回Cache[key]的值
        /// </summary>
        ///<param name="key"></param>
        public static object CacheValue(string key)
        {
            return _memoryCache.Get(key);
        }
 
        #region 缓存文档
        //public IActionResult Index()
        //{
        //    string cacheKey = "key";
        //    string result;
        //    if (!_memoryCache.TryGetValue(cacheKey, out result))
        //    {
        //        result = $"LineZero{DateTime.Now}";
        //        _memoryCache.Set(cacheKey, result);
        //        //设置相对过期时间2分钟
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .SetSlidingExpiration(TimeSpan.FromMinutes(2)));
        //        //设置绝对过期时间2分钟
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .SetAbsoluteExpiration(TimeSpan.FromMinutes(2)));
        //        //移除缓存
        //        _memoryCache.Remove(cacheKey);
        //        //缓存优先级 (程序压力大时,会根据优先级自动回收)
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .SetPriority(CacheItemPriority.NeverRemove));
        //        //缓存回调 10秒过期会回调
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .SetAbsoluteExpiration(TimeSpan.FromSeconds(10))
        //            .RegisterPostEvictionCallback((key, value, reason, substate) =>
        //            {
        //                Console.WriteLine($"键{key}值{value}改变,因为{reason}");
        //            }));
        //        //缓存回调 根据Token过期
        //        var cts = new CancellationTokenSource();
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .AddExpirationToken(new CancellationChangeToken(cts.Token))
        //            .RegisterPostEvictionCallback((key, value, reason, substate) =>
        //            {
        //                Console.WriteLine($"键{key}值{value}改变,因为{reason}");
        //            }));
        //        cts.Cancel();
        //    }
        //    ViewBag.Cache = result;
        //    return View();
        //}
        #endregion
    }
 
 
    ////写入
    //CacheHelperNetCore.CacheInsertAddMinutes("cache","123",10);
    //        //读取
    //       string value = CacheHelperNetCore.CacheValue("cache").ToString();
    ////清除
    //CacheHelperNetCore.CacheNull("cache");
}