1. 程式人生 > >reids 連線測試demo

reids 連線測試demo

    }           /**      * 判斷key是否存在      * @param key      * @return true OR false      */     public static Boolean exists(String key) throws Exception{         Boolean falg = false;         if (getRedis() == null) {             return falg;         }         falg = getRedis().exists(key);         closeRedis();         return falg;     }          /**      * 通過key判斷值得型別      * @param key      *      * @return      */     public static String getTypeByKey(String key) throws Exception{         String result = null;         if (getRedis() == null) {             return result;         }         result = getRedis().type(key);         closeRedis();         return result;     }               /**      * 根據key刪除key      * @param key key      * @return      * @throws Exception      */     public static Boolean delObjKey(String key) throws Exception {         Boolean flag = false;         if (key == null || key == "") {             return flag;         }         Long code = getRedis().del(key);         closeRedis();         if (code == 1L) {             flag = true;         }         return flag ;     }     /**      * 獲取所有key      * @return      * @throws Exception      */     public static Set<String> getAllKeys() throws Exception {         Set<String> keys = getRedis().keys("*");          closeRedis();         return keys;     }               /**      * 新增String型別的key      * @param key  key      * @throws Exception       */     public static void setObjStr(String key,String value) throws Exception {         getRedis().set(key, value);         closeRedis();     }          /**      * 獲取String型別的key的值      * @param key  key      * @return      * @throws Exception      */     public static String getObjStr(String key) throws Exception {         if (key == null || key == "") {             return null;         }         String value = getRedis().get(key);         closeRedis();         return value;     }          /**      * 將集合轉換為字串儲存      * @param key  key      * @param objList  儲存的集合      * @param activeTime  該物件的有效時間,單位為秒   如果為0 不設定過期時間      * @return      */     public static Boolean setArrayList(String key, List<Object> objList,             Integer activeTime) {         if (objList != null && key != null && key != "") {           //  getRedis().set(key, JSONArray.fromObject(objList).toString());             if (activeTime != null && activeTime > 0) {                 getRedis().expire(key, activeTime);             }             closeRedis();             return true;         }         return false;     }                /**      * key 設定生存時間,當 key 過期時(生存時間為 0 ),它會被自動刪除        * @param key       * @param seconds  以秒為單位      * @return 設定成功返回 1 當 key 不存在或者不能為 key 設定生存時間時返回 0        */     public static Long expireTime(String key, int seconds) throws Exception {         Long result = null;         if (getRedis() == null) {             return result;         }         result = getRedis().expire(key, seconds);         closeRedis();            return result;     }          /**      * 以秒為單位,返回給定 key 的剩餘生存時間(TTL, time to live)      * @param key      * @return 當 key 不存在時,返回 -2       * 當 key 存在但沒有設定剩餘生存時間時,返回 -1       * 否則,以秒為單位,返回 key 的剩餘生存時間      */     public static Long getExpireTime(String key) throws Exception {         Long result = null;         if (getRedis() == null) {             return result;         }         result = getRedis().ttl(key);         closeRedis();         return result;     }          public static void main(String[] args) throws Exception{         //位元組碼存物件         /*Gson gson = new Gson();                  User user = new User();         user.setAge(17);         user.setName("小白");                  User user2 = new User();         setObjStr("user", gson.toJson(user2));         user2 = gson.fromJson(getObjStr("user"), User.class) ; //json轉物件         System.out.println("名字:"+user2.getName()+"==========="+"年齡:"+user2.getAge());         List<User> userList =  new ArrayList<User>();         userList.add(user2);         userList.add(user);         setObjStr("userList", gson.toJson(userList));         List<User> l = new ArrayList<User>();         l = gson.fromJson(getObjStr("userList"), new TypeToken<List<User>>(){}.getType()) ;//把json轉換成list         System.err.println(l.size());         for (User user3 : l) {             System.out.println("名字:"+user3.getName()+"==========="+"年齡:"+user3.getAge());         }         Map<String, Object> paramMap = new HashMap<String, Object>();         paramMap.put("name", "小紅");         paramMap.put("age", 16);         setObjStr("paramMap", gson.toJson(paramMap));         System.out.println(gson.fromJson(getObjStr("paramMap"), new TypeToken<Map<String, Object>>(){}.getType()) );*/         /*if (user == null) {             System.out.println("獲取物件失敗,沒有對應的key");         }else{             System.out.println("名字:"+user.getName()+"==========="+"年齡:"+user.getAge());         }*/                           // 存String         setObjStr("userName", "小李");         System.out.println(getObjStr("userName"));                  //刪除key         //System.out.println(delObjKey("user"));                  //查詢所有key     /*    Iterator<String> it =  getAllKeys().iterator();         while (it.hasNext()) {             System.out.println(it.next());                      }*/         //判斷key是否存在         //System.out.println(exists("name"));                  //通過key獲取值的型別         //System.out.println(getTypeByKey("uList"));         //expireTime("name",500);         //System.err.println(getExpireTime("name"));     } }