亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)化內(nèi)存,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)計指定范圍內(nèi)的在線用戶數(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)建對應(yī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方案的適用場景

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

2. Bitmap方案的適用場景

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

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

相關(guān)文章

  • java文件操作練習(xí)代碼 讀取某個盤符下的文件

    java文件操作練習(xí)代碼 讀取某個盤符下的文件

    這篇文章主要介紹了java讀取某個盤符下的文件示例,代碼中要求的是絕對路徑,編譯過程中要注意絕對路徑問題和異常的抓取
    2014-01-01
  • Java數(shù)據(jù)結(jié)構(gòu)之快速冪的實現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)之快速冪的實現(xiàn)

    快速冪是用來解決求冪運算的高效方式。本文將詳細為大家介紹如何利用Java實現(xiàn)快速冪,以及利用快速冪求解冪運算問題,需要的可以參考一下
    2022-03-03
  • 詳解Java變量與常量

    詳解Java變量與常量

    這篇文章主要介紹了Java變量與常量,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • java常用工具類 UUID、Map工具類

    java常用工具類 UUID、Map工具類

    這篇文章主要為大家詳細介紹了Java常用工具類,包括UUID工具類、Map工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Spring Boot應(yīng)用的極速部署腳本示例代碼

    Spring Boot應(yīng)用的極速部署腳本示例代碼

    最近在工作中遇到了一個問題,需要極速的部署Spring Boot應(yīng)用,發(fā)現(xiàn)網(wǎng)上這方面的資料較少,所以自己來總結(jié)下,這篇文章主要給大家介紹了關(guān)于Spring Boot應(yīng)用的極速部署腳本的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • Java深入探究關(guān)鍵字abstract的使用

    Java深入探究關(guān)鍵字abstract的使用

    如果一個方法使用 abstract 來修飾,則說明該方法是抽象方法,抽象方法只有聲明沒有實現(xiàn)。需要注意的是 abstract 關(guān)鍵字只能用于普通方法,不能用于 static 方法或者構(gòu)造方法中
    2022-05-05
  • 詳解Struts2攔截器機制

    詳解Struts2攔截器機制

    這篇文章主要介紹了詳解Struts2攔截器機制,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • MyBatis注解方式之@Update/@Delete使用詳解

    MyBatis注解方式之@Update/@Delete使用詳解

    這篇文章主要介紹了MyBatis注解方式之@Update/@Delete使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java橋接模式實例詳解【簡單版與升級版】

    Java橋接模式實例詳解【簡單版與升級版】

    這篇文章主要介紹了Java橋接模式,結(jié)合實例形式分析了java橋接模式簡單版與升級版兩種實現(xiàn)技巧,需要的朋友可以參考下
    2019-07-07
  • Java中的JScrollPane使用詳細說明

    Java中的JScrollPane使用詳細說明

    這篇文章主要給大家介紹了關(guān)于Java中JScrollPane使用的相關(guān)資料,Java JScrollPane是Swing庫提供的一個組件,用于在需要滾動的區(qū)域中顯示內(nèi)容,需要的朋友可以參考下
    2024-07-07

最新評論