当前位置 : 主页 > 编程语言 > java >

Springboot Redis pipline 批量读取数据

来源:互联网 收集:自由互联 发布时间:2023-03-22
目的: 减少网络IO; 降低redis访问压力; DateTime beginTime = DateTime . now (); //读取MV所有Key String mvKeyPrefix = RedisUtil . PREFIX_RTDB + ":MV:" ; Set String keys = stringRedisTemplate . keys ( mvKeyPrefix + "*" ); i

目的:

  • 减少网络IO;
  • 降低redis访问压力;
DateTime beginTime = DateTime.now();//读取MV所有KeyString mvKeyPrefix = RedisUtil.PREFIX_RTDB + ":MV:";Set<String> keys = stringRedisTemplate.keys(mvKeyPrefix + "*");if( keys == null ){ log.error( "Read MV data with key[" + mvKeyPrefix + "*" + "]from redis failed!" ); return;}log.info( "keys count: " + keys.size() );//transform to listList<String> keysList = new ArrayList<>( keys );//批量获取整个MV表实时值List<Object> objectList = stringRedisTemplate.executePipelined((RedisCallback<Object>) connection -> { for( String key : keysList ) { connection.hGet(key.getBytes(), "instmag".getBytes()); } return null; });//组装 <mvNo, mvRtValue> MapMap<Integer, Double> mvRtDataMap = new HashMap<>();for(int nIndex = 0; nIndex < keys.size(); nIndex++){ //mv no String key = keysList.get(nIndex); Integer mvNo = Integer.parseInt( key.replace( mvKeyPrefix, "" ) ); //value Object obj = objectList.get(nIndex); if( obj == null ) obj = "0"; Double dValue = Double.parseDouble( obj.toString() ); // mvRtDataMap.put( mvNo, dValue );}// log.info( mvRtDataMap.toString() );DateTime endTime = DateTime.now();Interval interval = new Interval( beginTime, endTime );log.info("cost: " + interval.toPeriod().getMillis() + " ms" );
网友评论