当前位置: 首页 > article >正文

redis-bitmap使用场景

bitmap原理

Bitmap(位图)是一种基于二进制位的数据结构,用于高效地存储和操作大量的布尔值

可以对单个位进行读写操作

demo

package org.example;

import org.redisson.Redisson;
import org.redisson.api.RBitSet;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

public class BitmapTest {



    private static final RedissonClient redisson;

    static {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://******.redis.rds.aliyuncs.com:6379")
                .setUsername("******")
                .setPassword("*******")
        ;
        redisson = Redisson.create(config);
    }

    public static void main(String[] args) {

        // 获取 Bitmap 对象
        RBitSet bitSet = redisson.getBitSet("myBitMap");
        // 操作 Bitmap
        bitSet.set(0, true);  // 设置第 0 位为 1
        bitSet.set(1, false); // 设置第 1 位为 0
        bitSet.set(20, 30, true); // 设置第 20 到 29 位为 1

        // 获取位的值
        boolean bitValue = bitSet.get(0);
        System.out.println("Bit at position 0: " + bitValue);


        boolean bit1Value = bitSet.get(1);
        System.out.println("Bit at position 1: " + bit1Value);

        // 未设置的默认false
        boolean bit100Value = bitSet.get(100);
        System.out.println("Bit at position 100: " + bit100Value);

        // 统计所有为1的个数
        long count = bitSet.cardinality();   
        System.out.println("Number of bits set to 1: " + count);

        获取 Bitmap 的长度
        long size = bitSet.size();
        System.out.println("Bitmap size: " + size);

        // 关闭 Redisson 客户端
        redisson.shutdown();
    }

}

 

Bitmap 的长度是动态扩展的

未设置的位默认为 0

使用场景

用户签到

统计活跃用户

统计每天使用系统的用户总数,bitmap上每位代表一个用户,如果重复登陆,则是重复对某位设置,不会影响结果。

布隆过滤器


                

http://www.kler.cn/a/563438.html

相关文章:

  • Nacos + Dubbo3 实现微服务的Rpc调用
  • Vue组件间通信的方式
  • 毕业项目推荐:基于yolov8/yolo11的苹果叶片病害检测识别系统(python+卷积神经网络)
  • angular舒尔特方格
  • 医院HIS接入大模型:算力基础设施与训练能力的深度剖析与测算
  • Docker基础-常见命令
  • 面试之《react中,fiber更新时,怎么判断fiber是否应该在当前idle内执行》
  • osgEarth安装总结
  • 将夸克网盘的webdav挂载成本地磁盘驱动器时报错“405“
  • 学习汇编前置知识第二天
  • nginx代理后502
  • seacmsv9 SQL注入漏洞
  • DFS:二叉树的深搜与回溯
  • 冒泡排序(Bubble Sort)详细教程:Java实现与优化
  • Python使用列表实现栈、队列学习记录
  • 【C++修炼之路】C++类与对象:面向对象编程的第一步
  • 字节火山引擎-大模型声音复刻,流式语音合成接口
  • Linux网络之传输层协议(UDP,TCP协议)
  • OpenGL ES -> GLSurfaceView绘制点、线、三角形、正方形、圆(顶点法绘制)
  • 【Python爬虫(82)】开启物联网数据爬取之旅