lixiaojun
2023-04-12 2be5d90e96f163c67101571f6865b17effcb0f3f
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
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);
        }
    }
}