redis的使用方式
@Override
public ReturnDataForLayui getList(Device device) throws IOException {
PageHelper.startPage(device.getPage(), device.getLimit());
ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
String[] keys = new String[]{"device:",
"page:" + device.getPage() + ":",
"limit:" + device.getLimit() + ":",
"name:", "typeId:", "factory:"
};
if (device.getName() != null && !device.getName().isEmpty()) {
keys[2] = "name:" + device.getName() + ":";
}
if (device.getTypeId() != null && device.getTypeId() > 0) {
keys[3] = "typeId:" + device.getTypeId() + ":";
}
if (device.getFactory() != null && !device.getFactory().isEmpty()) {
keys[4] = "typeId:" + device.getTypeId() + ":";
}
String deviceKey = "";
for (String key : keys) {
deviceKey += key;
}
List<Device> list = null;
String devicekeystr = (String) valueOperations.get(deviceKey);
if (devicekeystr != null && devicekeystr.length() > 0) {
list = JsonUntils.jsontoList(devicekeystr, Device.class);
if (list != null) {
PageInfo<Device> info = new PageInfo<>(list);
return ReturnDataForLayui.success(list, info.getTotal());
}
}
list = deviceMapper.getList(device);
if (list != null && !list.isEmpty()) {
valueOperations.set(deviceKey, JsonUntils.objectToJsonString(list), 2, TimeUnit.HOURS);
PageInfo<Device> info = new PageInfo<>(list);
return ReturnDataForLayui.success(list, info.getTotal());
}
return ReturnDataForLayui.fail("无数据");
}