redis中不同value类型的存取操作方式

public class CacheController {

  public static void main(String[] args) throws Exception {

        CacheClientHA client = new CacheClientHA("cache", true);

    // value为普通String类型
    client.String().setex("string", 60, "aa");
    String res1 = client.String().get("string");

    // value为Hash类型
    HashMap<String, String> map = new HashMap<>();
    map.put("username", "lyc");
    map.put("pass", "123");
    client.Hash().hmset("hash", map);
    client.Key().expire("hash", 60);
    HashMap<String, String> res2 = (HashMap) client.Hash().hgetall("hash");

    // vlaue为List类型
    client.List().lpush("list", "lyc1");
    client.List().lpush("list", "lyc2");
    client.Key().expire("list", 60);
    List<String> res3 = client.List().lrange("list", 0, client.List().llen("list") - 1);

    // value为json字符串
    UserInfoDto userInfoDto = new UserInfoDto();
    userInfoDto.setUsername("lyc");
    userInfoDto.setPass("123");
    JSONObject jsonObject = JSONObject.fromObject(userInfoDto);
    client.String().setex("json", 60, jsonObject.toString());
    String res4 = client.String().get("json");
    UserInfoDto UserInfoDto2 = (UserInfoDto) JSONObject.toBean(JSONObject.fromObject(res4), UserInfoDto.class);

    // value为json数组字符串
    List<UserInfoDto> list = new ArrayList();
    UserInfoDto userInfoDto3 = new UserInfoDto();
    userInfoDto3.setUsername("lyc");
    userInfoDto3.setPass("123");
    UserInfoDto userInfoDto4 = new UserInfoDto();
    userInfoDto4.setUsername("lyc2");
    userInfoDto4.setPass("123");
    list.add(userInfoDto3);
    list.add(userInfoDto4);
    JSONArray jsonArray = JSONArray.fromObject(list);
    client.String().setex("jsonArray", 60, jsonArray.toString());
    String res5 = client.String().get("jsonArray");
    List<UserInfoDto> userInfoList = JSONArray.toList(JSONArray.fromObject(res5), new UserInfoDto(), new JsonConfig());

  }
}
原文地址:https://www.cnblogs.com/dali-lyc/p/7304957.html