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

SpringBoot依赖之Spring Data Redis实现位图Bitmap

Spring Boot 项目中使用 Spring Data Redis 实现位图Bitmap

暂未发表,记录于20240820

概念

Spring Data Redis (Access+Driver)
  • 依赖名称: Spring Data Redis (Access+Driver)
  • 功能描述: Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.
  • 中文释义:用于同步、异步和反应式使用的高级且线程安全的 Java Redis 客户端。支持集群、哨兵、管道、自动重新连接、编解码器等。

项目学习代码地址

操作演示:

在IDEA中创建项目过程可以参考上一篇:
SpringBoot依赖之Spring Data Redis一有序集合Sorted Set

Spring Boot 项目中使用 Spring Data Redis 实现位图(Bitmap)

接下来我们演示在 Spring Boot 项目中使用 Spring Data Redis 实现位图(Bitmap)操作,我们可以在之前的项目代码基础上扩展 Redis 服务类和控制器类,以支持对 Redis 列表的常见操作。以下是具体的实现步骤。

为了在 Spring Boot 项目中使用 Spring Data Redis 实现位图(Bitmap)操作,我们可以在之前的项目代码基础上扩展 Redis 服务类和控制器类,以支持对 Redis 列表的常见操作。以下是具体的实现步骤。

1. 更新 Redis 服务类

RedisService 类中添加列表相关的方法。

package com.dependencies.springdataredis;import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class RedisService {private final RedisTemplate<String, Object> redisTemplate;private final ValueOperations<String, Object> valueOperations;public RedisService(RedisTemplate<String, Object> redisTemplate) {this.redisTemplate = redisTemplate;this.valueOperations = redisTemplate.opsForValue();}// Bitmap操作public void setBit(String key, long offset, boolean value) {valueOperations.setBit(key, offset, value);}public boolean getBit(String key, long offset) {return valueOperations.getBit(key, offset);}
}
2. 更新控制器类

RedisController 中添加处理列表操作的端点。

package com.dependencies.springdataredis;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** @author zhizhou   2024/8/17 12:02*/
@RestController
public class RedisController {private final RedisService redisService;public RedisController(RedisService redisService) {this.redisService = redisService;}@GetMapping("/bitmap/set")public String setBit(@RequestParam String key, @RequestParam long offset, @RequestParam boolean value) {redisService.setBit(key, offset, value);return "位图中的位设置";}@GetMapping("/bitmap/get")public boolean getBit(@RequestParam String key, @RequestParam long offset) {return redisService.getBit(key, offset);}
}
3. 验证测试:

我们启动项目以后,就通过以下的额 URL 测试 Redis 列表的功能:

Bitmap 操作:

  • 设置 Bitmap 中某个位的值: http://localhost:8080/bitmap/set?key=oneBitmap&offset=996&value=true
  • 获取 Bitmap 中某个位的值: http://localhost:8080/bitmap/get?key=oneBitmap&offset=996
    在这里插入图片描述
    在这里插入图片描述
4. 总结

​ Redis Bitmap 本质上是一个二进制数组,支持按位操作,适合用于存储大量布尔值。使用场景:

1、用户签到:每个用户的签到情况可以用一个 Bitmap 存储,一个 bit 代表一天的签到状态。

2、活跃用户统计:通过位图可以高效统计某段时间内用户的活跃情况。

之前的业务中我们使用该类型来做实验,将几十万的userId 塞进bitmap中,直接判断阿布abool就可以执行相应的灰度策略。

setBit(expirement-1, userId, TRUE);Boolean abool = getBit(expirement-1, userId);

可以关注我,一起学习,一起为程序员职业生涯蓄能。


http://www.mrgr.cn/news/7415.html

相关文章:

  • mac苹果电脑配置Docker最新国内源
  • Java语言程序设计——篇十七(3)
  • Hadoop入门基础(二):Hadoop集群安装与部署详解(超详细教程)
  • 计算机网络:DNS、子网掩码、网关
  • 周末总结(2024/08/24)
  • 上位机图像处理和嵌入式模块部署(linux Qt程序的编译)
  • 【算法】粒子群优化
  • HarmonyOS应用开发者基础认证
  • python脚本:输入基因名,通过爬虫的方式获取染色体上的location。
  • 英伟达H20核心价值和高效运用揭秘
  • 微知-lspci如何查看pcie设备树状结构(-t)
  • el-image的配套使用(表格,表单)
  • 【PyTorch】深度学习PyTorch加载数据
  • react 路由创建与使用
  • docker应用
  • 软件测试——自动化测试selenium常用函数
  • 关于python的Django项目性能优化
  • 【开源社区】Elasticsearch(ES)中 exists 查询空值字段的坑
  • CSS3多行多栏布局
  • 计算机网络——HTTP与HTTPS协议