zoukankan      html  css  js  c++  java
  • 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());
    
      }
    }
  • 相关阅读:
    投资人的能量往往大多远远不仅于此,他能站在不同的角度和高度看问题(要早点拿投资,要舍得让出股份)——最好不要让 Leader 一边做技术、一边做管理,人的能力是有限的,精力也是有限的
    汇编实现获取CPU信息
    Web service的学习资源
    重启网卡的几种方法(命令行,API,
    认知服务
    平台化项目多语言架构
    移动跨平台开发框架Ionic开发一个新闻阅读APP
    net core 1.0 实现负载多服务器单点登录
    canvas1
    asp.net core + angular2
  • 原文地址:https://www.cnblogs.com/dali-lyc/p/7304957.html
Copyright © 2011-2022 走看看