@elf-express/admin-net-mcp 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +83 -88
- package/knowledge/Furion_Teaching_Manual/04-1-/351/205/215/347/275/256.md +442 -0
- package/knowledge/Furion_Teaching_Manual/04-2-/351/201/270/351/240/205.md +363 -0
- package/knowledge/Furion_Teaching_Manual/05-1-/345/213/225/346/205/213WebAPI.md +825 -0
- package/knowledge/Furion_Teaching_Manual/05-2-HttpContext.md +217 -0
- package/knowledge/Furion_Teaching_Manual/05-3-/347/257/251/351/201/270/345/231/250/346/224/224/346/210/252/345/231/250AOP.md +581 -0
- package/knowledge/Furion_Teaching_Manual/05-4-/350/253/213/346/261/202/347/250/275/346/240/270/346/227/245/350/252/214.md +129 -0
- package/knowledge/Furion_Teaching_Manual/05-5-/344/270/255/344/273/213/350/273/237/351/253/224Middleware.md +328 -0
- package/knowledge/Furion_Teaching_Manual/05-6-Vue-React-Angular/344/273/213/351/235/242/344/273/243/347/220/206.md +317 -0
- package/knowledge/Furion_Teaching_Manual/06-1/350/246/217/347/257/204/345/214/226/346/216/245/345/217/243.md +1458 -0
- package/knowledge/Furion_Teaching_Manual/06-2/347/254/254/344/270/211/346/226/271API_Scalar.md +91 -0
- package/knowledge/Furion_Teaching_Manual/07-/345/217/213/345/245/275/344/276/213/345/244/226/350/231/225/347/220/206.md +511 -0
- package/knowledge/Furion_Teaching_Manual/08-1-/350/263/207/346/226/231/351/251/227/350/255/211/345/237/272/347/244/216/344/275/277/347/224/250.md +587 -0
- package/knowledge/Furion_Teaching_Manual/10-1-SqlSugar/346/225/264/345/220/210.md +336 -0
- package/knowledge/Furion_Teaching_Manual/11-SaaS /345/244/232/347/247/237/346/210/266/347/255/206/350/250/230.md" +271 -0
- package/knowledge/Furion_Teaching_Manual/12-furion-dependency-injection.md +408 -0
- package/knowledge/Furion_Teaching_Manual/13-/347/211/251/344/273/266/350/263/207/346/226/231/346/230/240/345/260/204/357/274/210Mapster/357/274/211.md +162 -0
- package/knowledge/Furion_Teaching_Manual/14-/345/210/206/345/270/203/345/274/217/347/274/223/345/255/230.md +311 -0
- package/knowledge/Furion_Teaching_Manual/15-/345/256/211/345/205/250/351/211/264/346/235/203.md +832 -0
- package/knowledge/Furion_Teaching_Manual/17-/346/252/242/350/246/226/347/257/204/346/234/254/345/274/225/346/223/216.md +327 -0
- package/knowledge/Furion_Teaching_Manual/18-/346/227/245/350/252/214/350/250/230/351/214/204.md +639 -0
- package/knowledge/Furion_Teaching_Manual/19-1-HTTP/351/201/240/347/253/257/350/253/213/346/261/202/345/237/272/347/244/216/344/275/277/347/224/250.md +621 -0
- package/knowledge/Furion_Teaching_Manual/19-2-HTTP/351/201/240/347/253/257/350/253/213/346/261/202/351/200/262/351/232/216/346/214/207/345/215/227.md +928 -0
- package/knowledge/Furion_Teaching_Manual/19-3-HTTP/351/201/240/347/253/257/350/253/213/346/261/202/345/270/270/350/246/213/345/225/217/351/241/214.md +362 -0
- package/knowledge/Furion_Teaching_Manual/20-/350/263/207/346/226/231/345/212/240/350/247/243/345/257/206.md +286 -0
- package/knowledge/Furion_Teaching_Manual/20-/351/231/204/351/214/204-KSort/350/263/207/346/226/231/347/260/275/345/220/215/345/256/214/346/225/264/345/216/237/345/247/213/347/242/274.md +305 -0
- package/knowledge/Furion_Teaching_Manual/21-/345/205/250/347/220/203/345/214/226/345/222/214/346/234/254/345/234/260/345/214/226.md +342 -0
- package/knowledge/Furion_Teaching_Manual/22-/344/272/213/344/273/266/345/214/257/346/265/201/346/216/222EventBus.md +448 -0
- package/knowledge/Furion_Teaching_Manual/23-JSON/345/272/217/345/210/227/345/214/226.md +340 -0
- package/knowledge/Furion_Teaching_Manual/24-/345/215/263/346/231/202/351/200/232/350/250/212SignalR.md +247 -0
- package/knowledge/Furion_Teaching_Manual/25-/350/274/224/345/212/251/350/247/222/350/211/262/346/234/215/345/213/231WorkerService.md +295 -0
- package/knowledge/Furion_Teaching_Manual/26-1-/346/216/222/347/250/213/344/275/234/346/245/255/345/256/232/346/231/202/344/273/273/345/213/231.md +631 -0
- package/knowledge/Furion_Teaching_Manual/26-2-Cron/350/241/250/351/201/224/345/274/217.md +203 -0
- package/knowledge/Furion_Teaching_Manual/26-3-/344/273/273/345/213/231/344/275/207/345/210/227TaskQueue.md +215 -0
- package/knowledge/Furion_Teaching_Manual/27-/345/210/206/346/225/243/345/274/217ID/347/224/237/346/210/220.md +86 -0
- package/knowledge/Furion_Teaching_Manual/28-/346/250/241/347/265/204/345/214/226/351/226/213/347/231/274.md +68 -0
- package/knowledge/Furion_Teaching_Manual/29-/346/265/201/350/256/212/347/211/251/344/273/266Clay.md +313 -0
- package/knowledge/Furion_Teaching_Manual/30-/350/204/253/346/225/217/350/231/225/347/220/206/357/274/210Sensitive Detection).md" +168 -0
- package/knowledge/Furion_Teaching_Manual/32-/346/234/203/350/251/261/345/222/214/347/213/200/346/205/213/347/256/241/347/220/206.md +147 -0
- package/knowledge/Furion_Teaching_Manual/33-IPC/347/250/213/345/272/217/351/200/232/350/250/212.md +98 -0
- package/knowledge/Furion_Teaching_Manual/34-2-Docker/351/203/250/347/275/262.md +313 -0
- package/knowledge/Furion_Teaching_Manual/34-3-Nginx/351/203/250/347/275/262.md +157 -0
- package/knowledge/Furion_Teaching_Manual/34-8-WindowsService/351/203/250/347/275/262.md +112 -0
- package/knowledge/Furion_Teaching_Manual/35-1-Docker/347/222/260/345/242/203/346/214/201/347/272/214/351/203/250/347/275/262Jenkins.md +169 -0
- package/knowledge/Furion_Teaching_Manual/36-1-/345/226/256/345/205/203/346/270/254/350/251/246/346/225/264/345/220/210/346/270/254/350/251/246.md +275 -0
- package/knowledge/Furion_Teaching_Manual/36-3-/345/237/272/346/272/226/346/270/254/350/251/246Benchmarking.md +80 -0
- package/knowledge/attributes.md +153 -0
- package/knowledge/config.md +147 -0
- package/knowledge/entity.md +115 -0
- package/knowledge/event.md +124 -0
- package/knowledge/plugin.md +136 -0
- package/knowledge/service.md +146 -0
- package/knowledge/sqlsugar.md +172 -0
- package/knowledge/vue-typescript.md +338 -0
- package/package.json +3 -2
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
# 14. 分布式缓存
|
|
2
|
+
|
|
3
|
+
## 14.1 什么是缓存
|
|
4
|
+
|
|
5
|
+
缓存可以减少生成内容所需的工作,从而显著提高应用程序的性能和可伸缩性。 缓存适用于不经常更改的数据,因为生成成本很高。 通过缓存,可比从数据源返回数据的副本速度快得多。 应该对应用进行编写和测试,使其不要永远依赖于缓存的数据。
|
|
6
|
+
|
|
7
|
+
## 14.2 缓存类型
|
|
8
|
+
|
|
9
|
+
- **内存缓存**:顾名思义,就是缓存在应用部署所在服务器的内存中
|
|
10
|
+
- **分布式缓存**:分布式缓存是由多个应用服务器共享的缓存
|
|
11
|
+
- **响应缓存**:缓存服务器端 Not Modified 的数据
|
|
12
|
+
|
|
13
|
+
## 14.3 内存缓存使用
|
|
14
|
+
|
|
15
|
+
内存缓存是最常用的缓存方式,具有存取快,效率高特点。
|
|
16
|
+
|
|
17
|
+
内存缓存通过注入 `IMemoryCache` 方式注入即可。
|
|
18
|
+
|
|
19
|
+
> **备注**:在 Furion 框架中,内存缓存服务已经默认注册,无需手动注册。
|
|
20
|
+
|
|
21
|
+
### 14.3.1 基本使用
|
|
22
|
+
|
|
23
|
+
如,缓存当前时间:
|
|
24
|
+
|
|
25
|
+
```csharp
|
|
26
|
+
using Furion.DynamicApiController;
|
|
27
|
+
using Microsoft.Extensions.Caching.Memory;
|
|
28
|
+
using System;
|
|
29
|
+
|
|
30
|
+
namespace Furion.Application
|
|
31
|
+
{
|
|
32
|
+
public class CacheServices : IDynamicApiController
|
|
33
|
+
{
|
|
34
|
+
private const string _timeCacheKey = "cache_time";
|
|
35
|
+
|
|
36
|
+
private readonly IMemoryCache _memoryCache;
|
|
37
|
+
|
|
38
|
+
public CacheServices(IMemoryCache memoryCache)
|
|
39
|
+
{
|
|
40
|
+
_memoryCache = memoryCache;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
[ApiDescriptionSettings(KeepName = true)]
|
|
44
|
+
public DateTimeOffset GetOrCreate()
|
|
45
|
+
{
|
|
46
|
+
return _memoryCache.GetOrCreate(_timeCacheKey, entry =>
|
|
47
|
+
{
|
|
48
|
+
return DateTimeOffset.UtcNow;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 14.3.2 设置缓存选项
|
|
56
|
+
|
|
57
|
+
内存缓存支持设置缓存时间、缓存大小、及绝对缓存过期时间等。
|
|
58
|
+
|
|
59
|
+
```csharp
|
|
60
|
+
_memoryCache.GetOrCreate(_timeCacheKey, entry =>
|
|
61
|
+
{
|
|
62
|
+
entry.SlidingExpiration = TimeSpan.FromSeconds(3); // 滑动缓存时间
|
|
63
|
+
return DateTimeOffset.UtcNow;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
await _memoryCache.GetOrCreateAsync(_timeCacheKey, async entry =>
|
|
67
|
+
{
|
|
68
|
+
// 这里可以使用异步~~
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
> **关于缓存时间**
|
|
73
|
+
>
|
|
74
|
+
> 具有可调过期的缓存项集存在过时的风险。 如果访问的时间比滑动过期时间间隔更频繁,则该项将永不过期。
|
|
75
|
+
>
|
|
76
|
+
> 将弹性过期与绝对过期组合在一起,以保证项目在其绝对过期时间通过后过期。 绝对过期会将项的上限设置为可缓存项的时间,同时仍允许项在可调整过期时间间隔内未请求时提前过期。
|
|
77
|
+
>
|
|
78
|
+
> 如果同时指定了绝对过期和可调过期时间,则过期时间以逻辑方式运算。 如果滑动过期时间间隔 **或** 绝对过期时间通过,则从缓存中逐出该项。
|
|
79
|
+
|
|
80
|
+
```csharp
|
|
81
|
+
_memoryCache.GetOrCreate(_timeCacheKey, entry =>
|
|
82
|
+
{
|
|
83
|
+
entry.SetSlidingExpiration(TimeSpan.FromSeconds(3));
|
|
84
|
+
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(20);
|
|
85
|
+
return DateTime.Now;
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
前面的代码保证数据的缓存时间不超过绝对时间。
|
|
90
|
+
|
|
91
|
+
### 14.3.3 手动设置缓存选项
|
|
92
|
+
|
|
93
|
+
除了上面的 `Func<MemoryCacheEntryOptions, object>` 方式设置缓存选项,我们可以手动创建并设置:
|
|
94
|
+
|
|
95
|
+
```csharp
|
|
96
|
+
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
|
97
|
+
.SetSlidingExpiration(TimeSpan.FromSeconds(3));
|
|
98
|
+
|
|
99
|
+
_memoryCache.Set(_timeCacheKey, DateTimeOffset.UtcNow, cacheEntryOptions);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### 14.3.4 缓存依赖关系
|
|
103
|
+
|
|
104
|
+
下面的示例演示如何在依赖条目过期后使缓存条目过期。 `CancellationChangeToken` 添加到缓存的项。 当 `Cancel` 在 `CancellationTokenSource` 上调用时,将逐出两个缓存项。
|
|
105
|
+
|
|
106
|
+
```csharp
|
|
107
|
+
public IActionResult CreateDependentEntries()
|
|
108
|
+
{
|
|
109
|
+
var cts = new CancellationTokenSource();
|
|
110
|
+
_cache.Set(CacheKeys.DependentCTS, cts);
|
|
111
|
+
|
|
112
|
+
using (var entry = _cache.CreateEntry(CacheKeys.Parent))
|
|
113
|
+
{
|
|
114
|
+
entry.Value = DateTime.Now;
|
|
115
|
+
entry.RegisterPostEvictionCallback(DependentEvictionCallback, this);
|
|
116
|
+
|
|
117
|
+
_cache.Set(CacheKeys.Child,
|
|
118
|
+
DateTime.Now,
|
|
119
|
+
new CancellationChangeToken(cts.Token));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return RedirectToAction("GetDependentEntries");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public IActionResult GetDependentEntries()
|
|
126
|
+
{
|
|
127
|
+
return View("Dependent", new DependentViewModel
|
|
128
|
+
{
|
|
129
|
+
ParentCachedTime = _cache.Get<DateTime?>(CacheKeys.Parent),
|
|
130
|
+
ChildCachedTime = _cache.Get<DateTime?>(CacheKeys.Child),
|
|
131
|
+
Message = _cache.Get<string>(CacheKeys.DependentMessage)
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public IActionResult RemoveChildEntry()
|
|
136
|
+
{
|
|
137
|
+
_cache.Get<CancellationTokenSource>(CacheKeys.DependentCTS).Cancel();
|
|
138
|
+
return RedirectToAction("GetDependentEntries");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private static void DependentEvictionCallback(object key, object value,
|
|
142
|
+
EvictionReason reason, object state)
|
|
143
|
+
{
|
|
144
|
+
var message = $"Parent entry was evicted. Reason: {reason}.";
|
|
145
|
+
((HomeController)state)._cache.Set(CacheKeys.DependentMessage, message);
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
使用 `CancellationTokenSource` 允许将多个缓存条目作为一个组逐出。 `using` 块中创建的缓存条目将继承触发器和过期设置。
|
|
150
|
+
|
|
151
|
+
> **了解更多**:想了解更多内存中的缓存知识可查阅 [ASP.NET Core - 内存缓存](https://docs.microsoft.com/aspnet/core/performance/caching/memory) 章节。
|
|
152
|
+
|
|
153
|
+
## 14.4 分布式缓存
|
|
154
|
+
|
|
155
|
+
分布式缓存是由多个应用服务器共享的缓存,通常作为外部服务在访问它的应用服务器上维护。 分布式缓存可以提高 ASP.NET Core 应用程序的性能和可伸缩性,尤其是在应用程序由云服务或服务器场托管时。
|
|
156
|
+
|
|
157
|
+
与其他缓存方案相比,分布式缓存具有多项优势,其中缓存的数据存储在单个应用服务器上。
|
|
158
|
+
|
|
159
|
+
当分布式缓存数据时,数据将:
|
|
160
|
+
|
|
161
|
+
- **(一致性)** 跨多个服务器的请求
|
|
162
|
+
- 存活在服务器重启和应用部署之间
|
|
163
|
+
- 不使用本地内存
|
|
164
|
+
|
|
165
|
+
分布式缓存配置是特定于实现的。 本文介绍如何配置 SQL Server 和 Redis 分布式缓存。 第三方实现也可用,例如 GitHub 上的 NCache。
|
|
166
|
+
|
|
167
|
+
无论选择哪种实现,应用都会使用接口 `IDistributedCache` 与缓存交互。
|
|
168
|
+
|
|
169
|
+
### 14.4.1 使用条件
|
|
170
|
+
|
|
171
|
+
- 若要使用 **SQL Server** 分布式缓存,则添加 `Microsoft.Extensions.Caching.SqlServer` 包
|
|
172
|
+
- 若要使用 **Redis** 分布式缓存,则添加 `Microsoft.Extensions.Caching.StackExchangeRedis` 包
|
|
173
|
+
- 若要使用 **NCache** 分布式缓存,则添加 `NCache.Microsoft.Extensions.Caching.OpenSource` 包
|
|
174
|
+
|
|
175
|
+
### 14.4.2 IDistributedCache
|
|
176
|
+
|
|
177
|
+
`IDistributedCache` 接口提供以下方法来处理分布式缓存实现中的项:
|
|
178
|
+
|
|
179
|
+
- **Get / GetAsync**:接受字符串键,并检索缓存项作为 `byte[]` 数组(如果在缓存中找到)
|
|
180
|
+
- **Set / SetAsync**:使用字符串键将项(作为 `byte[]` 数组)添加到缓存中
|
|
181
|
+
- **Refresh / RefreshAsync**:根据项的键刷新缓存中的项,重置其滑动过期超时(如果有)
|
|
182
|
+
- **Remove / RemoveAsync**:根据缓存项的字符串键删除缓存项
|
|
183
|
+
|
|
184
|
+
### 14.4.3 分布式内存缓存
|
|
185
|
+
|
|
186
|
+
分布式内存缓存(`AddDistributedMemoryCache`)是一个框架提供的实现 `IDistributedCache`,它将项存储在内存中。 分布式内存缓存不是实际的分布式缓存,缓存项由应用程序实例存储在运行应用程序的服务器上。
|
|
187
|
+
|
|
188
|
+
**分布式内存缓存优点:**
|
|
189
|
+
|
|
190
|
+
- 用于开发和测试方案
|
|
191
|
+
- 在生产环境中使用单一服务器并且内存消耗不是问题。 实现分布式内存缓存会抽象化缓存的数据存储。 如果需要多个节点或容错,可以在将来实现真正的分布式缓存解决方案。
|
|
192
|
+
|
|
193
|
+
> **备注**:在 Furion 框架中,分布式内存缓存服务已经默认注册,无需手动调用 `services.AddDistributedMemoryCache();` 注册。
|
|
194
|
+
|
|
195
|
+
### 14.4.4 分布式 SQL Server 缓存
|
|
196
|
+
|
|
197
|
+
分布式 SQL Server 缓存实现(`AddDistributedSqlServerCache`)允许分布式缓存使用 SQL Server 数据库作为其后备存储。
|
|
198
|
+
|
|
199
|
+
若要在 SQL Server 实例中创建 SQL Server 缓存的项表,可以使用 `sql-cache` 工具。 该工具将创建一个表,其中包含指定的名称和架构。
|
|
200
|
+
|
|
201
|
+
通过运行命令 `sql-cache create` 创建一个表,提供 SQL Server 实例 (`Data Source`)、数据库 (`Initial Catalog`)、架构(例如 `dbo`)和表名称(例如 `TestCache`):
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DistCache;Integrated Security=True;" dbo TestCache
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
创建成功后,在 `Startup.cs` 中注册即可:
|
|
208
|
+
|
|
209
|
+
```csharp
|
|
210
|
+
services.AddDistributedSqlServerCache(options =>
|
|
211
|
+
{
|
|
212
|
+
options.ConnectionString =
|
|
213
|
+
_config["DistCache_ConnectionString"];
|
|
214
|
+
options.SchemaName = "dbo";
|
|
215
|
+
options.TableName = "TestCache";
|
|
216
|
+
});
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### 14.4.5 分布式 Redis 缓存
|
|
220
|
+
|
|
221
|
+
Redis 是内存中数据存储的开源数据存储,通常用作分布式缓存。在使用时通过 `services.AddStackExchangeRedisCache()` 中注册即可。
|
|
222
|
+
|
|
223
|
+
Redis 基本配置:
|
|
224
|
+
|
|
225
|
+
```csharp
|
|
226
|
+
services.AddStackExchangeRedisCache(options =>
|
|
227
|
+
{
|
|
228
|
+
// 连接字符串,这里也可以读取配置文件
|
|
229
|
+
options.Configuration = "192.168.111.134,password=aW1HAyupRKmiZn3Q";
|
|
230
|
+
// 键名前缀
|
|
231
|
+
options.InstanceName = "furion_";
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### 14.4.6 分布式 NCache 缓存
|
|
236
|
+
|
|
237
|
+
NCache 是在 .NET 和 .Net Core 中以本机方式开发的开源内存中分布式缓存。 NCache 在本地工作并配置为分布式缓存群集,适用于在 Azure 或其他托管平台上运行的 ASP.NET Core 应用。
|
|
238
|
+
|
|
239
|
+
NCache 基本配置:
|
|
240
|
+
|
|
241
|
+
1. 安装 `Alachisoft.NCache.OpenSource.SDK` 包
|
|
242
|
+
2. 在 `ncconf` 中配置缓存群集
|
|
243
|
+
3. 注册 NCache 服务
|
|
244
|
+
|
|
245
|
+
```csharp
|
|
246
|
+
services.AddNCacheDistributedCache(configuration =>
|
|
247
|
+
{
|
|
248
|
+
configuration.CacheName = "demoClusteredCache";
|
|
249
|
+
configuration.EnableLogs = true;
|
|
250
|
+
configuration.ExceptionsEnabled = true;
|
|
251
|
+
});
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## 14.5 分布式缓存使用
|
|
255
|
+
|
|
256
|
+
若要使用 `IDistributedCache` 接口,请通过构造函数依赖注入:
|
|
257
|
+
|
|
258
|
+
```csharp
|
|
259
|
+
public class IndexModel : PageModel
|
|
260
|
+
{
|
|
261
|
+
private readonly IDistributedCache _cache;
|
|
262
|
+
|
|
263
|
+
public IndexModel(IDistributedCache cache)
|
|
264
|
+
{
|
|
265
|
+
_cache = cache;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
public string CachedTimeUTC { get; set; }
|
|
269
|
+
|
|
270
|
+
public async Task OnGetAsync()
|
|
271
|
+
{
|
|
272
|
+
CachedTimeUTC = "Cached Time Expired";
|
|
273
|
+
// 获取分布式缓存
|
|
274
|
+
var encodedCachedTimeUTC = await _cache.GetAsync("cachedTimeUTC");
|
|
275
|
+
|
|
276
|
+
if (encodedCachedTimeUTC != null)
|
|
277
|
+
{
|
|
278
|
+
CachedTimeUTC = Encoding.UTF8.GetString(encodedCachedTimeUTC);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
public async Task<IActionResult> OnPostResetCachedTime()
|
|
283
|
+
{
|
|
284
|
+
var currentTimeUTC = DateTime.UtcNow.ToString();
|
|
285
|
+
byte[] encodedCurrentTimeUTC = Encoding.UTF8.GetBytes(currentTimeUTC);
|
|
286
|
+
|
|
287
|
+
// 设置分布式缓存
|
|
288
|
+
var options = new DistributedCacheEntryOptions()
|
|
289
|
+
.SetSlidingExpiration(TimeSpan.FromSeconds(20));
|
|
290
|
+
|
|
291
|
+
await _cache.SetAsync("cachedTimeUTC", encodedCurrentTimeUTC, options);
|
|
292
|
+
|
|
293
|
+
return RedirectToPage();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## 14.6 分布式缓存建议
|
|
299
|
+
|
|
300
|
+
确定 `IDistributedCache` 最适合你的应用的实现时,请考虑以下事项:
|
|
301
|
+
|
|
302
|
+
- 现有基础结构
|
|
303
|
+
- 性能要求
|
|
304
|
+
- 成本
|
|
305
|
+
- 团队经验
|
|
306
|
+
|
|
307
|
+
缓存解决方案通常依赖于内存中的存储以快速检索缓存的数据,但是,内存是有限的资源,并且很昂贵。 仅将常用数据存储在缓存中。
|
|
308
|
+
|
|
309
|
+
通常,**Redis 缓存提供比 SQL Server 缓存更高的吞吐量和更低的延迟**。 但是,通常需要进行基准测试来确定缓存策略的性能特征。
|
|
310
|
+
|
|
311
|
+
当 SQL Server 用作分布式缓存后备存储时,对缓存使用同一数据库,并且应用的普通数据存储和检索会对这两种情况的性能产生负面影响。 建议使用分布式缓存后备存储的**专用 SQL Server 实例**。
|