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

Posted dali_lyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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());

  }
}

 

以上是关于redis中不同value类型的存取操作方式的主要内容,如果未能解决你的问题,请参考以下文章

字典类型

Python中操作Redis

redis之hashes类型及操作

redis val是存取字典

redis里怎么知道key的value大小?或者按照value的大小排序

系统学习redis之四——redis数据类型之hash类型及操作