Redis中bitmap用于用户在线状态的数量统计

目录

Java测试

命令行数据测试


REDIS bitmap统计用户在线测试(可以针对用户单位快速统计,redis函数统计只返回在线数量)。

文章思想参考:第十章:Redis中bitmap的妙用

Java测试

Redis工具就不提供了,Jedis里面有提供,自己封装即可。

package test;


import java.util.List;
import java.util.Set;
import com.xxx.redis.RedisNodeManagerUtil;
import com.xxx.redis.RedisNodeManger;

public class RedisTest {
	
	String node = "aliyun";
	
	String dwCode = "xxxxxxxxxxx";
	
	static {
		RedisNodeManger.init();
	}
	
	
	/**
	 * 用户在线删除
	 */
	public void bitmapUserDelKey(){
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String key = dwCode;
		RedisNodeManagerUtil.del(node, key);
	}
	
	/**
	 * 用户在线离线设置
	 */
	public void bitmapUserIdValues(){
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long offset = 2020*1000;
		String key = dwCode;// 单位编码标识:在线数量
		for (int i = 10; i < 21; i++) {
			if(i%2 == 0){
				RedisNodeManagerUtil.setbit(node, key, offset+i, true);
			}else{
				RedisNodeManagerUtil.setbit(node, key, offset+i, false);
			}
		}
		Long count = RedisNodeManagerUtil.bitcount(node, key);
		System.out.println("true count key:"+count);
	}

	/**
	 * 统计在线数量
	 */
	public void bitmapCount(){
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String key = dwCode;
		Long count = RedisNodeManagerUtil.bitcount(node, key);
		System.out.println("count key:"+count);
		count = RedisNodeManagerUtil.bitcount(node, key, 0, Long.MAX_VALUE);
		System.out.println("count range key:"+count);
		/*count = RedisNodeManagerUtil.bitpos(node, key, true);
		System.out.println("bitpos true:"+count);
		count = RedisNodeManagerUtil.bitpos(node, key, false);
		System.out.println("bitpos false:"+count);*/
	}
	
	/**
	 * 函数入口
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		RedisTest test = new RedisTest();
		test.bitmapUserDelKey();
		test.bitmapUserIdValues();
		test.bitmapCount();
		
	}

}

测试结果:

Redis初始化完成
192.168.1.189:6379 redis节点初始化完成
true count key:6
count key:6
count range key:6

 

命令行数据测试

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章