1.凍結(jié)用戶,將數(shù)據(jù)存入Redis,并設(shè)置key的失效時間。
2.解凍用戶,刪除Redis數(shù)據(jù)
3.探花系統(tǒng)在用戶登錄,評論,發(fā)布動態(tài)時判斷其凍結(jié)狀態(tài),如果被凍結(jié)拋出異常
用戶狀態(tài)枚舉
public enum UserStatusEnum { FREEZE_LOGIN("1","凍結(jié)登錄"), FREEZE_FANYAN("2","凍結(jié)評論"), FREEZE_DONGTAI("3","凍結(jié)發(fā)布動態(tài)"); private String code; private String msg; UserStatusEnum(String code, String msg) { this.code = code; this.msg = msg; } public String getCode() { return code; } public String getMsg() { return msg; } }
后臺代碼
/** * 凍結(jié)用戶 * @param map * @return */ public HashMap<String, String> freezeUser(Map map) { // 1.構(gòu)造key String userId = map.get("userId").toString(); String key = Constants.FREEZE_USER + userId; // 2.獲取凍結(jié)時間,設(shè)置redis的存活時間 int day=0; Integer freezingTime = Integer.parseInt(map.get("freezingTime").toString()) ; if (freezingTime==1){ day=3; } if (freezingTime==2){ day=7; } // 3.存入redis String value = JSON.toJSONString(map); if (day>0){ redisTemplate.opsForValue().set(key,value,day,TimeUnit.DAYS); }else { redisTemplate.opsForValue().set(key,value); } HashMap<String, String> msg = new HashMap<>(); msg.put("message","凍結(jié)用戶成功"); return msg; } /** * 解凍用戶 * @param map * @return */ public HashMap<Object, Object> unfreezeUser(Map map) { String userId = map.get("userId").toString(); String key = Constants.FREEZE_USER + userId; redisTemplate.delete(key); HashMap<Object, Object> res = new HashMap<>(); res.put("message","解凍用戶成功"); return res; }
前臺代碼
@Service public class UserFreezeService { @Autowired private RedisTemplate redisTemplate; public void checkStatus(String freezeRange, Long userId) { // 取出value,判斷封禁類型 String key = Constants.FREEZE_USER + userId.toString(); String vlaue = (String) redisTemplate.opsForValue().get(key); if (!StringUtils.isEmpty(vlaue)) { Map map = JSON.parseObject(vlaue, Map.class); String range = (String) map.get("freezingRange"); // 取出封禁類型進行判斷 if (freezeRange.equals(range)) { throw new BusinessException(ErrorResult.countError()); } } } }
本文摘自 :https://www.cnblogs.com/