Skip to content

Commit cd8f115

Browse files
committed
[fix] 修复缓存数据异常问题
1 parent 9659b37 commit cd8f115

File tree

13 files changed

+93
-71
lines changed

13 files changed

+93
-71
lines changed

Ape.Volo.Api/Authentication/Jwt/ApiResponseHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ await Response.WriteAsync(new ActionResultVm
4444
protected override async Task HandleForbiddenAsync(AuthenticationProperties properties)
4545
{
4646
var loginUserInfo = await _apeContext.Cache.GetAsync<LoginUserInfo>(
47-
GlobalConstants.CacheKey.OnlineKey +
47+
GlobalConstants.CachePrefix.OnlineKey +
4848
_apeContext.HttpUser.JwtToken.ToMd5String16());
4949
if (loginUserInfo.IsNull())
5050
{

Ape.Volo.Api/Authentication/Jwt/PermissionHandler.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext
108108
var nowTime = DateTime.Now.ToLocalTime();
109109
if (expTime < nowTime)
110110
{
111-
await _apeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.OnlineKey +
111+
await _apeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.OnlineKey +
112112
_apeContext.HttpUser.JwtToken.ToMd5String16());
113113
context.Fail();
114114
return;
@@ -120,7 +120,7 @@ await _apeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.OnlineKey +
120120
#region 用户缓存信息是否已过期
121121

122122
var loginUserInfo = await _apeContext.Cache.GetAsync<LoginUserInfo>(
123-
GlobalConstants.CacheKey.OnlineKey +
123+
GlobalConstants.CachePrefix.OnlineKey +
124124
_apeContext.HttpUser.JwtToken.ToMd5String16());
125125
if (loginUserInfo == null)
126126
{
@@ -160,7 +160,7 @@ await _apeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.OnlineKey +
160160
};
161161
var onlineKey = onlineUser.AccessToken.ToMd5String16();
162162
var isTrue = await _apeContext.Cache.SetAsync(
163-
GlobalConstants.CacheKey.OnlineKey + onlineKey, onlineUser, TimeSpan.FromHours(2),
163+
GlobalConstants.CachePrefix.OnlineKey + onlineKey, onlineUser, TimeSpan.FromHours(2),
164164
null);
165165
if (!isTrue)
166166
{

Ape.Volo.Api/Controllers/Auth/AuthorizationController.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public async Task<ActionResult<object>> Captcha()
181181
{
182182
var (imgBytes, code) = SixLaborsImageHelper.BuildVerifyCode();
183183
var imgUrl = ImgHelper.ToBase64StringUrl(imgBytes);
184-
var captchaId = GlobalConstants.CacheKey.CaptchaId + GuidHelper.GenerateKey();
184+
var captchaId = GlobalConstants.CachePrefix.CaptchaId + GuidHelper.GenerateKey();
185185
await _apeContext.Cache.SetAsync(captchaId, code, TimeSpan.FromMinutes(2), null);
186186
var dic = new Dictionary<string, string> { { "img", imgUrl }, { "captchaId", captchaId } };
187187
return dic.ToJson();
@@ -215,12 +215,14 @@ public async Task<ActionResult<object>> Logout()
215215
{
216216
//清理缓存
217217
if (!_apeContext.HttpUser.IsNotNull()) return Success();
218-
await _apeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.OnlineKey +
218+
await _apeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.OnlineKey +
219219
_apeContext.HttpUser.JwtToken.ToMd5String16());
220-
await _apeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserInfoById +
220+
await _apeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserInfoById +
221221
_apeContext.HttpUser.Id.ToString().ToMd5String16());
222-
await _apeContext.Cache.RemoveAsync(
223-
GlobalConstants.CacheKey.UserInfoByName + _apeContext.HttpUser.Account.ToMd5String16());
222+
223+
await _apeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserMenuById +
224+
_apeContext.HttpUser.Id.ToString().ToMd5String16());
225+
224226

225227
return Success();
226228
}
@@ -253,7 +255,7 @@ private async Task<string> LoginResult(UserDto userDto, string type)
253255
loginUserInfo.AccessToken = refresh ? token.RefreshToken : token.AccessToken;
254256
var onlineKey = loginUserInfo.AccessToken.ToMd5String16();
255257
await _apeContext.Cache.SetAsync(
256-
GlobalConstants.CacheKey.OnlineKey + onlineKey,
258+
GlobalConstants.CachePrefix.OnlineKey + onlineKey,
257259
loginUserInfo, TimeSpan.FromHours(2), CacheExpireType.Absolute);
258260

259261
switch (type)

Ape.Volo.Api/Controllers/Permission/MenuController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public async Task<ActionResult<object>> GetMenuLazy(long pid)
130130
return Error("pid cannot be empty");
131131
}
132132

133-
var menulist = await _menuService.FindByPIdAsync(pid);
134-
return menulist.ToJsonByIgnore();
133+
var menuList = await _menuService.FindByPIdAsync(pid);
134+
return menuList.ToJsonByIgnore();
135135
}
136136

137137
/// <summary>

Ape.Volo.Business/Monitor/OnlineUserService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public OnlineUserService(ICache cache, ITokenBlacklistService tokenBlacklistServ
2727
public async Task<List<LoginUserInfo>> QueryAsync(Pagination pagination)
2828
{
2929
List<LoginUserInfo> loginUserInfos = new List<LoginUserInfo>();
30-
var arrayList = await _cache.ScriptEvaluateKeys(GlobalConstants.CacheKey.OnlineKey);
30+
var arrayList = await _cache.ScriptEvaluateKeys(GlobalConstants.CachePrefix.OnlineKey);
3131
if (arrayList.Length > 0)
3232
{
3333
foreach (var item in arrayList)
@@ -59,15 +59,15 @@ public async Task DropOutAsync(HashSet<string> ids)
5959
{
6060
foreach (var item in ids)
6161
{
62-
await _cache.RemoveAsync(GlobalConstants.CacheKey.OnlineKey + item);
62+
await _cache.RemoveAsync(GlobalConstants.CachePrefix.OnlineKey + item);
6363
}
6464
}
6565
}
6666

6767
public async Task<List<ExportBase>> DownloadAsync()
6868
{
6969
List<ExportBase> onlineUserExports = new List<ExportBase>();
70-
var arrayList = await _cache.ScriptEvaluateKeys(GlobalConstants.CacheKey.OnlineKey);
70+
var arrayList = await _cache.ScriptEvaluateKeys(GlobalConstants.CachePrefix.OnlineKey);
7171
if (arrayList.Length > 0)
7272
{
7373
foreach (var item in arrayList)

Ape.Volo.Business/Permission/MenuService.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ await TableWhere(x => x.Permission == createUpdateMenuDto.Permission)
9292
if (menu.ParentId > 0)
9393
{
9494
//清理缓存
95-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.LoadMenusByPId +
95+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadMenusByPId +
9696
menu.ParentId.ToString().ToMd5String16());
9797
var tempMenu = await TableWhere(x => x.Id == menu.ParentId).FirstAsync();
9898
if (tempMenu.IsNotNull())
@@ -103,6 +103,7 @@ await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.LoadMenusByPId +
103103
}
104104
}
105105

106+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadAllMenu);
106107
return true;
107108
}
108109

@@ -152,11 +153,11 @@ await TableWhere(m => m.ComponentName.Equals(createUpdateMenuDto.ComponentName))
152153
var createUpdateMenu = ApeContext.Mapper.Map<Menu>(createUpdateMenuDto);
153154
await UpdateEntityAsync(createUpdateMenu);
154155
//清理缓存
155-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.LoadMenusById +
156+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadMenusById +
156157
createUpdateMenu.Id.ToString().ToMd5String16());
157158
if (createUpdateMenu.ParentId > 0)
158159
{
159-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.LoadMenusByPId +
160+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadMenusByPId +
160161
createUpdateMenu.ParentId.ToString().ToMd5String16());
161162
}
162163

@@ -186,6 +187,7 @@ await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.LoadMenusByPId +
186187
}
187188
}
188189

190+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadAllMenu);
189191
return true;
190192
}
191193

@@ -209,13 +211,14 @@ public async Task<bool> DeleteAsync(HashSet<long> ids)
209211
//清除缓存
210212
foreach (var id in idList)
211213
{
212-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.LoadMenusById +
214+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadMenusById +
213215
id.ToString().ToMd5String16());
214-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.LoadMenusByPId +
216+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadMenusByPId +
215217
id.ToString().ToMd5String16());
216218
}
217219
}
218220

221+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadAllMenu);
219222
return isTrue;
220223
}
221224

@@ -261,15 +264,16 @@ public async Task<List<ExportBase>> DownloadAsync(MenuQueryCriteria menuQueryCri
261264
public async Task<List<MenuDto>> QueryAllAsync()
262265
{
263266
var menuDtos = await ApeContext.Cache.GetAsync<List<MenuDto>>("menus:LoadAllMenu");
264-
if (menuDtos.IsNotNull())
267+
if (menuDtos.Any())
265268
{
266269
return menuDtos;
267270
}
268271

269272
menuDtos = ApeContext.Mapper.Map<List<MenuDto>>(await Table.ToListAsync());
270-
if (menuDtos.IsNotNull())
273+
if (menuDtos.Any())
271274
{
272-
await ApeContext.Cache.SetAsync("menus:LoadAllMenu", menuDtos, TimeSpan.FromSeconds(120), null);
275+
await ApeContext.Cache.SetAsync(GlobalConstants.CachePrefix.LoadAllMenu, menuDtos,
276+
TimeSpan.FromSeconds(120), null);
273277
}
274278

275279
return menuDtos;
@@ -280,7 +284,7 @@ public async Task<List<MenuDto>> QueryAllAsync()
280284
/// </summary>
281285
/// <param name="userId">用户ID</param>
282286
/// <returns></returns>
283-
[UseCache(KeyPrefix = GlobalConstants.CacheKey.UserBuildMenuById)]
287+
[UseCache(Expiration = 120, KeyPrefix = GlobalConstants.CachePrefix.UserMenuById)]
284288
public async Task<List<MenuTreeVo>> BuildTreeAsync(long userId)
285289
{
286290
var user = await _userService.QueryByIdAsync(userId);
@@ -317,7 +321,7 @@ public async Task<List<MenuTreeVo>> BuildTreeAsync(long userId)
317321
}
318322

319323

320-
[UseCache(Expiration = 30, KeyPrefix = GlobalConstants.CacheKey.LoadMenusById)]
324+
[UseCache(Expiration = 30, KeyPrefix = GlobalConstants.CachePrefix.LoadMenusById)]
321325
public async Task<List<MenuDto>> FindSuperiorAsync(long id)
322326
{
323327
Expression<Func<Menu, bool>> whereLambda = m => true;
@@ -428,7 +432,7 @@ private async Task<List<MenuTreeVo>> BuildAsync(List<MenuDto> menuDtOs)
428432
return menuVos;
429433
}
430434

431-
[UseCache(Expiration = 30, KeyPrefix = GlobalConstants.CacheKey.LoadMenusByPId)]
435+
[UseCache(Expiration = 30, KeyPrefix = GlobalConstants.CachePrefix.LoadMenusByPId)]
432436
public async Task<List<MenuDto>> FindByPIdAsync(long pid = 0)
433437
{
434438
List<MenuDto> menuDtos = ApeContext.Mapper.Map<List<MenuDto>>(await TableWhere(x => x.ParentId == pid,

Ape.Volo.Business/Permission/PermissionService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class PermissionService : BaseServices<Role>, IPermissionService
2121
/// </summary>
2222
/// <param name="userId"></param>
2323
/// <returns></returns>
24-
[UseCache(Expiration = 30, KeyPrefix = GlobalConstants.CacheKey.UserPermissionRoles)]
24+
[UseCache(Expiration = 30, KeyPrefix = GlobalConstants.CachePrefix.UserPermissionRoles)]
2525
public async Task<List<string>> GetPermissionRolesAsync(long userId)
2626
{
2727
var permissionRoles =
@@ -44,7 +44,7 @@ await SugarRepository.QueryMuchAsync<Menu, RoleMenu, UserRole, string>(
4444
/// </summary>
4545
/// <param name="userId"></param>
4646
/// <returns></returns>
47-
[UseCache(Expiration = 60, KeyPrefix = GlobalConstants.CacheKey.UserPermissionUrls)]
47+
[UseCache(Expiration = 60, KeyPrefix = GlobalConstants.CachePrefix.UserPermissionUrls)]
4848
public async Task<List<PermissionVo>> GetPermissionVoAsync(long userId)
4949
{
5050
var permissionVos =

Ape.Volo.Business/Permission/RoleService.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ public async Task<bool> UpdateRolesMenusAsync(CreateUpdateRoleDto createUpdateRo
239239
//删除用户缓存
240240
foreach (var user in role.Users)
241241
{
242-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserPermissionRoles +
242+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserPermissionRoles +
243+
user.Id.ToString().ToMd5String16());
244+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserMenuById +
243245
user.Id.ToString().ToMd5String16());
244246
}
245247

@@ -270,7 +272,9 @@ public async Task<bool> UpdateRolesApisAsync(CreateUpdateRoleDto createUpdateRol
270272
//删除用户缓存
271273
foreach (var user in role.Users)
272274
{
273-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserPermissionUrls +
275+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserPermissionUrls +
276+
user.Id.ToString().ToMd5String16());
277+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserMenuById +
274278
user.Id.ToString().ToMd5String16());
275279
}
276280
}

Ape.Volo.Business/Permission/UserService.cs

+14-23
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ await TableWhere(x => x.Phone == createUpdateUserDto.Phone).AnyAsync())
160160
await SugarClient.Insertable(userJobs).ExecuteCommandAsync();
161161

162162
//清理缓存
163-
await ClearUserCache(user);
163+
await ClearUserCache(user.Id);
164164
return true;
165165
}
166166

@@ -176,7 +176,7 @@ public async Task<bool> DeleteAsync(HashSet<long> ids)
176176
var users = await TableWhere(x => ids.Contains(x.Id)).ToListAsync();
177177
foreach (var user in users)
178178
{
179-
await ClearUserCache(user);
179+
await ClearUserCache(user.Id);
180180
}
181181

182182
return await LogicDelete<User>(x => ids.Contains(x.Id)) > 0;
@@ -227,7 +227,7 @@ public async Task<List<ExportBase>> DownloadAsync(UserQueryCriteria userQueryCri
227227

228228
#region 扩展方法
229229

230-
[UseCache(Expiration = 30, KeyPrefix = GlobalConstants.CacheKey.UserInfoById)]
230+
[UseCache(Expiration = 60, KeyPrefix = GlobalConstants.CachePrefix.UserInfoById)]
231231
public async Task<UserDto> QueryByIdAsync(long userId)
232232
{
233233
var user = await TableWhere(x => x.Id == userId).Includes(x => x.Dept).Includes(x => x.Roles)
@@ -241,7 +241,6 @@ public async Task<UserDto> QueryByIdAsync(long userId)
241241
/// </summary>
242242
/// <param name="userName">邮箱 or 用户名</param>
243243
/// <returns></returns>
244-
[UseCache(Expiration = 30, KeyPrefix = GlobalConstants.CacheKey.UserInfoByName)]
245244
public async Task<UserDto> QueryByNameAsync(string userName)
246245
{
247246
User user;
@@ -326,13 +325,11 @@ public async Task<bool> UpdatePasswordAsync(UpdateUserPassDto userPassDto)
326325
if (isTrue)
327326
{
328327
//清理缓存
329-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserInfoById +
328+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserInfoById +
330329
curUser.Id.ToString().ToMd5String16());
331-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserInfoByName +
332-
curUser.Username.ToMd5String16());
333330

334331
//退出当前用户
335-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.OnlineKey +
332+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.OnlineKey +
336333
ApeContext.HttpUser.JwtToken.ToMd5String16());
337334
}
338335

@@ -357,7 +354,7 @@ public async Task<bool> UpdateEmailAsync(UpdateUserEmailDto updateUserEmailDto)
357354
}
358355

359356
var code = await ApeContext.Cache.GetAsync<string>(
360-
GlobalConstants.CacheKey.EmailCaptchaKey + updateUserEmailDto.Email.ToMd5String16());
357+
GlobalConstants.CachePrefix.EmailCaptcha + updateUserEmailDto.Email.ToMd5String16());
361358
if (code.IsNullOrEmpty() || !code.Equals(updateUserEmailDto.Code))
362359
{
363360
throw new BadRequestException("验证码错误");
@@ -393,7 +390,7 @@ public async Task<bool> UpdateAvatarAsync(IFormFile file)
393390
string relativePath = Path.GetRelativePath(prefix, avatarPath);
394391
relativePath = "/" + relativePath.Replace("\\", "/");
395392
curUser.AvatarPath = relativePath;
396-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserInfoById +
393+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserInfoById +
397394
curUser.Id.ToString().ToMd5String16());
398395
return await UpdateEntityAsync(curUser);
399396
}
@@ -402,23 +399,17 @@ await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserInfoById +
402399

403400
#region 用户缓存
404401

405-
private async Task ClearUserCache(User user)
402+
private async Task ClearUserCache(long userId)
406403
{
407404
//清理缓存
408-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserInfoById +
409-
user.Id.ToString().ToMd5String16());
410-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserInfoByName +
411-
user.Username.ToMd5String16());
412-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserRolesById +
413-
user.Id.ToString().ToMd5String16());
414-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserJobsById +
415-
user.Id.ToString().ToMd5String16());
405+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserInfoById +
406+
userId.ToString().ToMd5String16());
416407
await ApeContext.Cache.RemoveAsync(
417-
GlobalConstants.CacheKey.UserPermissionUrls + user.Id.ToString().ToMd5String16());
408+
GlobalConstants.CachePrefix.UserPermissionUrls + userId.ToString().ToMd5String16());
418409
await ApeContext.Cache.RemoveAsync(
419-
GlobalConstants.CacheKey.UserPermissionRoles + user.Id.ToString().ToMd5String16());
420-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.UserBuildMenuById +
421-
user.Id.ToString().ToMd5String16());
410+
GlobalConstants.CachePrefix.UserPermissionRoles + userId.ToString().ToMd5String16());
411+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.UserMenuById +
412+
userId.ToString().ToMd5String16());
422413
}
423414

424415
#endregion

Ape.Volo.Business/Queued/QueuedEmailService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ public async Task<bool> ResetEmail(string emailAddress, string messageTemplateNa
148148
queuedEmail.SentTries = 1;
149149
queuedEmail.EmailAccountId = emailAccount.Id;
150150

151-
await ApeContext.Cache.RemoveAsync(GlobalConstants.CacheKey.EmailCaptchaKey +
151+
await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.EmailCaptcha +
152152
queuedEmail.To.ToMd5String());
153153
var isTrue = await ApeContext.Cache.SetAsync(
154-
GlobalConstants.CacheKey.EmailCaptchaKey + queuedEmail.To.ToMd5String(), captcha,
154+
GlobalConstants.CachePrefix.EmailCaptcha + queuedEmail.To.ToMd5String(), captcha,
155155
TimeSpan.FromMinutes(5), null);
156156

157157
if (isTrue)

0 commit comments

Comments
 (0)