Redis点赞功能实现
为什么?
点赞功能的实现用MySQL也可以,可是为什么大家都用redis呢?
MySQL和redis的应用场景不同
从效率来说:
Redis的数据存放在内存,所以速度比较快
Mysql的数据存放在硬盘上,读写速度不比redis
redis主要做数据缓存,减少mysql数据库的压力,点赞过后要立即刷新显示在页面,所以推荐使用redis做点赞功能
延伸一下:redis的数据结构有List、Set、SortedSet、Hash、KV
List是双向链表,适用于最新列表,关注列表
Set适用于无顺序的集合,点赞点踩、抽奖、共同好友
SortedSet是优先队列,适用于排行榜
先在util中把redis的增删改查封装起来
@Service public class JedisAdapter implements InitializingBean { private static final Logger logger = LoggerFactory.getLogger(JedisAdapter.class); private JedisPool pool; @Override public void afterPropertiesSet() throws Exception { pool = new JedisPool("redis://localhost:6379/10"); } public long sadd(String key, String value) { Jedis jedis = null; try { jedis = pool.getResource(); return jedis.sadd(key, value); } catch (Exception e) { logger.error("发生异常" + e.getMessage()); } finally { if (jedis != null) { jedis.close(); } } return 0; } public long srem(String key, String value) { Jedis jedis = null; try { jedis = pool.getResource(); return jedis.srem(key, value); } catch (Exception e) { logger.error("发生异常" + e.getMessage()); } finally { if (jedis != null) { jedis.close(); } } return 0; } public long scard(String key) { Jedis jedis = null; try { jedis = pool.getResource(); return jedis.scard(key); } catch (Exception e) { logger.error("发生异常" + e.getMessage()); } finally { if (jedis != null) { jedis.close(); } } return 0; } public boolean sismember(String key, String value) { Jedis jedis = null; try { jedis = pool.getResource(); return jedis.sismember(key, value); } catch (Exception e) { logger.error("发生异常" + e.getMessage()); } finally { if (jedis != null) { jedis.close(); } } return false; } public List<String> brpop(int timeout, String key) { Jedis jedis = null; try { jedis = pool.getResource(); return jedis.brpop(timeout, key); } catch (Exception e) { logger.error("发生异常" + e.getMessage()); } finally { if (jedis != null) { jedis.close(); } } return null; } public long lpush(String key, String value) { Jedis jedis = null; try { jedis = pool.getResource(); return jedis.lpush(key, value); } catch (Exception e) { logger.error("发生异常" + e.getMessage()); } finally { if (jedis != null) { jedis.close(); } } return 0; } }
我们统一设定key的字段
喜欢为:LIKE:实体类型:实体ID
不喜欢为:DISLIKE:实体类型:实体ID
这样可以将喜欢一条评论的人存在一个集合,不喜欢的存在另一个集合,通过统计数量可以获得点赞和点踩数
public class RedisKeyUtil { private static String SPLIT = ":"; private static String BIZ_LIKE = "LIKE"; private static String BIZ_DISLIKE = "DISLIKE"; public static String getLikeKey(int entityType, int entityId) { return BIZ_LIKE + SPLIT + String.valueOf(entityType) + SPLIT + String.valueOf(entityId); } public static String getDisLikeKey(int entitype, int entityId) { return BIZ_DISLIKE + SPLIT + String.valueOf(entitype) + SPLIT + String.valueOf(entityId); } }
LikeService
这里面实现点赞、点踩、统计数量、获取喜欢状态的功能
点赞就是将通过评论Type和Id获取的likekey放到指定的集合中去
统计数量就是调用scard方法
获取喜欢的状态则是看当前的评论在当前用户下是否是集合中的成员
@Service public class LikeService { private static final Logger logger = LoggerFactory.getLogger(LikeService.class); @Autowired JedisAdapter jedisAdapter; public long getLikeCount(int entieyType, int entityId) { String likeKey = RedisKeyUtil.getLikeKey(entieyType, entityId); return jedisAdapter.scard(likeKey); } public long like(int userId, int entityType, int entityId) { String likeKey = RedisKeyUtil.getLikeKey(entityType, entityId); jedisAdapter.sadd(likeKey, String.valueOf(userId)); String dislikeKey = RedisKeyUtil.getDisLikeKey(entityType, entityId); jedisAdapter.srem(dislikeKey, String.valueOf(userId)); return jedisAdapter.scard(likeKey); } public long dislike(int userId, int entityType, int entityId) { String dislikeKey = RedisKeyUtil.getDisLikeKey(entityType, entityId); jedisAdapter.sadd(dislikeKey, String.valueOf(userId)); String likeKey = RedisKeyUtil.getLikeKey(entityType, entityId); jedisAdapter.srem(likeKey, String.valueOf(userId)); return jedisAdapter.scard(likeKey); } public int getLikeStatus(int userId, int entityType, int entityId) { String likeKey = RedisKeyUtil.getLikeKey(entityType, entityId); if (jedisAdapter.sismember(likeKey, String.valueOf(userId))) { return 1; } String disLikeKey = RedisKeyUtil.getDisLikeKey(entityType, entityId); return jedisAdapter.sismember(disLikeKey, String.valueOf(userId)) ? -1 : 0; } }
likeController
@Controller public class LikeController { @Autowired LikeService likeService; @Autowired HostHolder hostHolder; @Autowired CommentService commentService; @RequestMapping(path = {"/like"},method = {RequestMethod.POST}) @ResponseBody public String like(@RequestParam("commentId") int commentId) { if (hostHolder.getUser() == null) { return WendaUtil.getJSONString(999); } long likeCount = likeService.like(hostHolder.getUser().getId(), EntityType.ENTITY_COMMENT, commentId); return WendaUtil.getJSONString(0, String.valueOf(likeCount)); } @RequestMapping(path = {"/dislike"},method = {RequestMethod.POST}) @ResponseBody public String dislike(@RequestParam("commentId") int commentId) { if (hostHolder.getUser() == null) { return WendaUtil.getJSONString(999); } long likeCount = likeService.dislike(hostHolder.getUser().getId(), EntityType.ENTITY_COMMENT, commentId); return WendaUtil.getJSONString(0, String.valueOf(likeCount)); } }
在问题详情页面,将获取的喜欢数添加到里面