前后端分离项目?二手交易平台小程序
SpringBoot—-物品收藏功能实现
SpringBoot—-评论回复功能实现(数据库设计)
SpringBoot—-文件(图片)上传与显示(下载)
点赞
这个功能耗费了我挺多时间,简单实现很简单,就++ ? .但是还是感觉这种点赞是一个高频率的请求,而且搜的时候我看都是使用redis做缓存。b站也搜到一个视频来着,也是一样的。
效果:
MySQL使用了一张表和另外几张表的一个字段,一张存放点赞信息,就是谁点赞了谁在啥时候。字段存放点赞数量。就是物品信息表。评论表这些。
–
redis,使用的是hash数据结构,redis_liked存放点赞数据,redis_liked_count存放点赞数量数据。
解释 :
对于 “1::字符串::1 ”
这个是一种存放方式,前面1为objid就是被点赞物品或者评论id,字符串为微信openid每个用户唯一id,后面1为类型区分点赞的是物品还是主评论,子评论。
对于 "\"0\""
这个数据则是点赞的状态,1为点赞0为取消点赞对于"1::1"
这个前面1是objid就是物品或者子、主评论id,后面则是区别是哪个类型。“0”
就是点赞数量。
后台代码:
前端就发like或者取消unlike请求
package com.w.wx.controller; import com.w.wx.domain.Msg; import com.w.wx.service.impls.RedisServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController @RequestMapping("/wx/liked") public class LikedContro { @Autowired private RedisServiceImpl redisService; /** * 保存点赞数据到redis * 点赞数+1 * 同一个不能点赞两次 * @param objId * @param openid * @param type * @return */ @RequestMapping("like") public Msg saveLikedToRedis(<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码网^</strong>Integer objId,String openid , String type){ redisService.incrementLikedCount(objId, type, openid); redisService.saveLikedToRedis(objId,openid,type); int oneInfoCount = redisService.getOneInfoCount(objId, type); return Msg.success().add("count",oneInfoCount); } @RequestMapping("unlike") public Msg decrementLikedCount(Integer objId,String openid,String type){ redisService.decrementLikedCount(objId,type,openid); redisService.unlikeFromRedis(objId,openid,type); int oneInfoCount = redisService.getOneInfoCount(objId, type); return Msg.success().add("count",oneInfoCount); } //恢复redis @RequestMapping("restore") public Msg restoreRedisCountInfo(){ redisService.savaInfoFromDb2Re(0); redisService.savaInfoFromDb2Re(1); redisService.savaInfoFromDb2Re(2); return Msg.success(); } }