zoukankan      html  css  js  c++  java
  • Springboot整合redis:bitmaps用法

    1、统计当前登录用户数
    
    2、统计近五分钟的在线设备数量
    

      

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    

      

        /**
         * 设置bitmaps,统计在线设备数
         *
         * @param deviceId
         */
        private void setBitmaps(String key, Long deviceId) {
            try {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                //获取当前时间
                String curDate = formatter.format(new Date(System.currentTimeMillis()));
                stringRedisTemplate.opsForValue().setBit(key + curDate, deviceId, true);
    } catch (Exception e) { log.error("setBitmaps,deviceId is {}, exception is {}", deviceId, e); } } /** * 统计近五分钟在线数 */ public void getOnlineCount() { try { String key = "aiot:towercrane:device:online:"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //获取当前时间 String curDate = key + formatter.format(new Date(System.currentTimeMillis())); String curDateBeforeOne = key + formatter.format(new Date(System.currentTimeMillis() - 1 * 60 * 1000L)); String curDateBeforeTwo = key + formatter.format(new Date(System.currentTimeMillis() - 2 * 60 * 1000L)); String curDateBeforeThree = key + formatter.format(new Date(System.currentTimeMillis() - 3 * 60 * 1000L)); String curDateBeforeFour = key + formatter.format(new Date(System.currentTimeMillis() - 4 * 60 * 1000L)); long count = (long) stringRedisTemplate.execute((RedisCallback<Long>) con -> con.bitOp(RedisStringCommands.BitOperation.AND, curDate.getBytes(), curDateBeforeOne.getBytes(), curDateBeforeTwo.getBytes(), curDateBeforeThree.getBytes(), curDateBeforeFour.getBytes())); System.out.println(count); } catch (Exception e) { log.error("getAttendanceOnlineCount exception is {}", e); } }

      

    stringRedisTemplate
  • 相关阅读:
    使用Egret Conversion 转化as代码到ts代码,
    C++ 部分知识点
    mac下 使用 versions版本控制工具 修复游戏bug过程
    Egret 显示容器
    Egret --视觉编程,显示对象,事件
    VS2015 使用
    TypeScript 学习四 面向对象的特性,泛型,接口,模块,类型定义文件*.d.ts
    Prestashop--网站后台配置(一)
    Prestashop--访问优化
    eclipse 中 Android sdk 无法更新的问题
  • 原文地址:https://www.cnblogs.com/jiehanshi/p/14140729.html
Copyright © 2011-2022 走看看