情景:系统发通知给每一个员工,每个员工有一个系统系统,点击提示,通知列表有已读和未读
主要通过redis来实现,大致思路:将通知和员工绑定作为key,如果员工点进链接证明已读则将key存入redis中,那么通知列表中每一条通知如果有这个key,则是已读,反之则是未读。
接口部分代码:
实现:
public boolean getRead(String userId,String loginName){ boolean flag = false; Notice notice = new Notice(); notice.setStatus("0"); List<Notice> notices = noticeMapper.selectNoticeList(notice); for (Notice n : notices){ Long noticeId = n.getNoticeId(); String key = "TO_READ" + noticeId + userId + loginName; //检查缓存中是否有已读标识 if (!redisCache.hasKey(key)){ flag = true; break; } } return flag; }
通知列表已读未读:
public List<Notice> read(String userId, String loginName) { Notice notice = new Notice(); notice.setStatus("0"); List<Notice> notices = noticeMapper.selectNoticeList(notice); for (Notice n : notices){ Long noticeId = n.getNoticeId(); String key = "TO_READ" + noticeId + userId + loginName; //检查缓存中是否有已读标识, if (!redisCache.hasKey(key)){ //设置未读标识 n.setRead(true); } } return notices; }
点击公告详情触发已读
public AjaxResult getNoticeDetail(Long noticeId,String userId,String loginName){ log.info("查看系统公告详情"); Notice notice = noticeService.selectNoticeById(noticeId); String key = "TO_READ" + notice.getNoticeId() + userId + loginName; if (!redisCache.hasKey(key)){ redisCache.setCacheObject(key,"0"); } return AjaxResult.success(notice); }
标签:notice,String,未读,demo,Notice,已读,key From: https://www.cnblogs.com/person008/p/16744778.html