当前位置: 移动技术网 > IT编程>开发语言>Java > springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作

springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作

2020年09月19日  | 移动技术网IT编程  | 我要评论
springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作前言第一次写博客,记录一下:最近做了一个帖子的收藏、点赞数量的功能,其实之前也做过类似的功能,因为之前一直使用的mysql

springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作

前言

第一次写博客,记录一下:

在这里插入图片描述

最近做了一个帖子的收藏、点赞数量的功能,其实之前也做过类似的功能,因为之前一直使用的mysql 总是感觉对于这种频繁需要改变的值,不应该给予mysql过大的压力,本文章采用的是redis 做了持久化。下面贴出关键代码:dataresponse是项目中使用的结果封装实体类;forumdto是此功能的参数实体,如果有需要请留言。

常量如下:

 private static final string default_value = "0:0:0:0:0:0";
  public static final byte byte_zero = 0;
  public static final byte byte_one = 1;
  public static final byte byte_two = 2;
  public static final byte byte_three = 3;
  public static final byte byte_four = 4;
  public static final byte byte_five = 5;
  public static final byte byte_six = 6;
 @override
  public dataresponse keepnum(forumdto forumdto) {
    //将帖子id 设置为 key
    string key = forumdto.getpostid().tostring();
    //get 用户id
    string userid = forumdto.getuserid();
    string count, newcount;
    //绑定数据集key
    boundhashoperations<string, object, object> post = redistemplate.boundhashops("post:");
    //获取hkey
    // count: 0论坛-点赞量 1评论量 2收藏量 3浏览 4评论-点赞量
    if (null == post.get(key)) {
      //无则set
      post.put(key, default_value);
      //再取出来赋值给 count
      count = post.get(key).tostring();
    } else {
      //有直接赋值 count
      count = post.get(key).tostring();
    }
    // operationtype 1 浏览 2 帖子点赞 3 收藏 4评论-点赞
    string prefix;
    switch (forumdto.getoperationtype()) {
      case 1:
        //记录浏览次数 operationtype 1 : 记录浏览次数
        newcount = resetvalue(count, byte_three, true);
        post.put(key, newcount);
        break;
      case 2:
        //记录帖子-点赞
        prefix = "thumbs:post";
        switch (forumdto.getclicktype()) {
          case 0:
            /**
             * operationtype 2: + clicktype 0 = 给帖子点赞
             * 0点赞
             * 从redis中获取数量 帖子d 例如:177488r88t78r78r7
             * count: 0论坛-点赞量 1评论量 2收藏量 3浏览 4评论-点赞量
             * 避免每种数量都去查询redis 直接通过 redis value 记录所有的数量
             * 获取加 +1 后的值
             */
            if (redistemplate.opsforset().ismember(prefix + ":" + key, prefix + ":" + userid)) {
              return dataresponse.fail("不能重复点赞哦");
            } else {
              redistemplate.opsforset().add(prefix + ":" + key, prefix + ":" + userid);
            }
            newcount = resetvalue(count, byte_zero, true);
            //set to redis
            post.put(key, newcount);
            break;
          case 1:
            //operationtype 2: + clicktype 1 = 取消帖子点赞
            //1取消帖子点赞
            if (!redistemplate.opsforset().ismember(prefix + ":" + key, prefix + ":" + userid)) {
              //重复处理
              return dataresponse.fail("不能重复取消哦");
            } else {
              //删除
              redistemplate.opsforset().remove(prefix + ":" + key, prefix + ":" + userid);
            }
            newcount = resetvalue(count, byte_zero, false);
            post.put(key, newcount);
            break;
        }
        break;
      case 3:
        prefix = "collection:post";
        list<mqmessage> sendlist = new linkedlist<>();
        mqmessage mqmessage = new mqmessage();
        switch (forumdto.getclicktype()) {
          //operationtype 3 + clicktype 0 = 记录收藏
          case 0:
            //数量+1
            //根据用户id + 帖子id 查询redis 数据
            if (redistemplate.opsforset().ismember(prefix + ":" + key, prefix + ":" + userid)) {
              //重复处理
              return dataresponse.fail("不能重复收藏哦");
            }
            //add
            redistemplate.opsforset().add(prefix + ":" + key, prefix + ":" + userid);
            //set to redis
            newcount = resetvalue(count, byte_two, true);
            post.put(key, newcount);
            mqmessage.settype(new byte("9"));
            mqmessage.setsenderid(userid);
            mqmessage.setpostid(forumdto.getpostid());
            sendlist.add(mqmessage);
            this.sendmq.send(sendlist);
            break;
          //operationtype 3 + clicktype 1 = 取消收藏
          case 1:
            //取消收藏
            //尝试从redis取出当前用户是否已经收藏
            if (!redistemplate.opsforset().ismember(prefix + ":" + key, prefix + ":" + userid)) {
              //重复处理
              return dataresponse.fail("不能重复取消哦");
            }
            //删除
            redistemplate.opsforset().remove(prefix + ":" + key, prefix + ":" + userid);
            newcount = resetvalue(count, byte_two, false);
            post.put(key, newcount);
            mqmessage.settype(new byte("10"));
            mqmessage.setsenderid(userid);
            mqmessage.setpostid(forumdto.getpostid());
            sendlist.add(mqmessage);
            this.sendmq.send(sendlist);
            break;
        }
        break;
      case 4:
        //记录评论-点赞
        // operationtype 4: + clicktype 0 = 给评论点赞
        if (null == forumdto.getcommentid()) {
          return dataresponse.fail("评论id不能为空");
        }
        string commentnum, ckey = forumdto.getcommentid().tostring();
        boundhashoperations<string, object, object> comment = redistemplate.boundhashops("post:comment");
        if (null == comment.get(ckey)) {
          //无则set
          comment.put(ckey, "0");
          //再取出来赋值给 count
          commentnum = comment.get(ckey).tostring();
        } else {
          //有直接赋值 count
          commentnum = comment.get(ckey).tostring();
        }
        //赞评论
        prefix = "thumbs:comment";
        switch (forumdto.getclicktype()) {
          case 0:
            /**
             * 0点赞
             * 从redis中获取数量 帖子d 例如:177488r88t78r78r7
             * count: 0论坛-点赞量 1评论量 2收藏量 3浏览 4评论-点赞量
             * 避免每种数量都去查询redis 直接通过 redis value 记录所有的数量
             * 获取加 + 后的值
             */
            if (redistemplate.opsforset().ismember(prefix + ":" + ckey, prefix + ":" + userid)) {
              return dataresponse.fail("不能重复点赞哦");
            } else {
              redistemplate.opsforset().add(prefix + ":" + ckey, prefix + ":" + userid);
            }
            //set to redis
            comment.put(ckey, cresetvalue(commentnum, true));
            break;
          case 1:
            //1取消评论点赞
            if (!redistemplate.opsforset().ismember(prefix + ":" + ckey, prefix + ":" + userid)) {
              //重复处理
              return dataresponse.fail("不能重复取消哦");
            } else {
              //删除
              redistemplate.opsforset().remove(prefix + ":" + ckey, prefix + ":" + userid);
            }
            newcount = cresetvalue(commentnum, false);
            comment.put(ckey, newcount);
            break;
        }
        break;
      default:
        dataresponse.fail(responseenum.failed);
    }
    return dataresponse.success(responseenum.success);
  }

 

resetvalue代码:

 /**
   * 功能描述: <br>
   * 〈点赞数、收藏数等数量重置〉
   * @param val  数组
   * @param type  0帖子点赞量 1评论量 2收藏量 3浏览 4评论点赞量
   * @param isplus 是否增加数量 true +  false -
   * @return: java.lang.string
   * @author:王震
   * @date: 2020/8/5 10:27
   * stringutils包:import org.apache.commons.lang3.stringutils;
   * 可以使用jdk的包替代split方法;但jdk的包需要验证正则,效率较低。
   */
  private string resetvalue(string val, int j, boolean isplus) {
    string[] value = stringutils.split(val, ":");
    long temp = long.valueof(value[j]);
    stringbuffer sb = new stringbuffer(16);
    if (isplus) {
      temp += 1;
    } else {
      temp -= 1;
    }
    value[j] = temp.tostring();
    for (int i = 0, len = value.length; i < len; i++) {
      if (i != len - 1) {
        sb.append(value[i]).append(":");
      }else {
        sb.append(value[i]);
      }
    }
    return sb.tostring();
  }

总结

到此这篇关于springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作的文章就介绍到这了,更多相关springboot +redis实现点赞收藏评论内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

您可能感兴趣的文章:

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网