• 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());
    
      }
    }
  • 相关阅读:
    Dynamics CRM for Outlook问题集
    Dynamics CRM
    VMWare安装CentOS-6.3-x86_64-minimal和LAMP
    Microsoft Dynamics CRM Update Rollup
    Reporting Service 迁移: 从2005到2008
    [转]非常好的vsftpd安装于配置
    关闭水滴直播平台 周鸿祎曾态度强硬
    贾跃亭此前曾公开表示,“法拉第未来计划于2
    爆出的法拉第未来(Faraday Future,以下简称“FF”)
    YII2笔记之三
  • 原文地址:https://www.cnblogs.com/dali-lyc/p/7304957.html
Copyright © 2020-2023  润新知