59e51671
“wangming”
1
|
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
|
## 简介
缓存的概念,相信大家都很了解了。主要目的是为了提高系统运行效率,减少服务器压力。由于硬盘的访问速度远远小于内存的速度,
- 对于需要频繁访问的数据,我们可以使用缓存。
- 对于需要时间过期功能的数据,我们可以使用缓存。
> 缓存中尽量不要存放重要数据,它和硬盘的区别在于,强制中断之后,缓存的数据将会被清空难以找回。
框架的缓存模块是基于面向对象的。如果接触过其他缓存框架,可能在第一次接触框架的这种方式,会有一定的不适应,但是对于复杂类型、复杂key的情况,面向对象化的方式,边界会更加清晰。
## 使用方式
使用 `IDistributedCache`进行管理缓存,包括
- 缓存读取
- 缓存覆盖
- 缓存删除
- 缓存写入
支持时间过期及批量操作
### IDistributedCache<TCacheItem>简单类型
如果你的缓存的key只是一个固定的字符串,那么直接可以通过依赖注入进行`IDistributedCache<TCacheItem>`接口即可,TCacheItem就是你定义存储下来的缓存对象
例如:
``` cs
//定义存储模型
public class BookCacheItem
{
public string Name { get; set; }
public float Price { get; set; }
}
//进行获取或者添加
public class BookService : ITransientDependency
{
private readonly IDistributedCache<BookCacheItem> _cache;
public BookService(IDistributedCache<BookCacheItem> cache)
{
_cache = cache;
}
public async Task<BookCacheItem> GetAsync(Guid bookId)
{
return await _cache.GetOrAddAsync(
bookId.ToString(), //缓存键
async () => await GetBookFromDatabaseAsync(bookId),
() => new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.Now.AddHours(1)
}
);
}
private Task<BookCacheItem> GetBookFromDatabaseAsync(Guid bookId)
{
//TODO: 从数据库获取数据
}
}
```
### IDistributedCache<TCacheItem, TCacheKey>简单key
同时,推荐使用`IDistributedCache<TCacheItem, TCacheKey>`接口,把key和value同时都当作对象去标识,更加符合面向对象的特征
这里TCacheKey可以为简单类型Guid
``` cs
[CacheName("Books")]
public class BookCacheItem
{
public string Name { get; set; }
public float Price { get; set; }
}
public class BookService : ITransientDependency
{
private readonly IDistributedCache<BookCacheItem, Guid> _cache;
public BookService(IDistributedCache<BookCacheItem, Guid> cache)
{
_cache = cache;
}
public async Task<BookCacheItem> GetAsync(Guid bookId)
{
return await _cache.GetOrAddAsync(
bookId, //Guid类型作为缓存键
async () => await GetBookFromDatabaseAsync(bookId),
() => new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.Now.AddHours(1)
}
);
}
private Task<BookCacheItem> GetBookFromDatabaseAsync(Guid bookId)
{
//TODO: 从数据库获取数据
}
}
```
### 复杂类型的缓存键
如果TCacheKey为复杂类型,可以重写key的ToString方法即可
``` cs
public class UserInOrganizationCacheKey
{
public Guid UserId { get; set; }
public Guid OrganizationId { get; set; }
//构建缓存key
public override string ToString()
{
return $"{UserId}_{OrganizationId}";
}
}
public class BookService : ITransientDependency
{
private readonly IDistributedCache<UserCacheItem, UserInOrganizationCacheKey> _cache;
public BookService(
IDistributedCache<UserCacheItem, UserInOrganizationCacheKey> cache)
{
_cache = cache;
}
...
}
```
## 配置
AbpDistributedCacheOptions 是配置缓存的主要Option类.
示例:为应用程序设置缓存键前缀
``` cs
Configure<AbpDistributedCacheOptions>(options =>
{
options.KeyPrefix = "MyApp1";
});
```
## 其他缓存实现
默认使用的缓存实现是基于`MemoryCache`本地缓存实现的
同时还支持其他的实现:
[Redis支持](https://docs.abp.io/zh-Hans/abp/latest/Redis-Cache)
由于模块化,只需要在对应的模块中添加依赖即可,不需要修改任何其他代码。
|