记得上下班打卡 | git大法好,push需谨慎

Commit 448ea260 authored by 姜秀龙's avatar 姜秀龙

LostFound 发现缓存有很多问题

parent 0f72e88e
...@@ -63,6 +63,11 @@ public class SweetLostFoundAdminController { ...@@ -63,6 +63,11 @@ public class SweetLostFoundAdminController {
@GetMapping("/permission/{phone}/{performanceId}") @GetMapping("/permission/{phone}/{performanceId}")
@ApiOperation("获取权限") @ApiOperation("获取权限")
/**
* 1. 删除不能图简单直接删除两个 key 这样容易删除单站把全站的也删除掉
* 2. 不能再读取的时候读不到在查 sql 再写入 redis 这里的情况不适用,没权限的用户导致一直查 sql 了
* 另外还会导致因为权限混在一起,crud 的时候考虑的会太复杂,现在分开处理逻辑就简单些,所以要在 cu的写入 redis
*/
public ResponseDto<SweetLostFoundAdminVo> getPermission( public ResponseDto<SweetLostFoundAdminVo> getPermission(
@ApiParam("手机号") @PathVariable String phone, @ApiParam("手机号") @PathVariable String phone,
@ApiParam("演出ID") @PathVariable String performanceId) { @ApiParam("演出ID") @PathVariable String performanceId) {
......
...@@ -64,7 +64,13 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd ...@@ -64,7 +64,13 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd
foundAdmin.setPermissionType(admin.getPermissionType()); foundAdmin.setPermissionType(admin.getPermissionType());
foundAdmin.setAuthScope(admin.getAuthScope()); foundAdmin.setAuthScope(admin.getAuthScope());
foundAdmin.setPerformanceId(performanceId); foundAdmin.setPerformanceId(performanceId);
return ResponseDto.success(baseMapper.insert(foundAdmin) > 0); boolean result = baseMapper.insert(foundAdmin) > 0;
SweetLostFoundAdminVo vo = new SweetLostFoundAdminVo();
BeanUtils.copyProperties(foundAdmin, vo);
lostFoundRedisUtils.setAdminCache(phone, performanceId, vo);
return ResponseDto.success(result);
} }
// 假设: // 假设:
...@@ -122,11 +128,12 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd ...@@ -122,11 +128,12 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd
foundAdmin.setAuthScope(admin.getAuthScope()); foundAdmin.setAuthScope(admin.getAuthScope());
foundAdmin.setPerformanceId(performanceId); foundAdmin.setPerformanceId(performanceId);
// 更新缓存 // 缓存
lostFoundRedisUtils.deleteAdminCache(phone, performanceId); SweetLostFoundAdminVo vo = new SweetLostFoundAdminVo();
if (!phone.equals(existingAdmin.getPhone())) { BeanUtils.copyProperties(foundAdmin, vo);
lostFoundRedisUtils.deleteAdminCache(existingAdmin.getPhone(), performanceId); lostFoundRedisUtils.setAdminCache(phone, performanceId, vo);
} // 因为可能会更改权限范围 写入有范围的判断 所以直接删除老的
lostFoundRedisUtils.deleteAdminCache(existingAdmin.getPhone(), existingAdmin.getPerformanceId(), existingAdmin.getAuthScope());
return ResponseDto.success(baseMapper.updateById(foundAdmin) > 0); return ResponseDto.success(baseMapper.updateById(foundAdmin) > 0);
} }
...@@ -142,7 +149,7 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd ...@@ -142,7 +149,7 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd
// 逻辑删除:更新is_deleted字段为1 // 逻辑删除:更新is_deleted字段为1
admin.setIsDeleted(1); admin.setIsDeleted(1);
lostFoundRedisUtils.deleteAdminCache(admin.getPhone(), admin.getPerformanceId()); lostFoundRedisUtils.deleteAdminCache(admin.getPhone(), admin.getPerformanceId(), admin.getAuthScope());
return baseMapper.updateById(admin) > 0; return baseMapper.updateById(admin) > 0;
} }
...@@ -195,24 +202,24 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd ...@@ -195,24 +202,24 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd
@Override @Override
public SweetLostFoundAdminVo hasPermission(String phone, String performanceId) { public SweetLostFoundAdminVo hasPermission(String phone, String performanceId) {
SweetLostFoundAdminVo adminCache = lostFoundRedisUtils.getAdminCache(phone, performanceId); SweetLostFoundAdminVo adminCache = lostFoundRedisUtils.getAdminCache(phone, performanceId);
if (adminCache == null) { // if (adminCache == null) {
QueryWrapper<SweetLostFoundAdmin> queryWrapper = new QueryWrapper<>(); // QueryWrapper<SweetLostFoundAdmin> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("phone", phone.trim()) // queryWrapper.eq("phone", phone.trim())
.eq("is_deleted", 0); // .eq("is_deleted", 0);
queryWrapper.and(wrapper -> // queryWrapper.and(wrapper ->
wrapper.eq("performance_id", performanceId.trim()) // wrapper.eq("performance_id", performanceId.trim())
.or() // .or()
.eq("auth_scope", 2) // .eq("auth_scope", 2)
); // );
SweetLostFoundAdmin admin = baseMapper.selectOne(queryWrapper); // SweetLostFoundAdmin admin = baseMapper.selectOne(queryWrapper);
if (admin == null) { // if (admin == null) {
return null; // return null;
} // }
SweetLostFoundAdminVo vo = new SweetLostFoundAdminVo(); // SweetLostFoundAdminVo vo = new SweetLostFoundAdminVo();
BeanUtils.copyProperties(admin, vo); // BeanUtils.copyProperties(admin, vo);
lostFoundRedisUtils.setAdminCache(phone, performanceId, vo); // lostFoundRedisUtils.setAdminCache(phone, performanceId, vo);
return vo; // return vo;
} // }
return adminCache; return adminCache;
} }
} }
\ No newline at end of file
...@@ -162,10 +162,18 @@ public class LostFoundRedisUtils { ...@@ -162,10 +162,18 @@ public class LostFoundRedisUtils {
*/ */
public SweetLostFoundAdminVo getAdminCache(String phone, String performanceId) { public SweetLostFoundAdminVo getAdminCache(String phone, String performanceId) {
try { try {
String key = ADMIN_DETAIL_KEY + phone + ":" + performanceId; // 优先检查全站管理员缓存
Object cachedValue = redisUtil.get(key); String globalKey = ADMIN_DETAIL_KEY + phone + ":global";
if (cachedValue != null) { Object globalCache = redisUtil.get(globalKey);
return (SweetLostFoundAdminVo) cachedValue; if (globalCache != null) {
return (SweetLostFoundAdminVo) globalCache;
}
// 检查特定演出管理员缓存
String specificKey = ADMIN_DETAIL_KEY + phone + ":" + performanceId;
Object specificCache = redisUtil.get(specificKey);
if (specificCache != null) {
return (SweetLostFoundAdminVo) specificCache;
} }
} catch (Exception e) { } catch (Exception e) {
log.error("检查管理员缓存失败", e); log.error("检查管理员缓存失败", e);
...@@ -182,8 +190,15 @@ public class LostFoundRedisUtils { ...@@ -182,8 +190,15 @@ public class LostFoundRedisUtils {
*/ */
public void setAdminCache(String phone, String performanceId, SweetLostFoundAdminVo vo) { public void setAdminCache(String phone, String performanceId, SweetLostFoundAdminVo vo) {
try { try {
String key = ADMIN_DETAIL_KEY + phone + ":" + performanceId; if (vo.getAuthScope() != null && vo.getAuthScope() == 2) {
redisUtil.set(key, vo, DETAIL_EXPIRE_TIME); // 全站管理员使用全局缓存键
String globalKey = ADMIN_DETAIL_KEY + phone + ":global";
redisUtil.set(globalKey, vo, DETAIL_EXPIRE_TIME);
} else {
// 特定演出管理员使用演出特定的缓存键
String specificKey = ADMIN_DETAIL_KEY + phone + ":" + performanceId;
redisUtil.set(specificKey, vo, DETAIL_EXPIRE_TIME);
}
} catch (Exception e) { } catch (Exception e) {
log.error("设置管理员缓存失败", e); log.error("设置管理员缓存失败", e);
} }
...@@ -195,12 +210,25 @@ public class LostFoundRedisUtils { ...@@ -195,12 +210,25 @@ public class LostFoundRedisUtils {
* @param phone 手机号 * @param phone 手机号
* @param performanceId 演出ID * @param performanceId 演出ID
*/ */
public void deleteAdminCache(String phone, String performanceId) { public void deleteAdminCache(String phone, String performanceId, Integer authScope) {
try { try {
String key = ADMIN_DETAIL_KEY + phone + ":" + performanceId; if (authScope != null && authScope == 1) {
redisUtil.del(key); // 删除特定演出管理员缓存
String specificKey = ADMIN_DETAIL_KEY + phone + ":" + performanceId;
redisUtil.del(specificKey);
} else if (authScope != null && authScope == 2) {
// 删除全站管理员缓存
String globalKey = ADMIN_DETAIL_KEY + phone + ":global";
redisUtil.del(globalKey);
} else {
String specificKey = ADMIN_DETAIL_KEY + phone + ":" + performanceId;
redisUtil.del(specificKey);
String globalKey = ADMIN_DETAIL_KEY + phone + ":global";
redisUtil.del(globalKey);
}
} catch (Exception e) { } catch (Exception e) {
log.error("删除管理员缓存失败", e); log.error("删除管理员缓存失败", e);
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment