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

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

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

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

項(xiàng)目依賴(lài)與配置

1. 引入依賴(lài)

首先,在pom.xml文件中添加Spring Data Redis依賴(lài):

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

2. 配置Redis連接

application.properties中進(jìn)行Redis連接的配置:

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

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

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

編寫(xiě)一個(gè)Redis操作Service,用于處理用戶(hù)在線狀態(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;
    }

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

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

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

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

2. 控制器示例

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

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實(shí)現(xiàn)按位存儲(chǔ)

1. Bitmap操作Service

創(chuàng)建一個(gè)專(zhuān)門(mén)用于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;
    }

    // 用戶(hù)登錄(userId需為L(zhǎng)ong類(lèi)型)
    public void login(Long userId) {
        redisTemplate.execute((RedisCallback<Boolean>) connection ->
                connection.setBit(ONLINE_USERS_BITMAP_KEY.getBytes(), userId, true));
    }

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

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

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

    // 統(tǒng)計(jì)指定范圍內(nèi)的在線用戶(hù)數(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)建對(duì)應(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方案的適用場(chǎng)景

  • 當(dāng)需要精確統(tǒng)計(jì)在線用戶(hù)數(shù)量,并且能夠獲取在線用戶(hù)列表時(shí),可以使用Set方案。
  • 適合用戶(hù)規(guī)模在百萬(wàn)級(jí)別以下的情況,因?yàn)镾et會(huì)存儲(chǔ)每個(gè)用戶(hù)的ID。

2. Bitmap方案的適用場(chǎng)景

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

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

相關(guān)文章

  • SpringBoot使用CXF集成WebService的方法

    SpringBoot使用CXF集成WebService的方法

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

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

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

    Mybatis入門(mén)教程之新增、更新、刪除功能

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

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

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

    編寫(xiě)Java代碼對(duì)HDFS進(jìn)行增刪改查操作代碼實(shí)例

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

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

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

    Java NIO.2 使用Path接口來(lái)監(jiān)聽(tīng)文件、文件夾變化

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

    Java實(shí)現(xiàn)PNG圖片格式轉(zhuǎn)BMP圖片格式

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

    Java中2個(gè)對(duì)象字段值比較是否相同

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

    Java?超詳細(xì)講解數(shù)據(jù)結(jié)構(gòu)中的堆的應(yīng)用

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

最新評(píng)論

丰城市| 金昌市| 开鲁县| 鸡东县| 岢岚县| 错那县| 东乡| 定远县| 当雄县| 大荔县| 洛扎县| 垦利县| 随州市| 九龙坡区| 镇赉县| 祁东县| 海晏县| 依兰县| 达州市| 长葛市| 河池市| 栾城县| 桃源县| 南召县| 上高县| 嵩明县| 和政县| 南通市| 平度市| 义乌市| 东阳市| 盐城市| 浮梁县| 金沙县| 岗巴县| 南投市| 加查县| 南涧| 开鲁县| 赣州市| 延庆县|