/** 朋友圈 喜歡功能接口* *//** * 喜歡 * * @param userId * @param publishId * @return */Boolean loveComment(Long userId, String publishId);/** * 取消喜歡 * * @param userId * @param publishId * @return */Boolean disLoveComment(Long userId, String publishId);/** * 查詢喜歡數(shù) * * @param publishId * @return */Long queryLoveCount(String publishId);/** * 查詢 戶是否喜歡該動(dòng)態(tài) * * @param userId * @param publishId * @return */Boolean queryUserIsLove(Long userId, String publishId);
朋友圈 喜歡功能接口 實(shí)現(xiàn)類
/** 朋友圈 喜歡功能* */@Overridepublic Boolean loveComment(Long userId, String publishId) { //查詢?cè)?戶是否已經(jīng)喜歡 if (this.queryUserIsLove(userId, publishId)) { return false; } //喜歡 boolean result = this.saveComment(userId, publishId, CommentType.LOVE, null); if (!result) { return false; } //喜歡成功后,修改Redis中的總的喜歡數(shù) String redisKey = this.getCommentRedisKeyPrefix(publishId); String hashKey = CommentType.LOVE.toString(); this.redisTemplate.opsForHash().increment(redisKey, hashKey, 1); //標(biāo)記 戶已經(jīng)喜歡 hashKey = this.getCommentUserLoveRedisKey(userId); this.redisTemplate.opsForHash().put(redisKey, hashKey, “1”); return true;}private String getCommentUserLoveRedisKey(Long userId) { return COMMENT_USER_LOVE_REDIS_KEY_PREFIX + userId;}@Overridepublic Boolean disLoveComment(Long userId, String publishId) { if (!this.queryUserIsLove(userId, publishId)) { //如果 戶沒(méi)有喜歡,就直接返回 return false; } boolean result = this.removeComment(userId, publishId, CommentType.LOVE); if (!result) { //刪除失敗 return false; } //刪除redis中的記錄 String redisKey = this.getCommentRedisKeyPrefix(publishId); String hashKey = this.getCommentUserLoveRedisKey(userId); this.redisTemplate.opsForHash().delete(redisKey, hashKey); this.redisTemplate.opsForHash().increment(redisKey, CommentType.LOVE.toString(), -1); return true;}@Overridepublic Long queryLoveCount(String publishId) { // 先從redis中命中,如果命中的話就返回,沒(méi)有命中就查詢Mongodb String redisKey = this.getCommentRedisKeyPrefix(publishId); String hashKey = CommentType.LOVE.toString(); Object value = this.redisTemplate.opsForHash().get(redisKey, hashKey); if (ObjectUtil.isNotEmpty(value)) { return Convert.toLong(value); } //查詢count Long count = this.queryCommentCount(publishId, CommentType.LOVE); //存儲(chǔ)到redis中 this.redisTemplate.opsForHash().put(redisKey, hashKey, String.valueOf(count)); return count;}@Overridepublic Boolean queryUserIsLove(Long userId, String publishId) { String redisKey = this.getCommentRedisKeyPrefix(publishId); String hashKey = this.getCommentUserLoveRedisKey(userId); Object value = this.redisTemplate.opsForHash().get(redisKey, hashKey); if (ObjectUtil.isNotEmpty(value)) { return StrUtil.equals(Convert.toStr(value), “1”); } //查詢mongodb Query query = Query.query(Criteria.where(“publishId”) .is(new ObjectId(publishId)) .and(“userId”).is(userId) .and(“commentType”).is(CommentType.LOVE.getType())); long count = this.mongoTemplate.count(query, Comment.class); if (count == 0) { return false; } //標(biāo)記 戶已經(jīng)喜歡 this.redisTemplate.opsForHash().put(redisKey, hashKey, “1”); return true;}