【hash使用场景之房间号创建】

RandomService.java
package org.example.testjedis;

import java.util.Random;

public class RandomService {
    private static final Random r = new Random();

    /**
     * 随机一个start-end的值
     *
     * @param start
     * @param end
     * @return
     */
    public static int rand(int start, int end) {
        return Math.abs(r.nextInt()) % (end - start + 1) + start;
    }
}

RoomIdService.java

package org.example.testjedis;

import redis.clients.jedis.Jedis;

public class RoomIdService {
    private static final int beganRoomId = 111111;
    private static final int endRoomId = 999999;
    private static final String ROOM_ID_KEY = "roomid";

    /**
     * 随机1个6位的房间号
     *
     * @return
     */
    public static int getRoomId() {
        int roomId = -1;
        long ret = 0;

        int count = 0;
        while (ret == 0) {
            roomId = RandomService.rand(beganRoomId, endRoomId);
            try (Jedis jedis = new Jedis()) {
                ret = jedis.hsetnx(ROOM_ID_KEY, String.valueOf(roomId), "1");
            }

            // 没找到,则说明房间号紧缺,创建房间号失败. 基本不会发生这样的情况
            if (ret == 0) {
                if (++count > 1000) {
                    roomId = -1;
                    break;
                }
            }
        }

        return roomId;
    }

    /**
     * 房间结束,删除过期的房间号
     *
     * @param roomId
     * @return 0:失败 1:成功
     */
    public static boolean delRoomId(int roomId) {
        long del = 0;
        try (Jedis jedis = new Jedis()) {
            del = jedis.hdel(ROOM_ID_KEY, String.valueOf(roomId));
        }
        return del == 1;
    }

    /**
     * 清理所有的房间号
     */
    public static void clearAllRoomId() {
        System.out.println("开始清理房间号...");
        try (Jedis jedis = new Jedis()) {
            jedis.del(ROOM_ID_KEY);
        }
        System.out.println("清理房间号结束...");
    }

    /**
     * 已经使用的房间号数量
     *
     * @return
     */
    public static int getUsedRoomIdSize() {
        long len = 0;
        try (
                Jedis jedis = new Jedis()
        ) {
            len = jedis.hlen(ROOM_ID_KEY);
        }
        return (int) len;
    }
}

Main.java

package org.example.testjedis;

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        RoomIdService.clearAllRoomId();

        Set<Integer> roomIdSet = new HashSet<>();
        for (int i = 0; i < 88; i++) {
            int roomId = RoomIdService.getRoomId();
            if (roomId > 0) {
                roomIdSet.add(roomId);
            } else {
                System.err.println("创建房间失败");
            }
        }

        System.out.println(roomIdSet);
        System.out.println(roomIdSet.size());
        System.out.println(RoomIdService.getUsedRoomIdSize());
    }
}

/*
开始清理房间号...
清理房间号结束...
[706314, 585993, 789134, 570506, 851599, 283777, 226571, 696449, 172813, 245522, 189715, 424727, 370838, 979989, 466714, 237603, 992559, 789930, 295458, 363565, 194985, 621346, 893728, 692903, 880160, 918305, 365617, 986551, 859569, 200383, 321081, 433848, 447416, 669620, 854477, 592969, 280389, 179008, 952265, 371138, 525248, 161354, 995398, 426956, 646851, 803911, 407373, 322639, 895425, 147790, 716358, 739782, 197453, 270539, 425045, 138838, 499411, 239706, 509405, 810838, 963794, 912620, 739306, 853996, 354918, 375009, 384352, 303969, 541805, 294764, 947812, 269419, 949089, 783995, 896509, 758779, 138864, 263415, 508275, 179446, 676222, 147450, 555120, 630129, 529139, 145273, 927731, 586486]
88
88
 */

总结:对于全球同服这种,房间号统一管理用redis则非常方便

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值