5.点赞功能 Redis
创始人
2024-05-25 10:22:32
0
  1. Redis

(1)简介

  • Redis 是一个高性能的 key-value 数据库

  • 原子 – Redis的所有操作都是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。

  • 非关系形数据库

  • 数据全部存在内存中,性能高。

(2)数据类型

Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。

  • string 是 redis 最基本的类型,你可以理解成与 Memcached 一模一样的类型,一个 key 对应一个 value。

  • Redis hash 是一个键值(key=>value)对集合。Redis hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象。

  • Redis 列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。

  • Redis 的 Set 是 string 类型的无序集合,集合是通过hash实现的

  • Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。

(3)基本操作

@Test
public void testStrings() {String redisKey = "test:count";redisTemplate.opsForValue().set(redisKey, 1);System.out.println(redisTemplate.opsForValue().get(redisKey));System.out.println(redisTemplate.opsForValue().increment(redisKey));System.out.println(redisTemplate.opsForValue().decrement(redisKey));
}@Test
public void testHashes() {String redisKey = "test:user";redisTemplate.opsForHash().put(redisKey, "id", 1);redisTemplate.opsForHash().put(redisKey, "username", "zhangsan");System.out.println(redisTemplate.opsForHash().get(redisKey, "id"));System.out.println(redisTemplate.opsForHash().get(redisKey, "username"));
}@Test
public void testLists() {String redisKey = "test:ids";redisTemplate.opsForList().leftPush(redisKey, 101);redisTemplate.opsForList().leftPush(redisKey, 102);redisTemplate.opsForList().leftPush(redisKey, 103);System.out.println(redisTemplate.opsForList().size(redisKey));System.out.println(redisTemplate.opsForList().index(redisKey, 0));System.out.println(redisTemplate.opsForList().range(redisKey, 0, 2));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));
}@Test
public void testSets() {String redisKey = "test:teachers";redisTemplate.opsForSet().add(redisKey, "刘备", "关羽", "张飞", "赵云", "诸葛亮");System.out.println(redisTemplate.opsForSet().size(redisKey));System.out.println(redisTemplate.opsForSet().pop(redisKey));System.out.println(redisTemplate.opsForSet().members(redisKey));
}@Test
public void testSortedSets() {String redisKey = "test:students";redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);redisTemplate.opsForZSet().add(redisKey, "悟空", 90);redisTemplate.opsForZSet().add(redisKey, "八戒", 50);redisTemplate.opsForZSet().add(redisKey, "沙僧", 70);redisTemplate.opsForZSet().add(redisKey, "白龙马", 60);System.out.println(redisTemplate.opsForZSet().zCard(redisKey));System.out.println(redisTemplate.opsForZSet().score(redisKey, "八戒"));System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "八戒"));System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey, 0, 2));
}多次访问同一个key
@Test
public void testBoundOperations() {String redisKey = "test:count";BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);operations.increment();operations.increment();operations.increment();operations.increment();operations.increment();System.out.println(operations.get());
}

(4)spring 配置 redis

引入依赖

org.springframework.bootspring-boot-starter-data-redis

在 application.properties 中声明:访问哪个库,host地址,端口号

# RedisProperties
spring.redis.database=11
spring.redis.host=localhost
spring.redis.port=6379

在 config 下实现 RedisConfig 类

注入连接工厂才能访问数据库 RedisConnectionFactory factory

实例化 bean new RedisTemplate<>();

设置工厂后有访问数据库能力 template.setConnectionFactory(factory);

指定序列化方式(数据转化方式)

//定义自定义的redis对象@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory factory){RedisTemplate redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(factory);//主要配置 序列化的方式//设置key 的 序列化方式redisTemplate.setKeySerializer(RedisSerializer.string());//设置value的序列化方式redisTemplate.setValueSerializer(RedisSerializer.json());//设置hash 的 key序列化redisTemplate.setHashKeySerializer(RedisSerializer.string());//设置 hash 的 value 序列化redisTemplate.setHashValueSerializer(RedisSerializer.json());//出发 使其生效redisTemplate.afterPropertiesSet();return  redisTemplate;}

(5)Redis 事务 管理

事务内命令不会立即执行,提交后统一执行

使用编程式事务进行管理,声明式事务用的少

调用 redisTemplate ,方法内部做匿名实现

SessionCallback() 里方法execute重写,内部实现事务逻辑

启用事务 operations.multi();

提交事务 operations.exec();

// 编程式事务
@Test
public void testTransactional() {Object obj = redisTemplate.execute(new SessionCallback() {@Overridepublic Object execute(RedisOperations operations) throws DataAccessException {String redisKey = "test:tx";operations.multi();operations.opsForSet().add(redisKey, "zhangsan");operations.opsForSet().add(redisKey, "lisi");operations.opsForSet().add(redisKey, "wangwu");System.out.println(operations.opsForSet().members(redisKey));return operations.exec();}});System.out.println(obj);
}

2.点赞

(1)业务层

生成redis key的工具 在 util 下实现 RedisKeyUtil,集合set存储谁给某个实体点的赞

public class RedisKeyUtil {private static final String SPLIT = ":";private static final String PREFIX_ENTITY_LIKE = "like:entity";private static final String PREFIX_USER_LIKE = "like:user";// 某个实体的赞// like:entity:entityType:entityId -> set(userId)public static String getEntityLikeKey(int entityType, int entityId) { //实体类型  实体IDreturn PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;}}

Service 下实现 LikeService

@Service
public class LikeService {@Autowiredprivate RedisTemplate redisTemplate;// 点赞public void like(int userId, int entityType, int entityId) {//获取keyString entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType,entityId);//判断当前用户是否点过赞   即userid 是否在set中if(redisTemplate.opsForSet().isMember(entityLikeKey,userId)){redisTemplate.opsForSet().remove(entityLikeKey,userId);}else {redisTemplate.opsForSet().add(entityLikeKey,userId);}}// 查询某实体点赞的数量public long findEntityLikeCount(int entityType, int entityId){String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType,entityId);return redisTemplate.opsForSet().size(entityLikeKey);}// 查询某人对某实体的点赞状态public int findEntityLikeStatus(int userId, int entityType, int entityId) {String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType,entityId);return redisTemplate.opsForSet().isMember(entityLikeKey,userId)? 1:0 ;}
}

(2)表现层

Controller 下实现 LikeController

  1. 获取当前用户

  1. 调用service点赞方法

  1. 获取数量和状态

  1. 放入map

  1. 返回json格式数据

@Controller
public class LikeController {@Autowiredprivate LikeService likeService;@Autowiredprivate HostHolder hostHolder;@RequestMapping(path = "/like", method = RequestMethod.POST)@ResponseBodypublic String like(int entityType, int entityId){User user = hostHolder.getUser();//点赞likeService.like(user.getId(), entityType,entityId);//更新点赞数量long likeCount = likeService.findEntityLikeCount(entityType,entityId);//查询状态int likeStatus = likeService.findEntityLikeStatus(user.getId(),entityType,entityId);Map map = new HashMap<>();map.put("likeCount", likeCount);map.put("likeStatus", likeStatus);return CommunityUtil.getJSONString(0, null, map);}
}

帖子详情页面赞的数量的显示

修改 DiscussPostController 下的 getDiscussPost

//根据 帖子id 查询帖子内容 评论 评论的回复@RequestMapping(path = "/detail/{discussPostId}",method = RequestMethod.GET)public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page){//根据帖子id查询帖子DiscussPost post = discussPostService.findDiscussPostById(discussPostId);model.addAttribute("post",post);//根据userid查询userUser user =userService.findUserById(post.getUserId());model.addAttribute("user",user);// 点赞数量long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, discussPostId);model.addAttribute("likeCount", likeCount);// 点赞状态int likeStatus = hostHolder.getUser() == null ? 0 :likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_POST, discussPostId);model.addAttribute("likeStatus", likeStatus);//查评论的分页信息page.setLimit(5);page.setPath("/discuss/detail/" + discussPostId);page.setRows(post.getCommentCount());//评论:给帖子的评论//回复:给评论的评论//获取所有评论List commentList = commentService.findCommentsByEntity(ENTITY_TYPE_POST,post.getId(), page.getOffset(),page.getLimit());//用于封装 每条评论及每条评论的回复。。。List> commentVoList = new ArrayList<>();//每一条评论 找到评论的作者。找到该评论的回复,回复的作者,回复的用户for (Comment comment:commentList) {Map commentVo = new HashMap<>();//存入评论内容commentVo.put("comment",comment);//放入 作者commentVo.put("user",userService.findUserById(comment.getUserId()));// 点赞数量likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, comment.getId());commentVo.put("likeCount", likeCount);// 点赞状态likeStatus = hostHolder.getUser() == null ? 0 :likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, comment.getId());commentVo.put("likeStatus", likeStatus);//获取该评论的所有回复List replyList = commentService.findCommentsByEntity(ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);//用于封装 每一条回复的 作者 回复咪表List> replyVoList = new ArrayList<>();if(replyVoList != null){for (Comment reply: replyList) {Map replyVo = new HashMap<>();//回复replyVo.put("reply", reply);// 放入 回复的作者replyVo.put("user", userService.findUserById(reply.getUserId()));//回复目标User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getTargetId());replyVo.put("target", target);// 点赞数量likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, reply.getId());replyVo.put("likeCount", likeCount);// 点赞状态likeStatus = hostHolder.getUser() == null ? 0 :likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, reply.getId());replyVo.put("likeStatus", likeStatus);//将 单条回复放入 此 评论 总的 回复表replyVoList.add(replyVo);}}//将回复总表 嵌入 单条评论commentVo.put("replys", replyVoList);//回复数量int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId());commentVo.put("replyCount", replyCount);commentVoList.add(commentVo);}model.addAttribute("comments", commentVoList);return "/site/discuss-detail";}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...