当我们需要一次性获取在redis中以hash方式存储的所有key-value时,我们可以使用下面的方式来获取。 public void testGetMore() throws IOException
当我们需要一次性获取在redis中以hash
方式存储的所有key-value
时,我们可以使用下面的方式来获取。
public void testGetMore() throws IOException { RedisCallback<List<Object>> pipelineCallback = new RedisCallback<List<Object>>() { @Override public List<Object> doInRedis(RedisConnection connection) throws DataAccessException { connection.openPipeline(); connection.hGetAll("1129de8b-25bb-5678-be75-9552bcccf660".getBytes()); connection.hGetAll("1129de8b-25bb-1234-be75-9552bcccf660".getBytes()); return connection.closePipeline(); }; List<Object> results = (List<Object>) redisTemplate.execute(pipelineCallback); for (Object item : results) { if (item==null) { System.out.println("I'm null"); }else { if (item instanceof JedisByteHashMap) { JedisByteHashMap jedisByteHashMap = (JedisByteHashMap)item; for (Map.Entry entry : jedisByteHashMap.entrySet()) { byte[] key = (byte[]) entry.getKey(); System.out.println(new String(key)); byte[] value = (byte[]) entry.getValue(); System.out.println(new String(value)); } } } } }
当然了,我们需要封装这个函数为通用的方法,这个时候,我们只需要把Key的List作形参即可。这里不再封装了,需要注意的就是一定要判断null的情况。因为传入的hashkey不一定能在redis中找到对应的记录。