最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot運用Redis統(tǒng)計用戶在線數(shù)量的兩種方法實現(xiàn)

 更新時間:2025年06月25日 09:36:19   作者:weixin_43833540  
本文主要介紹了SpringBoot運用Redis統(tǒng)計用戶在線數(shù)量的兩種方法實現(xiàn),包括通過RedisSet精確記錄用戶狀態(tài),或用RedisBitmap按位存儲優(yōu)化內存,Set適合小規(guī)模場景,Bitmap適用于大規(guī)模連續(xù)ID,可根據(jù)需求選擇實現(xiàn)方式

在Spring Boot里運用Redis統(tǒng)計用戶在線數(shù)量。

項目依賴與配置

1. 引入依賴

首先,在pom.xml文件中添加Spring Data Redis依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. 配置Redis連接

application.properties中進行Redis連接的配置:

spring.redis.host=localhost
spring.redis.port=6379

方案1:借助Redis Set實現(xiàn)精確統(tǒng)計

1. 創(chuàng)建Redis操作Service

編寫一個Redis操作Service,用于處理用戶在線狀態(tài):

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class OnlineUserService {

    private static final String ONLINE_USERS_KEY = "online_users";

    private final RedisTemplate<String, String> redisTemplate;

    public OnlineUserService(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    // 用戶登錄
    public void login(String userId) {
        redisTemplate.opsForSet().add(ONLINE_USERS_KEY, userId);
    }

    // 用戶退出
    public void logout(String userId) {
        redisTemplate.opsForSet().remove(ONLINE_USERS_KEY, userId);
    }

    // 獲取在線用戶數(shù)
    public Long getOnlineCount() {
        return redisTemplate.opsForSet().size(ONLINE_USERS_KEY);
    }

    // 獲取所有在線用戶ID
    public Set<String> getOnlineUsers() {
        return redisTemplate.opsForSet().members(ONLINE_USERS_KEY);
    }
}

2. 控制器示例

創(chuàng)建一個控制器,用于測試上述功能:

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/online")
public class OnlineUserController {

    private final OnlineUserService onlineUserService;

    public OnlineUserController(OnlineUserService onlineUserService) {
        this.onlineUserService = onlineUserService;
    }

    @PostMapping("/login/{userId}")
    public String login(@PathVariable String userId) {
        onlineUserService.login(userId);
        return userId + " 已登錄";
    }

    @PostMapping("/logout/{userId}")
    public String logout(@PathVariable String userId) {
        onlineUserService.logout(userId);
        return userId + " 已退出";
    }

    @GetMapping("/count")
    public Long getCount() {
        return onlineUserService.getOnlineCount();
    }

    @GetMapping("/users")
    public Set<String> getUsers() {
        return onlineUserService.getOnlineUsers();
    }
}

方案2:使用Redis Bitmap實現(xiàn)按位存儲

1. Bitmap操作Service

創(chuàng)建一個專門用于Bitmap操作的Service:

import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class OnlineUserBitmapService {

    private static final String ONLINE_USERS_BITMAP_KEY = "online_users_bitmap";

    private final RedisTemplate<String, Object> redisTemplate;

    public OnlineUserBitmapService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    // 用戶登錄(userId需為Long類型)
    public void login(Long userId) {
        redisTemplate.execute((RedisCallback<Boolean>) connection ->
                connection.setBit(ONLINE_USERS_BITMAP_KEY.getBytes(), userId, true));
    }

    // 用戶退出
    public void logout(Long userId) {
        redisTemplate.execute((RedisCallback<Boolean>) connection ->
                connection.setBit(ONLINE_USERS_BITMAP_KEY.getBytes(), userId, false));
    }

    // 檢查用戶是否在線
    public Boolean isOnline(Long userId) {
        return redisTemplate.execute((RedisCallback<Boolean>) connection ->
                connection.getBit(ONLINE_USERS_BITMAP_KEY.getBytes(), userId));
    }

    // 獲取在線用戶數(shù)
    public Long getOnlineCount() {
        return redisTemplate.execute((RedisCallback<Long>) connection ->
                connection.bitCount(ONLINE_USERS_BITMAP_KEY.getBytes()));
    }

    // 統(tǒng)計指定范圍內的在線用戶數(shù)
    public Long getOnlineCount(long start, long end) {
        return redisTemplate.execute((RedisCallback<Long>) connection ->
                connection.bitCount(ONLINE_USERS_BITMAP_KEY.getBytes(), start, end));
    }
}

2. 控制器示例

創(chuàng)建對應的控制器:

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/online/bitmap")
public class OnlineUserBitmapController {

    private final OnlineUserBitmapService onlineUserBitmapService;

    public OnlineUserBitmapController(OnlineUserBitmapService onlineUserBitmapService) {
        this.onlineUserBitmapService = onlineUserBitmapService;
    }

    @PostMapping("/login/{userId}")
    public String login(@PathVariable Long userId) {
        onlineUserBitmapService.login(userId);
        return userId + " 已登錄";
    }

    @PostMapping("/logout/{userId}")
    public String logout(@PathVariable Long userId) {
        onlineUserBitmapService.logout(userId);
        return userId + " 已退出";
    }

    @GetMapping("/count")
    public Long getCount() {
        return onlineUserBitmapService.getOnlineCount();
    }

    @GetMapping("/{userId}")
    public Boolean isOnline(@PathVariable Long userId) {
        return onlineUserBitmapService.isOnline(userId);
    }
}

使用建議

1. Set方案的適用場景

  • 當需要精確統(tǒng)計在線用戶數(shù)量,并且能夠獲取在線用戶列表時,可以使用Set方案。
  • 適合用戶規(guī)模在百萬級別以下的情況,因為Set會存儲每個用戶的ID。

2. Bitmap方案的適用場景

  • 若用戶ID是連續(xù)的整數(shù)(或者可以映射為連續(xù)整數(shù)),Bitmap方案會更節(jié)省內存。
  • 對于大規(guī)模用戶(比如億級)的在線統(tǒng)計,Bitmap方案具有明顯優(yōu)勢。
  • 示例中使用Long類型的userId,在實際應用中,你可能需要一個ID映射器,將業(yè)務ID轉換為連續(xù)的整數(shù)。

到此這篇關于SpringBoot運用Redis統(tǒng)計用戶在線數(shù)量的兩種方法實現(xiàn)的文章就介紹到這了,更多相關Spring Boot Redis統(tǒng)計用戶在線內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot使用CXF集成WebService的方法

    SpringBoot使用CXF集成WebService的方法

    這篇文章主要介紹了SpringBoot使用CXF集成WebService的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Java動態(tài)編譯執(zhí)行代碼示例

    Java動態(tài)編譯執(zhí)行代碼示例

    這篇文章主要介紹了Java動態(tài)編譯執(zhí)行代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Mybatis入門教程之新增、更新、刪除功能

    Mybatis入門教程之新增、更新、刪除功能

    這篇文章給大家介紹了Mybatis進行基本的增刪改操作,非常不錯,具有參考借鑒價值,需要的的朋友參考下
    2017-02-02
  • Java子線程無法獲取Attributes的解決方法(最新推薦)

    Java子線程無法獲取Attributes的解決方法(最新推薦)

    在Java多線程編程中,子線程無法直接獲取主線程設置的Attributes是一個常見問題,本文探討了這一問題的原因,并提供了兩種解決方案,對Java子線程無法獲取Attributes的解決方案感興趣的朋友一起看看吧
    2025-01-01
  • 編寫Java代碼對HDFS進行增刪改查操作代碼實例

    編寫Java代碼對HDFS進行增刪改查操作代碼實例

    這篇文章主要介紹了Java代碼對HDFS進行增刪改查操作,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Java查搜索文件內容實現(xiàn)方式

    Java查搜索文件內容實現(xiàn)方式

    用戶為解決無法搜索文件內容的問題,編寫了一個支持單文件、文件夾及多層遞歸查找的工具,具備字符串匹配及忽略大小寫功能,并計劃擴展日期、創(chuàng)建人等組合搜索條件,最終打包成exe便于快速查找所有文件內容
    2025-09-09
  • Java NIO.2 使用Path接口來監(jiān)聽文件、文件夾變化

    Java NIO.2 使用Path接口來監(jiān)聽文件、文件夾變化

    Java7對NIO進行了大的改進,新增了許多功能,接下來通過本文給大家介紹Java NIO.2 使用Path接口來監(jiān)聽文件、文件夾變化 ,需要的朋友可以參考下
    2019-05-05
  • Java實現(xiàn)PNG圖片格式轉BMP圖片格式

    Java實現(xiàn)PNG圖片格式轉BMP圖片格式

    在實際開發(fā)中,有時需要在不同平臺、不同應用場景中對圖片格式進行轉換,本文主要介紹了如何使用 Java 語言實現(xiàn)將 PNG 格式的圖片轉換為 BMP 格式的圖片,需要的可以了解下
    2025-03-03
  • Java中2個對象字段值比較是否相同

    Java中2個對象字段值比較是否相同

    本文主要介紹了Java中2個對象字段值比較是否相同,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • Java?超詳細講解數(shù)據(jù)結構中的堆的應用

    Java?超詳細講解數(shù)據(jù)結構中的堆的應用

    堆首先是一個完全二叉樹,堆分為小根堆和大根堆。小根堆,所有結點的左右子節(jié)點都不小于根節(jié)點;大根堆,所有結點的左右子節(jié)點都不大于根節(jié)點。優(yōu)先級隊列(priorityQueue)底層就是一個小根堆
    2022-04-04

最新評論

大足县| 德昌县| 金乡县| 论坛| 布尔津县| 五寨县| 广灵县| 烟台市| 长子县| 唐山市| 大厂| 和林格尔县| 彭阳县| 霍林郭勒市| 田阳县| 石景山区| 汽车| 马鞍山市| 贡嘎县| 平乡县| 龙州县| 斗六市| 德昌县| 金塔县| 板桥市| 白城市| 肥西县| 鹿邑县| 石阡县| 叙永县| 林甸县| 封开县| 资阳市| 什邡市| 万山特区| 宜兰县| 靖远县| 临安市| 青河县| 卓尼县| 安西县|