Redis(5)实用场景

优惠券秒杀:https://blog.csdn.net/weixin_43994244/article/details/127560350

用户点赞

需求:

  • 同一个用户只能给一篇文章点赞一次,再次点击则取消点赞
  • 如果当前用户已经点赞,则点赞按钮高亮显示(前端已实现,判断字段Blog类的isLike属性)

解决:
将不同的文章点赞的用户分别放入不同的set集合中,满足唯一性

用户点赞代码:

  /**
     * 用户点赞
     * 1.判断当前登录用户是否已经点赞
     * 2.未点赞,数据库点赞数+1,保存到redis的set集合
     * 3.已点赞,数据库点赞数-1,从redis的set集合中移除
     * @return
     */
    @Override
    public Result likeBlog(Long id) {
        //1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        //2.判断当前登录用户是否已经点赞
        String key = "blog:liked:"+id;
        Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, userId.toString());
        if(BooleanUtil.isFalse(isMember)){
            //3.如果未点赞,可以点赞
            //3.1数据库点赞数+1
            boolean isSuccess = update().setSql("liked = liked + 1").eq("id", id).update();
            //3.2保存用户到redis的set集合
            if(isSuccess){
                stringRedisTemplate.opsForSet().add(key,userId.toString());
            }

        }else{
            //4.已点赞,取消点赞
            //4.1数据库点赞数-1
            boolean isSuccess = update().setSql("liked = liked - 1").eq("id", id).update();
            //4.2用户从redis的set集合移除
            if(isSuccess){
                stringRedisTemplate.opsForSet().remove(key,userId.toString());
            }
        }
        return Result.ok();
    }

查看列表和详情时,需要在redis中查看是否点赞,给isLike赋值

public Result getBlogById(Long id) {
        //查询blog
        Blog blog = getById(id);
        if(blog == null){
            return Result.fail("博客不存在");
        }
        //查询blog用户
        queryBlogUser(blog);
        //查询blog是否点赞
        isBlogLiked(blog);
        return Result.ok(blog);
    }

  private void queryBlogUser(Blog blog) {
        Long userId = blog.getUserId();
        User user = userService.getById(userId);
        blog.setName(user.getNickName());
        blog.setIcon(user.getIcon());
    }

    private void isBlogLiked(Blog blog) {
        //1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        //2.判断当前登录用户是否已经点赞
        String key = "blog:liked:"+ blog.getId();
        Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, userId.toString());
        blog.setIsLike(BooleanUtil.isTrue(isMember));
    }

点赞排行榜

需求: 在探店笔记的详情页面,可以把给该笔记点赞的人显示出来,比如最早点赞的TOP5,形成点赞排行榜。
解决: 之前点赞放到set集合内,但是set集合不能排序,所以将set集合改为可排序的sortedSet集合。
在这里插入图片描述
修改点赞代码:
1.点赞列表改为sortedSet
2.用户点赞通过获取score判断

 /**
     * 用户点赞
     * @param id
     * @return
     */
    @Override
    public Result likeBlog(Long id) {
        //1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        //2.判断当前登录用户是否已经点赞,用score判断是否存在
        String key = "blog:liked:"+id;
        Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
        if(score == null){
            //3.如果未点赞,可以点赞
            //3.1数据库点赞数+1
            boolean isSuccess = update().setSql("liked = liked + 1").eq("id", id).update();
            //3.2保存用户到redis的sortedSet集合  zadd key value score
            if(isSuccess){
                stringRedisTemplate.opsForZSet().add(key,userId.toString(),System.currentTimeMillis());
            }

        }else{
            //4.已点赞,取消点赞
            //4.1数据库点赞数-1
            boolean isSuccess = update().setSql("liked = liked - 1").eq("id", id).update();
            //4.2用户从redis的set集合移除
            if(isSuccess){
                stringRedisTemplate.opsForZSet().remove(key,userId.toString());
            }
        }
        return Result.ok();
    }

添加用户未登录不需要查询是否点赞判断:

    private void isBlogLiked(Blog blog) {
        //1.获取登录用户
        UserDTO user = UserHolder.getUser();
        //如果用户未登录,不需要查询是否点赞
        if(user == null){
            return;
        }
        Long userId = user.getId();
        //2.判断当前登录用户是否已经点赞
        String key = "blog:liked:"+ blog.getId();
        Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
        blog.setIsLike(score != null);
    }

点赞排行榜代码:

   //返回点赞前5名
    @Override
    public Result likesBlog(Long id) {
        String key = RedisConstants.BLOG_LIKED +id;
        //1.查询前5个点赞用户
        Set<String> ids = stringRedisTemplate.opsForZSet().range(key, 0, 4);
        if(ids == null || ids.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        //解析其中的Id
        List<Long> userIds = ids.stream().map(Long::valueOf).collect(Collectors.toList());
        //根据用户Id解析为用户信息
        //WHERE id IN (5,1) ORDER BY FIELD(id,5,1),查询结果顺序根据传入id的顺序
        //id拼接字符串
        String idStr  = StrUtil.join(",", ids);
        List<UserDTO> userDTOS = userService.query().in("id",ids).last("ORDER BY FIELD(id,"+idStr+")").list()
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOS);
    }

好友关注

关注和取消关注

针对用户的操作:可以对用户进行关注和取消关注功能。
关注是User之间的关系,是博主与粉丝的关系,数据库中有一张tb_follow表来标示:包含主键,用户id,关联的用户id,创建时间。

需求: 基于该表数据结构,实现两个接口:

  • 关注和取关接口,需要存放到set集合中(实现共同关注)
  • 判断是否关注的接口,用于页面点亮图标展示

在这里插入图片描述
关注/取消关注代码:

@Override
public Result follow(Long followUserId, Boolean isFollow) {
    // 1.获取登录用户
    Long userId = UserHolder.getUser().getId();
    String key = "follows:" + userId;
    // 1.判断到底是关注还是取关
    if (isFollow) {
        // 2.关注,新增数据
        Follow follow = new Follow();
        follow.setUserId(userId);
        follow.setFollowUserId(followUserId);
        boolean isSuccess = save(follow);
        if (isSuccess) {
            // 把关注用户的id,放入redis的set集合 sadd userId followerUserId
            stringRedisTemplate.opsForSet().add(key, followUserId.toString());
        }
    } else {
        // 3.取关,删除 delete from tb_follow where user_id = ? and follow_user_id = ?
        boolean isSuccess = remove(new QueryWrapper<Follow>()
                .eq("user_id", userId).eq("follow_user_id", followUserId));
        if (isSuccess) {
            // 把关注用户的id从Redis集合中移除
            stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
        }
    }
    return Result.ok();
}

查询是否关注代码:

 @Override
    public Result isFollow(Long id) {
        Long userId = UserHolder.getUser().getId();
        //查询是否关注
        Integer count = query().eq("user_id", userId).eq("follow_user_id", id).count();
        return Result.ok(count>0);
    }

共同关注

需求: 在博主个人页面展示出当前用户与博主的共同关注,查看登录用户和目标用户的共同关注好友。
解决: 可以把两人的关注的人分别放入到一个set集合中,然后再通过api去查看这两个set集合中的交集数据,实现共同关注功能。

查询共同关注代码:

@Override
public Result followCommons(Long id) {
    // 1.获取当前用户
    Long userId = UserHolder.getUser().getId();
    String key = "follows:" + userId;
    // 2.求交集
    String key2 = "follows:" + id;
    Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key, key2);
    if (intersect == null || intersect.isEmpty()) {
        // 无交集
        return Result.ok(Collections.emptyList());
    }
    // 3.解析id集合
    List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
    // 4.查询用户
    List<UserDTO> users = userService.listByIds(ids)
            .stream()
            .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
            .collect(Collectors.toList());
    return Result.ok(users);
}

Feed流

当关注了用户后,这个用户发了动态,应该把这些数据推送给用户,这个需求又把他叫做Feed流,关注推送也叫做Feed流。
Feed流直译为投喂,为用户持续的提供“沉浸式”的体验,通过无限下拉刷新获取新的信息。

传统的模式的内容解锁:需要用户通过搜索引擎或者是其他方式去解锁想要看的内容
在这里插入图片描述
新型的Feed流的的效果:不需要用户再去推送信息,而是系统分析用户到底想要什么,然后直接把内容推送给用户,从而使用户能够更加的节约时间,不用主动去寻找。
在这里插入图片描述
Feed流产品有两种常见模式:
Timeline:不做内容筛选,简单的按照内容发布时间排序,常用于好友或关注。例如朋友圈。

  • 优点:信息全面,不会有缺失。并且实现也相对简单
  • 缺点:信息噪音较多,用户不一定感兴趣,内容获取效率低

智能排序:利用智能算法屏蔽掉违规的、用户不感兴趣的内容。推送用户感兴趣信息来吸引用户。

  • 优点:投喂用户感兴趣信息,用户粘度很高,容易沉迷
  • 缺点:如果算法不精准,可能起到反作用

本例中的个人页面,是基于关注的好友来做Feed流,因此采用Timeline的模式。该模式的实现方案有三种:

  1. 拉模式也叫做读扩散,当张三和李四和王五发了消息后,都会保存在自己的邮箱中,假设赵六要读取信息,那么他会从读取他自己的收件箱,此时系统会从他关注的人群中,把他关注人的信息全部都进行拉取,然后在进行排序。
    在这里插入图片描述
    **优点:**比较节约空间,因为赵六在读信息时,并没有重复读取,而且读取完之后可以把他的收件箱进行清除。
    **缺点:**比较延迟,当用户读取数据时才去关注的人里边去读取数据,假设用户关注了大量的用户,此时就会拉取海量的内容,对服务器压力巨大。
  2. 推模式也叫做写扩散,没有写邮箱的,当张三写了一个内容,此时会主动的把张三写的内容发送到他的粉丝收件箱中去,假设此时李四再来读取,就不用再去临时拉取了。
    在这里插入图片描述
    **优点:**时效快,不用临时拉取
    **缺点:**内存压力大,假设一个大V写信息,很多人关注他, 就会写很多分数据到粉丝那边去
  3. 推拉结合也叫做读写混合,兼具推和拉两种模式的优点
    推拉模式是一个折中的方案,站在发件人这端,如果是个普通的人,采用写扩散的方式,直接把数据写入到他的粉丝中去,因为普通的人他的粉丝关注量比较小,所以这样做没有压力;如果是大V,直接将数据先写入到一份到发件箱里边去,然后再直接写一份到活跃粉丝收件箱里边去。
    站在收件人这端来看,如果是活跃粉丝,那么大V和普通的人发的都会直接写入到自己收件箱里边来,而如果是普通的粉丝,由于他们上线不是很频繁,所以等他们上线时,再从发件箱里边去拉信息。
    在这里插入图片描述

推送到粉丝邮箱

需求:

  • 修改新增探店笔记的业务,在保存blog到数据库的同时,推送到粉丝的收件箱
  • 收件箱满足可以根据时间戳排序,必须用Redis的数据结构实现(sortedSet)
  • 查询收件箱数据时,可以实现分页查询

Feed流中的数据会不断更新,所以数据的角标也在变化,因此不能采用传统的分页模式。
如果用list结构查询数据,只能按照脚标或者首尾。sortedSet会根据score值排序查询,这种查询方式与list脚标查询类似,但是还可以根据score值(时间戳)的范围来查询,时间戳从大到小排列,每次查询时记住最小的时间戳,下次查询找比这个时间戳更小的。

1.保存完探店笔记到数据库,获得到当前笔记的粉丝,然后把数据推送到粉丝的redis中

 @Override
    public Result saveBlog(Blog blog) {
        //1.保存博文到数据库
        UserDTO user = UserHolder.getUser();
        blog.setUserId(user.getId());
        boolean isSuccess = save(blog);
        if(!isSuccess){
            return Result.fail("新增博文失败!");
        }
        //2.查询笔记作者的所有粉丝 select * from tb_follow where follow_user_id =?
        List<Follow> follows = followService.query().eq("follow_user_id", user.getId()).list();
        //3.推送笔记id给所有粉丝
        for (Follow follow : follows) {
            //获取粉丝id
            Long userId = follow.getUserId();
            //推送消息到粉丝的收件箱
            String key = "feed:"+userId;
            stringRedisTemplate.opsForZSet().add(key,blog.getId().toString(),System.currentTimeMillis());
        }
        //返回id
        return Result.ok(blog.getId());
    }

2.查询并展示推送的Blog信息
2.1 每次查询完成后,要解析出查询出数据的最小时间戳,作为下一次查询的条件
2.2 找到与上一次查询相同的查询个数作为偏移量,下次查询时,跳过这些查询过的数据,拿到需要的数据
所以方法的请求参数有:上一次查询的最小时间戳和偏移量,两个参数第一次会由前端来指定,以后的查询就根据后台结果作为条件,再次传递到后台。
在这里插入图片描述返回值实体类:

@Data
public class ScrollResult {
    private List<?> list;
    private Long minTime;
    private Integer offset;
}

分页查收邮箱

   @Override
    public Result queryBlogOfFollow(Long max, Integer offset) {
        //1.查询当前用户的收件箱
        Long userId = UserHolder.getUser().getId();
        String key = "feed:"+userId;
        //ZREVRANGEBYSCORE key max min  WITHSCORES LIMIT offset count
        Set<ZSetOperations.TypedTuple<String>> typedTuples = stringRedisTemplate.opsForZSet()
                .reverseRangeByScoreWithScores(key, 0, max, offset, 2);
        if(typedTuples == null || typedTuples.isEmpty()){
            return Result.ok();
        }
        //2.解析收件箱,blogId,score(时间戳),返回minTime(最小值),offset(上次最小值一样的元素个数)
        ArrayList<Long> ids = new ArrayList<>(typedTuples.size());

        long minTime = 0;
        int os = 1;
        for (ZSetOperations.TypedTuple<String> typedTuple : typedTuples) {
            //获取id
            ids.add(Long.valueOf(Objects.requireNonNull(typedTuple.getValue())));

            long time = Objects.requireNonNull(typedTuple.getScore()).longValue();
            //计算偏移量
            if(time == minTime){
                os++;
            }else{
                //获取最小时间戳
                minTime = time;
                os = 1;
            }
          if(os > 2){os = 2;}
        }
        String idStr = StrUtil.join(",",ids);
        //4.根据id查询博文,必须有序
        List<Blog> blogs = blogService.query().in("id",ids).last("ORDER BY FIELD(id,"+idStr+")" ).list();
        for (Blog blog : blogs) {
            //查询blog用户
            queryBlogUser(blog);
            //查询blog是否点赞
            isBlogLiked(blog);
        }
        //5.封装结果并返回
        ScrollResult result = new ScrollResult();
        result.setList(blogs);
        result.setOffset(os);
        result.setMinTime(minTime);
        return Result.ok(result);
    }

附近商户

GEO数据结构

GEO就是Geolocation的简写形式,代表地理坐标。Redis在3.2版本中加入了对GEO的支持,允许存储地理坐标信息,根据经纬度来检索数据。
常见命令:

  • GEOADD:添加一个地理空间信息,包含:经度(longitude)、纬度(latitude)、值(member)

在这里插入图片描述
redis中存储的数据结构:
在这里插入图片描述

  • GEODIST:计算指定的两个点之间的距离并返回

在这里插入图片描述

  • GEOHASH:将指定member的坐标转为hash字符串形式并返回
  • GEOPOS:返回指定member的坐标

在这里插入图片描述

  • GEORADIUS:指定圆心、半径,找到该圆内包含的所有member,并按照与圆心之间的距离排序后返回。6.以后已废弃
  • GEOSEARCH:在指定范围内搜索member,并按照与指定点之间的距离排序后返回。范围可以是圆形或矩形。6.2.新功能

在这里插入图片描述

  • GEOSEARCHSTORE:与GEOSEARCH功能一致,不过可以把结果存储到一个指定的key。 6.2.新功能

点击美食之后,商家按照距离,人气,评分等排序方式展现出来,按照距离排序就需要使用GEO,需要向后台传入当前app的经纬度(此处是写死的) ,以当前坐标作为圆心,同时绑定相同的店家类型type和分页信息,后台查询出对应的数据再返回。
在这里插入图片描述
在GEO结构中只需要存储x轴,y轴和商铺id即可,毕竟redis是一个内存级数据库,如果存海量数据,redis还是力不从心。
redis中并没有存储t商铺ype,所以我们无法根据type来对数据进行筛选,所以可以按照商铺类型做分组,类型相同的商铺作为同一组,以typeId为key存入同一个GEO集合中即可。
在这里插入图片描述
将数据库中的店铺坐标信息存放到redis中:

@Test
    void loadShopData() {
        // 1.查询店铺信息
        List<Shop> list = shopService.list();
        // 2.把店铺分组,按照typeId分组,typeId一致的放到一个集合
        Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));
        // 3.分批完成写入Redis
        for (Map.Entry<Long, List<Shop>> entry : map.entrySet()) {
            // 3.1.获取类型id
            Long typeId = entry.getKey();
            String key = "shop:geo:" + typeId;
            // 3.2.获取同类型的店铺的集合
            List<Shop> value = entry.getValue();
            List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(value.size());
            // 3.3.写入redis GEOADD key 经度 纬度 member
            for (Shop shop : value) {
                //每次都发送一个请求
                // stringRedisTemplate.opsForGeo().add(key, new Point(shop.getX(), shop.getY()), shop.getId().toString());
                locations.add(new RedisGeoCommands.GeoLocation<>(
                        shop.getId().toString(),
                        new Point(shop.getX(), shop.getY())
                ));
            }
            stringRedisTemplate.opsForGeo().add(key, locations);
        }
    }

实现附近商户功能:

  /**
     * 根据商铺类型分页查询商铺信息
     * @param typeId 商铺类型
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/of/type")
    public Result queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "x", required = false) Double x,
            @RequestParam(value = "y", required = false) Double y
    ) {
        return shopService.queryShopByType(typeId, current, x, y);
  }


@Override
public Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {
        //1.判断是否需要根据坐标查询
        if(x == null || y == null){
            //不需要坐标查询,按照数据库查询
            Page<Shop> page = query().eq("type_id", typeId)
                    .page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));
            return Result.ok(page.getRecords());
        }
        //2.计算分页参数
        int from = (current -1) * SystemConstants.DEFAULT_PAGE_SIZE;
        int end = current * SystemConstants.DEFAULT_PAGE_SIZE;
        //3.查询redis,按照距离排序,分页,结果shopId,distance
        //GEOSEARCH key BYLONLAT x y BYRADIUS 10 WITHDISTANCE
        String key = "shop:geo:"+typeId;
        //fromCoordinate通过经纬度查询,fromMember()通过点查询
        //new Distance(5000)半径
        //RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance()结果带上距离
        //limit()分页,永远从0开始到end的数量
        GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate.opsForGeo().search(key,
                GeoReference.fromCoordinate(x, y),
                new Distance(5000),
                RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance()
                        .limit(end));

        if(results == null){
            return Result.ok(Collections.emptyList());
        }

        List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();
        //没数据了,直接返回
        if(list.size() <= from){
            return Result.ok(Collections.emptyList());
        }

        //收集店铺id
        List<Long> ids = new ArrayList<>(list.size());
        Map<String,Distance> distanceMap = new HashMap<>(list.size());
        //截取from到end值
        list.stream().skip(from).forEach(result ->{
            //获取shopId
            String shopIdStr = result.getContent().getName();
            ids.add(Long.valueOf(shopIdStr));
            //获取距离
            Distance distance = result.getDistance();
            //{"shopId","距离"}放入map中
            distanceMap.put(shopIdStr,distance);
        });
        //5根据id批量查询店铺信息,必须有序
        String idStr = StrUtil.join(",", ids);
      /*  if(StrUtil.isBlank(idStr)){
            return Result.ok();
        }*/
        List<Shop> shops = query().in("id", ids).last("ORDER BY FIELD (id," + idStr + ")").list();
        //店铺存储距离
        for (Shop shop : shops) {
            shop.setDistance(distanceMap.get(shop.getId().toString()).getValue());
        }
        return Result.ok(shops);
    }

用户签到

如果用数据库方式实现,表结构如下:
在这里插入图片描述
但是用户一次签到,就有一条记录,假如有1000万用户,平均每人每年签到次数为10次,则这张表一年的数据量为 1亿条,每签到一次需要使用(8 + 8 + 1 + 1 + 3 + 1)共22 字节的内存,一个月则最多需要600多字节,数据量太大。

采用类似签到表方案,如图所示:
在这里插入图片描述
把每一个bit位对应当月的每一天,形成了映射关系。用0和1标示业务状态,这种思路就称为位图(BitMap)。这样就用极小的空间,来实现了大量数据的表示。(位图就是用Bit位与某种业务状态进行映射)
Redis中是利用string类型数据结构实现BitMap,因此最大上限是512M,转换为bit则是 2^32个bit位。

BitMap

  • SETBIT:向指定位置(offset)存入一个0或1

  • GETBIT :获取指定位置(offset)的bit值(只能查询一个值)

  • BITCOUNT :统计BitMap中值为1的bit位的数量
    在这里插入图片描述
    在这里插入图片描述

  • BITFIELD :操作(查询、修改、自增)BitMap中bit数组中的指定位置(offset)的值(一次可以查询多个值,u有符号i无符号) (查询结果为十进制)
    在这里插入图片描述

  • BITFIELD_RO :获取BitMap中bit数组,并以十进制形式返回

  • BITOP :将多个BitMap的结果做位运算(与 、或、异或)

  • BITPOS :查找bit数组中指定范围内第一个0或1出现的位置
    在这里插入图片描述

统计连续签到天数

从最后一次签到开始向前统计,直到遇到第一次未签到为止,计算总的签到次数,就是连续签到次数。

BITFIFLD key GET u[dayOfMonth] 0 本月到今天为止的所有签到数据

从后往前遍历每个bit位
1.与1做与运算,得到最后一个Bit位置
2.右移一位,下一个Bit成为最后一个Bit位,重复1.2步骤

   //连续签到天数
    @Override
    public Result signCount() {
        //当前登录用户
        Long userId = UserHolder.getUser().getId();
        //获取日期
        LocalDateTime now = LocalDateTime.now();
        //拼接key
        String keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyyMM"));
        String key = "sign:" + userId + keySuffix;
        //获取今天是本月第几天
        int dayOfMonth = now.getDayOfMonth();
        //获取本月截止今天为止的所有签到记录,返回的是十进制数字
        //BITFIELD sign:1010:202301 GET u9 0
        List<Long> result = stringRedisTemplate.opsForValue().bitField(key,
                BitFieldSubCommands.create().get(BitFieldSubCommands.BitFieldType.unsigned(dayOfMonth)).valueAt(0));
        if (CollUtil.isEmpty(result)) {
            return Result.ok(0);
        }

        Long num = result.get(0);
        if (num == null || num == 0) {
            return Result.ok(0);
        }
        //循环遍历
        int count = 0;
        while(true) {
            //数字与1做与运算,得到数字最后一个bit位
            if((num & 1) == 0){
                //为0,未签到,结束
                break;
            }else{
                //为1,已签到,计数器+1
                count++;
            }
            //数字右移一位,抛弃最后一位,继续下一位
            //num = num >> 1;
            num>>>=1;
        }
        return Result.ok(count);
    }

UV统计

UV:全称Unique Visitor,也叫独立访客量,是指通过互联网访问、浏览这个网页的自然人。1天内同一个用户多次访问该网站,只记录1次。
PV:全称Page View,也叫页面访问量或点击量,用户每访问网站的一个页面,记录1次PV,用户多次打开页面,则记录多次PV。往往用来衡量网站的流量。

UV统计在服务端做会比较麻烦,因为要判断该用户是否已经统计过了,需要将统计过的用户信息保存。但是如果每个访问的用户都保存到Redis中,数据量会非常恐怖,可以采用HLL。

Hyperloglog(HLL)是从Loglog算法派生的概率算法,用于确定非常大的集合的基数,而不需要存储其所有值。
相关算法参考:https://juejin.cn/post/6844903785744056333#heading-0
Redis中的HLL是基于string结构实现的,单个HLL的内存永远小于16kb内存占用低的令人发指!作为代价,其测量结果是概率性的,有小于0.81%的误差。不过对于UV统计来说,这完全可以忽略。
在这里插入图片描述
测试:向HyperLogLog中添加100万条数据,看看内存占用和统计效果如何

    @Test
    void testHyperLogLog() {
        String[] values = new String[1000];
        int j = 0;
        for (int i = 0; i < 1000000; i++) {
            j = i % 1000;
            values[j] = "user_" + i;
            if(j == 999){
                // 发送到Redis
                stringRedisTemplate.opsForHyperLogLog().add("hl2", values);
            }
        }
        // 统计数量
        Long count = stringRedisTemplate.opsForHyperLogLog().size("hl2");
        System.out.println("count = " + count);//997593
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值