jedis还给连接池后,设置还存在吗?

Jedis作为客户端,连接Redis服务器。

JedisPool管理jedis连接,现在有一个问题,如果在获取一个jedis连接后,如果对连接进行了设置,比如选择了1号数据库(默认是0号),这个连接使用完后,返回池中。如果再次获取后,它连接的是1号数据库,还是默认的数据库?

从代码中可以看到,jedis在关闭连接时会把数据库设为0.

redis.clients.jedis.BinaryClient.java
这里写图片描述

参考:https://www.cnblogs.com/plf112233/p/6527902.html


pom文件中的jedis配置

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.6.2</version>
</dependency>

JedisClient.java

package com.pyc.redis;

import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisClient {
    private static Logger logger = Logger.getLogger(RedisClient.class);

    public static JedisPool jedisPool; // 池化管理jedis链接池

    static {
        // 读取相关的配置:redis.properties
        ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");

        int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
        int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
        int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));

        String ip = resourceBundle.getString("redis.ip");
        int port = Integer.parseInt(resourceBundle.getString("redis.port"));

        JedisPoolConfig config = new JedisPoolConfig();
        // 设置最大连接数
        config.setMaxTotal(maxActive);
        // 设置最大空闲数
        config.setMaxIdle(maxIdle);
        // 设置超时时间
        config.setMaxWaitMillis(maxWait);

        // 初始化连接池
        jedisPool = new JedisPool(config, ip, port);
    }

    /**
     * 向缓存中设置字符串内容
     * 
     * @param key
     *            key
     * @param value
     *            value
     * @return
     * @throws Exception
     */
    public static boolean set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }
//  
//  public static boolean set(String key, String value, Integer seconds) {
//      Jedis jedis = null;
//      try {
//          jedis = jedisPool.getResource();
//          jedis.set(key, value);
//          jedis.expire(key, seconds);
//          return true;
//      } catch (Exception e) {
//          e.printStackTrace();d
//          return false;
//      } finally {
//          if (jedis != null) { jedis.close(); }
//      }
//  }

//  public static boolean set(String key, Object value, Integer seconds) {
//      Jedis jedis = null;
//      try {
//          String objectJson = JSON.toJSONString(value);
//          jedis = jedisPool.getResource();
//          jedis.set(key, objectJson);
//          jedis.expire(key, seconds);
//          return true;
//      } catch (Exception e) {
//          e.printStackTrace();d
//          return false;
//      } finally {
//          if (jedis != null) { jedis.close(); }
//      }
//  }

    /**
     * 向缓存中设置对象
     * 
     * @param key
     * @param value
     * @return
     */
//  public static boolean set(String key, Object value) {
//      try (Jedis jedis = jedisPool.getResource()) {
//          String objectJson = JSON.toJSONString(value);
//          jedis.set(key, objectJson);
//          return true;
//      } catch (Exception e) {
//          return false;
//      }
//  }

    public static boolean expire(String key, Integer seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();  // 从池中获取一个Jedis对象
            jedis.expire(key, seconds);
            return true;
        } catch (Exception e) {
            logger.error(e+" expire(key:"+key+", seconds:"+seconds+")", e);
            return false;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

    /**
     * 删除缓存中得对象,根据key
     * 
     * @param key
     * @return
     */
    public static boolean del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
            return true;
        } catch (Exception e) {
            logger.error(e+" del(key:"+key+")", e);
            return false;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

    /**
     * 判断key值是否存在
     * @author zhouna
     * @data 2017年11月20日 上午11:29:01
     *
     * @param key
     * @return
     */
    public static boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            logger.error(e+" exists(key:"+key+")", e);
            return false;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

//  /**
//   * 根据key 获取内容
//   * 
//   * @param key
//   * @return
//   */
//  public static String get(String key) {
//      Jedis jedis = null;
//      try {
//          jedis = jedisPool.getResource();
//          return jedis.get(key);
//      } catch (Exception e) {
//          logger.error(e);
//          return null;
//      } finally {
//          if (jedis != null) { jedis.close(); }
//      }
//  }

    /**
     * 根据key 获取对象
     * 
     * @param key
     * @return
     */
    public static <T> T get(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            return JSON.parseObject(value, clazz);
        } catch (Exception e) {
            logger.error(e+" get(key:"+key+", clazz:"+clazz+")", e);
            return null;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

//  /**
//   * 根据key 获取对象
//   * 
//   * @param key
//   * @return
//   */
//  public static String getStr(String key) {
//      Jedis jedis = null;
//      try {
//          jedis = jedisPool.getResource();
//          return jedis.get(key);
//      } catch (Exception e) {
//          e.printStackTrace();d
//          return null;
//      } finally {
//          if (jedis != null) { jedis.close(); }
//      }
//  }

//  public static Map<String, String> getMap(String key) {
//      Jedis jedis = null;
//      try {
//          jedis = jedisPool.getResource();
//          String value = jedis.get(key);
//          return JSON.parseObject(value, Map.class);
//      } catch (Exception e) {
//          e.printStackTrace();d
//          return null;
//      } finally {
//          if (jedis != null) { jedis.close(); }
//      }
//  }

//  /**
//   * 根据key 获取对象
//   * 
//   * @param key
//   * @return
//   */
//  public static <T> List<T> getList(String key, Class<T> clazz) {
//      Jedis jedis = null;
//      try {
//          jedis = jedisPool.getResource();
//          String value = jedis.get(key);
//          return JSON.parseArray(value, clazz);
//      } catch (Exception e) {
//          logger.error(e);
//          return null;
//      } finally {
//          try {
//              if (jedis != null) { jedis.close(); }
//          } catch (Exception e) {
//              System.out.println("");
//          }
//      }
//  }

    public static <T> List<T> hgetList(String key, String field, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.hget(key, field);
            return JSON.parseArray(value, clazz);
        } catch (Exception e) {
            logger.error(e+" hgetList(key:"+key+", field:"+field+", clazz:"+clazz+")", e);
            return null;
        } finally {
            try {
                if (jedis != null) { jedis.close(); }
            } catch (Exception e) {
                System.out.println("");
            }
        }
    }

    public static void flushAll() {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.flushAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

    /**
     * 删除index号数据库中的所有数据
     * @author zhouna
     * @data 2018年3月5日 下午5:06:49
     *
     * @param index
     */
    public static void flushDB(int index) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(index);
            jedis.flushDB();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

//  public static void main(String[] args) {
//      Map<String, String> map = RedisClient.getMap("test", String.class);
//      System.out.println(map);
//  }

//  /** 
//     * json string convert to map with javaBean 
//     */  
//    public static <T> Map<String,T> json2map(String jsonStr,Class<T> clazz){  
//        Map<String,T> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, T>>() {});  
//       
//        if (map == null) return null;
//        for (Entry<String, T> entry : map.entrySet()) {  
//            JSONObject obj = (JSONObject) entry.getValue();  
//            map.put(entry.getKey(), JSONObject.toJavaObject(obj, clazz));  
//        }  
//        return map;  
//    } 
//    
//    public static <T> Map<String, T> getMap(String key, Class<T> clazz) {
//      Jedis jedis = null;
//      try {
//          jedis = jedisPool.getResource();
//          String jsonStr = jedis.get(key);
//          Map<String, T> result = null;
//          result = json2map(jsonStr, clazz);
//          return result;
//      } catch (Exception e) {
//          e.printStackTrace();d
//          return null;
//      } finally {
//          if (jedis != null) { jedis.close(); }
//      }
//  }

    /******************下面对hash操作*******************/
    public static boolean hdel(String key, String field) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hdel(key, field);
            return true;
        } catch (Exception e) {
            logger.error(e+" hdel(key:"+key+", field:"+field+")", e);
            return false;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

    public static boolean hexists(String key, String field) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hexists(key, field);
        } catch (Exception e) {
            logger.error(e+" hexists(key:"+key+", field:"+field+")", e);
            return false;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

    /**
     * 根据key 获取内容
     * 
     * @param key
     * @return
     */
    public static String hget(String key, String field) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hget(key, field);
        } catch (Exception e) {
            logger.error(e+" hget(key:"+key+", field:"+field+")", e);
            return null;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

    public static Map<String, String> hgetAll(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hgetAll(key);
        } catch (Exception e) {
            logger.error(e+" hgetAll(key:"+key+")", e);
            return null;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

    public static void hsetAll(String key, Map<String, String> map) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hmset(key, map);
        } catch (Exception e) {
            logger.error(e.getMessage()+" hsetAll(key:"+key+", map:"+map+")", e);
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

//  public static boolean hset(String key, String field, Object value, Integer seconds) {
//      Jedis jedis = null;
//      try {
//          String objectJson = JSON.toJSONString(value);
//          jedis = jedisPool.getResource();
//          jedis.hset(key, field, objectJson);
//          jedis.expire(key, seconds);
//          return true;
//      } catch (Exception e) {
//          logger.error(e+" hset(key:"+key+", field:"+field+", value:"+value+", seconds:"+seconds+")", e);
//          return false;
//      } finally {
//          if (jedis != null) { jedis.close(); }
//      }
//  }

    /**
     * 向缓存中设置对象
     * @author zhouna
     * @data 2017年11月30日 下午4:13:40
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public static boolean hset(String key, String field, Object value) {
        Jedis jedis = null;
        try {
            String objectJson = JSON.toJSONString(value);
            jedis = jedisPool.getResource();  // 从池中获取一个Jedis对象
            jedis.hset(key, field, objectJson);
            return true;
        } catch (Exception e) {
            logger.error(e+" hset(key:"+key+", field:"+field+", value:"+value+")", e);
            return false;
        } finally {
            if (jedis != null) { jedis.close(); }
        }
    }

//  /**
//   * 根据key 获取对象
//   * 
//   * @param key
//   * @return
//   */
//  public static <T> T hget(String key, String field, Class<T> clazz) {
//      Jedis jedis = null;
//      try {
//          jedis = jedisPool.getResource();
//          String value = jedis.hget(key, field);
//          return JSON.parseObject(value, clazz);
//      } catch (Exception e) {
//          e.printStackTrace();d
//          return null;
//      } finally {
//          if (jedis != null) { jedis.close(); }
//      }
//  }
}

猜你喜欢

转载自blog.csdn.net/familyshizhouna/article/details/79448434