zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#if !NET45
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Net.Http;
#endif
using System;
using System.Collections.Generic;
 
namespace Prow.Cache
{
    /// <summary>
    /// CacheManager静态操作提供类
    /// </summary>
    public static class CacheManager
    {
#if !NET45
#nullable disable
        private static Lazy<ICache> _lazy;
#nullable restore
#else
        private static readonly Lazy<ICache> _lazy = new Lazy<ICache>(() => new DefaultCache());
#endif
 
        /// <summary>
        /// 获得 缓存所有键值集合
        /// </summary>
        public static IEnumerable<string> Keys { get { return _lazy.Value.Keys; } }
 
        /// <summary>
        /// 获得 跨站点配置项
        /// </summary>
        public static IEnumerable<ICacheCorsItem> CorsSites { get { return _lazy.Value.CorsSites; } }
 
        #region 静态方法
#if !NET45
        internal static void RegisterCacheManager(this IServiceCollection services)
        {
            _lazy = new Lazy<ICache>(() => CreateCache(services.BuildServiceProvider()));
        }
 
        internal static ICache CreateCache(IServiceProvider provider)
        {
            return DefaultCache.Instance ?? CreateDefaultCache(provider);
        }
 
        private static ICache CreateDefaultCache(IServiceProvider provider)
        {
            var httpContextAccessor = provider.GetRequiredService<IHttpContextAccessor>();
            var options = provider.GetRequiredService<IOptionsMonitor<CacheOptions>>();
            var factory = provider.GetRequiredService<IHttpClientFactory>();
            return new DefaultCache(options, httpContextAccessor, factory);
        }
#endif
 
        /// <summary>
        /// 获取或者添加一个缓存项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存项键值</param>
        /// <param name="valueFactory">创建缓存项回调方法</param>
        /// <param name="profileKey">配置文件中缓存键值</param>
        /// <returns></returns>
        public static T GetOrAdd<T>(string key, Func<string, T> valueFactory, string? profileKey = null)
        {
            return _lazy.Value.GetOrAdd(key, valueFactory, profileKey);
        }
 
        /// <summary>
        /// 获取或者添加一个缓存项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="option">缓存项配置 ICacheOption 实例</param>
        /// <param name="valueFactory">创建缓存项回调方法</param>
        /// <returns></returns>
        public static T GetOrAdd<T>(ICacheOption option, Func<string, T> valueFactory)
        {
            return _lazy.Value.GetOrAdd(option, valueFactory);
        }
 
        /// <summary>
        /// 添加或者更新一个缓存项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存项键值</param>
        /// <param name="valueFactory">创建缓存项回调方法</param>
        /// <param name="updateFactory">更新缓存项回调方法</param>
        /// <param name="profileKey">配置文件中缓存键值</param>
        /// <returns></returns>
        public static T AddOrUpdate<T>(string key, Func<string, T> valueFactory, Func<string, T, T> updateFactory, string? profileKey = null)
        {
            return _lazy.Value.AddOrUpdate(key, valueFactory, updateFactory, profileKey);
        }
 
        /// <summary>
        /// 添加或者更新一个缓存项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="option">缓存项配置 ICacheOption 实例</param>
        /// <param name="valueFactory">创建缓存项回调方法</param>
        /// <param name="updateFactory">更新缓存项回调方法</param>
        /// <returns></returns>
        public static T AddOrUpdate<T>(ICacheOption option, Func<string, T> valueFactory, Func<string, T, T> updateFactory)
        {
            return _lazy.Value.AddOrUpdate(option, valueFactory, updateFactory);
        }
 
        /// <summary>
        /// 通过指定缓存键值获取缓存项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存键值</param>
        /// <returns>缓存项</returns>
        public static T Get<T>(string key)
        {
            return _lazy.Value.Get<T>(key);
        }
 
        /// <summary>
        /// 通过指定缓存项键值集合清除缓存
        /// </summary>
        /// <param name="keys">缓存项键值集合</param>
        public static void Clear(IEnumerable<string> keys)
        {
            _lazy.Value.Clear(keys);
        }
 
        /// <summary>
        /// 清除指定缓存项键值缓存,缓存项键值为空时清除所有缓存
        /// </summary>
        public static void Clear(string? key = null)
        {
            if (string.IsNullOrEmpty(key)) _lazy.Value.Clear();
            else _lazy.Value.Clear(key);
        }
 
        /// <summary>
        /// 通过配置文件跨域清理缓存
        /// </summary>
        /// <param name="keys"></param>
        public static void CorsClear(IEnumerable<string> keys)
        {
            _lazy.Value.CorsClear(keys);
        }
 
        /// <summary>
        /// 获得缓存项总数
        /// </summary>
        /// <returns></returns>
        public static int Count => _lazy.Value.Count;
 
        /// <summary>
        /// 获取缓存项集合
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ICacheItem> ToList()
        {
            return _lazy.Value.ToList();
        }
        #endregion
    }
}