引入混合缓存
This commit is contained in:
parent
28523c58b2
commit
bfd1ee0547
25
notes/helloshop/diagrams/hybridcache.drawio
Normal file
25
notes/helloshop/diagrams/hybridcache.drawio
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<mxfile host="65bd71144e">
|
||||||
|
<diagram id="IbUPyTJ7BBrHTTByFtqF" name="Page-1">
|
||||||
|
<mxGraphModel dx="1261" dy="1139" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
|
||||||
|
<root>
|
||||||
|
<mxCell id="0"/>
|
||||||
|
<mxCell id="1" parent="0"/>
|
||||||
|
<mxCell id="8" style="edgeStyle=none;html=1;" edge="1" parent="1" source="4" target="7">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4" value="Service 1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="40" width="270" height="190" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="9" style="edgeStyle=none;html=1;" edge="1" parent="1" source="5" target="7">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="5" value="Service 2" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="290" width="270" height="190" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="7" value="Redis" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="450" y="180" width="250" height="170" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
</root>
|
||||||
|
</mxGraphModel>
|
||||||
|
</diagram>
|
||||||
|
</mxfile>
|
82
notes/helloshop/hybrid-cache-library.md
Normal file
82
notes/helloshop/hybrid-cache-library.md
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
# 使用 HybridCache 混合缓存库
|
||||||
|
|
||||||
|
混合缓存可以同时使用内存缓存和分布式缓存,它可以提高缓存的命中率,减少对分布式缓存的访问,从而提高性能。
|
||||||
|
|
||||||
|
- 当从缓存中获取数据时,它首先会从内存缓存中获取数据,如果内存缓存中没有数据,它会从分布式缓存中获取数据。
|
||||||
|
- 当向缓存中写入数据时,它会同时向内存缓存和分布式缓存中写入数据。
|
||||||
|
|
||||||
|
```text
|
||||||
|
|
||||||
|
Service 1 ----> HybridCache ----> MemoryCache ----> DistributedCache
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 安装程序包
|
||||||
|
|
||||||
|
```shell
|
||||||
|
dotnet add package Microsoft.Extensions.Caching.Hybrid
|
||||||
|
```
|
||||||
|
|
||||||
|
## 配置服务
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
builder.Services.AddHybridCache();
|
||||||
|
```
|
||||||
|
|
||||||
|
## 使用服务
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class SomeService(HybridCache cache)
|
||||||
|
{
|
||||||
|
private HybridCache _cache = cache;
|
||||||
|
|
||||||
|
public async Task<string> GetSomeInfoAsync(string name, int id, CancellationToken token = default)
|
||||||
|
{
|
||||||
|
return await _cache.GetOrCreateAsync( $"{name}-{id}",async cancel => await GetDataFromTheSourceAsync(name, id, cancel),cancellationToken: token );
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetDataFromTheSourceAsync(string name, int id, CancellationToken token)
|
||||||
|
{
|
||||||
|
string someInfo = $"someinfo-{name}-{id}";
|
||||||
|
return someInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 按标记移除缓存条目
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class SomeService(HybridCache cache)
|
||||||
|
{
|
||||||
|
private HybridCache _cache = cache;
|
||||||
|
|
||||||
|
public async Task<string> GetSomeInfoAsync(string name, int id, CancellationToken token = default)
|
||||||
|
{
|
||||||
|
var tags = new List<string> { "tag1", "tag2", "tag3" };
|
||||||
|
var entryOptions = new HybridCacheEntryOptions
|
||||||
|
{
|
||||||
|
Expiration = TimeSpan.FromMinutes(1),
|
||||||
|
LocalCacheExpiration = TimeSpan.FromMinutes(1)
|
||||||
|
};
|
||||||
|
return await _cache.GetOrCreateAsync(
|
||||||
|
$"{name}-{id}", // Unique key to the cache entry
|
||||||
|
async cancel => await GetDataFromTheSourceAsync(name, id, cancel),
|
||||||
|
entryOptions,
|
||||||
|
tags,
|
||||||
|
cancellationToken: token
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetDataFromTheSourceAsync(string name, int id, CancellationToken token)
|
||||||
|
{
|
||||||
|
string someInfo = $"someinfo-{name}-{id}";
|
||||||
|
return someInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RemoveByTagAsync(string tag)
|
||||||
|
{
|
||||||
|
await _cache.RemoveByTagAsync(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user