Bläddra i källkod

Redis工具类更新

xian 6 år sedan
förälder
incheckning
6fca136ca0
1 ändrade filer med 51 tillägg och 7 borttagningar
  1. 51 7
      watero-common-tool/src/main/java/com/iamberry/redis/RedisUtils.java

+ 51 - 7
watero-common-tool/src/main/java/com/iamberry/redis/RedisUtils.java

@@ -2,11 +2,13 @@ package com.iamberry.redis;
 
 import com.alibaba.fastjson.JSONObject;
 import com.iamberry.wechat.tools.NameUtils;
+import com.iamberry.wechat.tools.payUtil.DatetimeUtil;
 import org.apache.log4j.Logger;
 import redis.clients.jedis.Jedis;
 import redis.clients.jedis.JedisPool;
 import redis.clients.jedis.JedisPoolConfig;
 
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -81,7 +83,7 @@ public class RedisUtils {
         } catch (Exception e) {
             logger.error("", e);
         } finally {
-            getColse(jedis);
+            colse(jedis);
         }
         return false;
     }
@@ -100,7 +102,7 @@ public class RedisUtils {
         } catch (Exception e) {
             logger.error("", e);
         } finally {
-            getColse(jedis);
+            colse(jedis);
         }
         return false;
     }
@@ -120,7 +122,7 @@ public class RedisUtils {
         } catch (Exception e) {
             logger.error("", e);
         } finally {
-            getColse(jedis);
+            colse(jedis);
         }
         return null;
     }
@@ -138,7 +140,7 @@ public class RedisUtils {
         } catch (Exception e) {
             logger.error("", e);
         } finally {
-            getColse(jedis);
+            colse(jedis);
         }
         return null;
     }
@@ -158,7 +160,7 @@ public class RedisUtils {
         } catch (Exception e) {
             logger.error("", e);
         } finally {
-            getColse(jedis);
+            colse(jedis);
         }
         return null;
     }
@@ -176,18 +178,60 @@ public class RedisUtils {
         } catch (Exception e) {
             logger.error("", e);
         } finally {
-            getColse(jedis);
+            colse(jedis);
         }
         return false;
     }
 
+
+    /**
+     * 设置过期时间
+     * @param key
+     * @param date
+     */
+    public static void expire(String key, Date date) {
+        Jedis jedis = null;
+        try {
+            jedis = getJedis();
+
+            if (null != date) {
+                long nowTime = System.currentTimeMillis();
+                long dateTime = date.getTime();
+                if (dateTime > nowTime) {
+                    jedis.expire(key, (int)(dateTime - nowTime) / 1000);
+                }
+            }
+        }catch (Exception e){
+            logger.error("", e);
+        }finally {
+            colse(jedis);
+        }
+    }
+
     /**
      * 关闭连接
      * @param jedis
      */
-    private static void getColse(Jedis jedis) {
+    private static void colse(Jedis jedis) {
         if(jedis != null) {
             jedis.close();
         }
     }
+
+    public static void main(String[] args) {
+        // 判断Redis是否存在当前月的缓存信息
+        String key = "weixiu_total_" + "201905";
+        String total = get(key);
+        String no = null;
+        if (total == null) {
+            // 本月没有生成过数据
+            no = "0001";
+            // 写入下次的使用数据
+            put(key, "0002");
+            // 设置31天后过期
+            expire(key, new Date(System.currentTimeMillis() + 2678400000L));
+        } else {
+            no = total;
+        }
+    }
 }