【发布时间】:2016-02-28 02:55:39
【问题描述】:
我对 redis 比较陌生。 我正在设计一个rest API,以获取redis db中的整个数据集。
我使用下面的代码来获取所有的键
公共地图 getAllRedisData() {
Map<Object, Object> map = new HashMap<>();
Set<byte[]> keys = redisService.getTemplate().getConnectionFactory().getConnection().keys("*".getBytes());
Iterator<byte[]> it = keys.iterator();
while(it.hasNext()){
byte[] data = (byte[])it.next();
// String key = new String(data,0,data.length);
Object key = null;
try {
key = getObjectFromByteArray(data);
System.out.println(key);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return map;
}
private Object getObjectFromByteArray(byte[] bytes) throws IOException, ClassNotFoundException
{
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new ObjectInputStream(bis)) {
return in.readObject();
}
}
我能够获取所有键,但无法检索所有键的值。下面是方法 "对象值 = redisService.getTemplate().opsForValue().get("");"
我猜这是因为某些键的数据类型不同。
如果我必须修改/改变我的方法,请告诉我。
【问题讨论】:
标签: java spring-mvc redis