Moli0
2021-12-15 24d68e7998e80cbd4ef3ed04acbf2539a9c5a46b
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;
 
namespace Helpers
{
    /// <summary>
    /// 公共缓存类
    /// </summary>
    public static class CacheHelper
    {
        private static ObjectCache cache = System.Runtime.Caching.MemoryCache.Default;
 
        /// <summary>
        /// 查询缓存是否存在
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>是否存在</returns>
        public static bool ExistsCache(string key)
        {
            return cache.Contains(key);
        }
 
        /// <summary>
        /// 根据缓存键移除缓存对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">缓存键</param>
        public static void RemoveCache<T>(string key) where T : class
        {
            if (!cache.Contains(key)) return;
            var model = GetCache<T>(key);
            foreach (var a in model.GetType().GetProperties()){
                a.SetValue(model,"");
            }
            SetCache<T>(key, model, 1);
            cache.Remove(key);
        }
 
        /// <summary>
        /// 设置缓存对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="expires">过期时间(单位:分钟,默认值30)</param>
        public static void SetCache<T>(string key, T obj, double expires = 30) where T : class
        {
            DateTimeOffset cc = DateTimeOffset.Now.AddMinutes(expires);
 
            cache.Set(key, obj, cc);
        }
 
        /// <summary>
        /// 根据缓存键获取缓存对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <returns>缓存对象</returns>
        public static T GetCache<T>(string key) where T : class
        {
            if (!cache.Contains(key)) return null;
            var item = cache.GetCacheItem(key);
            if (item != null)
            {
                return item.Value as T;
            }
            return null;
        }
 
        /// <summary>
        /// 获取缓存对象,如果不存在,则重新设置
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="func">缓存委托</param>
        /// <param name="expires">过期时间(单位:分钟,默认值30)</param>
        /// <returns>缓存对象</returns>
        public static T GetCache<T>(string key, Func<T> func, double expires = 30) where T : class
        {
            if (!cache.Contains(key))
            {
                CacheItemPolicy policy = new CacheItemPolicy()
                {
                    AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(expires)
                };
 
                T obj = null;
 
                if (func != null)
                {
                    obj = func();
 
                    if (obj != null)
                    {
                        cache.Set(key, obj, policy);
                    }
                }
 
                return obj;
            }
            else { return cache.GetCacheItem(key).Value as T; };
        }
    }
}