目的:

  • 减少网络IO;
  • 降低redis访问压力;
DateTime beginTime = DateTime.now();

//读取MV所有Key
String 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 list
List<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> Map
Map<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" );