|
@@ -0,0 +1,213 @@
|
|
|
|
+package com.thinkgem.jeesite.modules.gsq.util;
|
|
|
|
+
|
|
|
|
+import java.util.Set;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+
|
|
|
|
+import redis.clients.jedis.Jedis;
|
|
|
|
+import redis.clients.jedis.JedisPool;
|
|
|
|
+import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Redis工具类操作
|
|
|
|
+ * @ClassName: RedisUtil
|
|
|
|
+ * @Description: TODO(这里用一句话描述这个类的作用)
|
|
|
|
+ * @author zhangqifei
|
|
|
|
+ * @date 2018年4月3日 下午12:19:59
|
|
|
|
+ */
|
|
|
|
+public class RedisUtil {
|
|
|
|
+
|
|
|
|
+ //Redis服务器IP
|
|
|
|
+ private static String ADDR = "101.200.40.164";
|
|
|
|
+ //Redis的端口号
|
|
|
|
+ private static int PORT = 12301;
|
|
|
|
+ //访问密码
|
|
|
|
+ private static String AUTH = "tripto!@#$1803";
|
|
|
|
+ //可用连接实例的最大数目,默认值为8;
|
|
|
|
+ //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
|
|
|
|
+ private static int MAX_ACTIVE = 50;
|
|
|
|
+ //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
|
|
|
|
+ private static int MAX_IDLE = 10;
|
|
|
|
+ //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
|
|
|
|
+ private static int MAX_WAIT = 10000;
|
|
|
|
+ private static int TIMEOUT = 10000;
|
|
|
|
+ //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
|
|
|
|
+ private static boolean TEST_ON_BORROW = true;
|
|
|
|
+ private static JedisPool jedisPool = null;
|
|
|
|
+ /**
|
|
|
|
+ * 初始化Redis连接池
|
|
|
|
+ */
|
|
|
|
+ public static void Init(String h,int p,String a,int max_active,int max_idle,int max_wait,int timeout){
|
|
|
|
+ try {
|
|
|
|
+ ADDR = h;
|
|
|
|
+ PORT = p;
|
|
|
|
+ AUTH = a;
|
|
|
|
+ MAX_ACTIVE = max_active;
|
|
|
|
+ MAX_IDLE = max_idle;
|
|
|
|
+ MAX_WAIT = max_wait;
|
|
|
|
+ TIMEOUT = timeout;
|
|
|
|
+
|
|
|
|
+ JedisPoolConfig config = new JedisPoolConfig();
|
|
|
|
+ config.setMaxTotal(MAX_ACTIVE);
|
|
|
|
+ config.setMaxIdle(MAX_IDLE);
|
|
|
|
+ config.setMaxWaitMillis(MAX_WAIT);
|
|
|
|
+ config.setTestOnBorrow(TEST_ON_BORROW);
|
|
|
|
+ jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取Jedis实例
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public synchronized static Jedis getJedis() {
|
|
|
|
+ try {
|
|
|
|
+ if (jedisPool != null) {
|
|
|
|
+ Jedis resource = jedisPool.getResource();
|
|
|
|
+ return resource;
|
|
|
|
+ } else {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 释放jedis资源
|
|
|
|
+ * @param jedis
|
|
|
|
+ */
|
|
|
|
+ public static void returnResource(final Jedis jedis) {
|
|
|
|
+ if (jedis != null) {
|
|
|
|
+ jedisPool.returnResource(jedis);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 入Redis
|
|
|
|
+ * @param key 键
|
|
|
|
+ * @param value 值
|
|
|
|
+ * @param duration 过期时间(秒)
|
|
|
|
+ */
|
|
|
|
+ public static void addRedis(String key,String value,int duration) {
|
|
|
|
+ Jedis jedis = getJedis();
|
|
|
|
+ jedis.set(key, value);
|
|
|
|
+ jedis.expire(key,duration);
|
|
|
|
+ returnResource(jedis);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 入Redis
|
|
|
|
+ * @param key 键
|
|
|
|
+ * @param value 值
|
|
|
|
+ */
|
|
|
|
+ public static void addRedis(String key,String value) {
|
|
|
|
+ Jedis jedis = getJedis();
|
|
|
|
+ jedis.set(key, value);
|
|
|
|
+ returnResource(jedis);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读取Redis
|
|
|
|
+ * @param key
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getRedis(String key) {
|
|
|
|
+ Jedis jedis = getJedis();
|
|
|
|
+ String info = jedis.get(key);
|
|
|
|
+ returnResource(jedis);
|
|
|
|
+ return info;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 统计当前缓存数量
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static int countKey() {
|
|
|
|
+ int num = 0 ;
|
|
|
|
+ Jedis jedis = getJedis();
|
|
|
|
+ num = jedis.keys("*").size();
|
|
|
|
+ returnResource(jedis);
|
|
|
|
+ return num;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 删除key,可以是一个,也可以是多个key
|
|
|
|
+ * @param keys
|
|
|
|
+ */
|
|
|
|
+ public synchronized static void deleteKey(String ... keys ){
|
|
|
|
+ getJedis().del(keys);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除匹配的key<br>
|
|
|
|
+ * 如以my为前缀的则 参数为"my*"
|
|
|
|
+ * @param key
|
|
|
|
+ */
|
|
|
|
+ public synchronized static void deleteKeys(String pattern){
|
|
|
|
+ //列出所有匹配的key
|
|
|
|
+ Set<String> keySet = getJedis().keys(pattern);
|
|
|
|
+ if(keySet == null || keySet.size()<=0){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String keyArr[] = new String[keySet.size()];
|
|
|
|
+ int i =0;
|
|
|
|
+ for (String keys : keySet) {
|
|
|
|
+ keyArr[i] = keys;
|
|
|
|
+ i++;
|
|
|
|
+ }
|
|
|
|
+ deleteKey(keyArr);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 匹配所有KEY
|
|
|
|
+ * 如以my为前缀的则 参数为"my*"
|
|
|
|
+ * @param key
|
|
|
|
+ */
|
|
|
|
+ public synchronized static String[] GetKeys(String pattern){
|
|
|
|
+ //列出所有匹配的key
|
|
|
|
+ Set<String> keySet = getJedis().keys(pattern);
|
|
|
|
+ if(keySet == null || keySet.size()<=0){
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ String keyArr[] = new String[keySet.size()];
|
|
|
|
+ int i =0;
|
|
|
|
+ for (String keys : keySet) {
|
|
|
|
+ keyArr[i] = keys;
|
|
|
|
+ i++;
|
|
|
|
+ }
|
|
|
|
+ return keyArr;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ /*String[] sr = RedisUtil.GetKeys("CONTRO_10_Policy_*");
|
|
|
|
+ for (int i = 0; i < sr.length; i++) {
|
|
|
|
+ System.out.println(sr[i]+"\t"+RedisUtil.getRedis(sr[i]).length());
|
|
|
|
+ }*/
|
|
|
|
+
|
|
|
|
+ String redis_cache_address = "101.200.40.164";
|
|
|
|
+ int redis_cache_port = 12301;
|
|
|
|
+ String redis_cache_pwd = "tripto!@#$1803";
|
|
|
|
+ int redis_cache_max_active = 50;
|
|
|
|
+ int redis_cache_max_idle = 10;
|
|
|
|
+ int redis_cache_max_wait = 10000;
|
|
|
|
+ int redis_cache_timeout = 10000;
|
|
|
|
+ RedisUtil.Init(redis_cache_address,redis_cache_port,redis_cache_pwd,redis_cache_max_active,redis_cache_max_idle,redis_cache_max_wait,redis_cache_timeout);
|
|
|
|
+
|
|
|
|
+ RedisUtil.deleteKeys("location_*");
|
|
|
|
+
|
|
|
|
+ System.out.println(RedisUtil.countKey());
|
|
|
|
+
|
|
|
|
+ JSONArray array = DBOPHelper.executeQueryJSON("SELECT id, province_id, province_name, city_id, city_name, district_id, district_name FROM oa_area");
|
|
|
|
+ for (int i = 0; i < array.size(); i++) {
|
|
|
|
+ JSONObject json = array.getJSONObject(i);
|
|
|
|
+ RedisUtil.addRedis("location_"+json.getString("district_id"), json.toJSONString());
|
|
|
|
+ System.out.println(i);
|
|
|
|
+ }
|
|
|
|
+ System.out.println(RedisUtil.countKey());
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|