public class RedisUtlis {
//依赖注入jedis对象
// 存入数据到redis
public static void saveJsonData(String key, List<Setmeal> values) throws Exception {
JedisPool jedisPool = new JedisPool();
//获取jieis连接
Jedis jedis = jedisPool.getResource();
//判断key是否存在
// 把传入的对象转换成json字符串
String jsonData = JSON.toJSONString(values);
//存入到redis
jedis.set(RedisConst.SETMEAL_DATA_Setmeal,jsonData);
//释放资源
jedis.close();
}
// 获取缓存数据
public static List<Setmeal> getJsonData(String key) throws Exception{
JedisPool jedisPool = new JedisPool();
Jedis jedis = jedisPool.getResource();
String s = jedis.get(key);
// 把字符串转换回集合对象
try {
//把数据转换成数组
JSONArray jsonArray = JSON.parseArray(s);
//数组转换成字符串
String js= JSONObject.toJSONString(jsonArray);
//字符串转换成list
List<Setmeal> setmeals = JSONObject.parseArray(js,Setmeal.class);
return setmeals;
}catch (Exception e){
e.printStackTrace();
jedis.close();
return null;
}
}
}