Redis Cache

    /*以下的方法都可以使用*/
    public List<PUser> select() {
        try {
            Jedis jedis = new Jedis();
            String key = "newss";
            //jackson内置的对象有序列化和反序列化的方法
            ObjectMapper objectMapper = new ObjectMapper();
            //判断Redis是否有这个key
            if (jedis.exists(key)) {
                //Gson的字符串转为List<Object>
                //new Gson().fromJson(jedis.get(key), new TypeReference<List<PUser>>() {}.getType());
                //jackson字符串转为List<Object>
                return objectMapper.readValue(jedis.get(key), new TypeReference<List<PUser>>() {
                });
            }
            //Redis当中没有,直接查询数据库
            List<PUser> pUsers = mapper.selectAll();
            jedis.set("newss", objectMapper.writeValueAsString(PUser.class));
            return pUsers;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

猜你喜欢

转载自www.cnblogs.com/YouAreABug/p/10182990.html
今日推荐