Redis使用pipeline批量查询所有键值对
本意是redis存储hashMap的时候单个key对应的value值存储的数据过大,有11m,读取该value值速度太慢,查询网上资料后都说把这个key-value拆分,再用pipeline或者multiGet查询,经过调研,如下:把这个key-value拆分成了46个key-value,然后再用pipeline或者multiGet查询,发现pipeline和multiGet确实可以批量查询多键值对,但是总速度跟之前单个键值对查询速度没有区别-.- 下面是redis表和pipeline以及multiGet的批量查询方法:
对应的redis表
@SuppressWarnings({ "rawtypes", "unchecked" })
public List executePipelined(Collection<String> keySet) {
return redisTemplate.executePipelined(new SessionCallback<Object>() {
@Override
public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {
HashOperations hashOperations = operations.opsForHash();
for (String key : keySet) {
hashOperations.entries(key);
}
return null;
}
});
}
@Test
public void testPp(){
Collection<String> keySet = new ArrayList<>();
keySet.add("info_platform_test:7");
List list = executePipelined(keySet);
}
multiGet用法
public List<String> multiGetCaches(List<String> key){
try{
if (null != key && key.size() > 0) {
Collection<String> collection = key;
List<String> list = redisTemplate.opsForHash().multiGet("info_platform_test:7",collection);
if (null != list && list.size() > 0) {
return list;
} else {
return null;
}
}else {
return null;
}
}catch (RedisConnectionFailureException e){
return null;
} catch (Exception e){
return null;
}
}
@Test
public void multTest(){
List<String> stringList = new ArrayList<>();
for (int j=0; j<47; j++){
stringList.add(j+"");
}
List<String> list = multiGetCaches(stringList);
}
|