3. JedisPool
- To get a Jedis instance, you need to get it from JedisPool
- Jedis instances need to be returned to JedisPool
- If Jedis is used incorrectly, it also needs to be returned to JedisPool
- See code for examples
1) JedisPoolUtil
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisPoolUtil {
private static volatile JedisPool jedisPool = null;// Volatile variables are not cached by the local thread, and reads and writes to them are direct operations on shared memory.
private JedisPoolUtil(a) {}
public static JedisPool getJedisPoolInstance(a)
{
if(null == jedisPool)
{
synchronized (JedisPoolUtil.class)
{
if(null == jedisPool)
{
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxActive(1000);
poolConfig.setMaxIdle(32);
poolConfig.setMaxWait(100*1000);
poolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(poolConfig,"127.0.0.1"); }}}return jedisPool;
}
public static void release(JedisPool jedisPool,Jedis jedis)
{
if(null! = jedis) { jedisPool.returnResourceObject(jedis); }}}Copy the code
(2) call
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class Test01 {
public static void main(String[] args) {
JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance();
Jedis jedis = null;
try
{
jedis = jedisPool.getResource();
jedis.set("k18"."v183");
} catch (Exception e) {
e.printStackTrace();
}finally{ JedisPoolUtil.release(jedisPool, jedis); }}}Copy the code
Configuration summary
JedisPool configuration parameters are mostly assigned by the corresponding items of JedisPoolConfig.
MaxActive: Controls how many jedis instances can be allocated to a pool, using pool.getResource(); If the value is -1, there is no limit; If the pool has already allocated maxActive jedis instances, the pool is in the exhausted state. MaxIdle: Controls the number of jedis instances in a pool that are in idle state. Whenustedaction: Action to be taken when all jedis instances in the pool have been allocated. By default, there are three. When_hausted_fail –> NoSuchElementException is thrown when there is no jedis instance. When_hausted_block –> blocked, or JedisConnectionException thrown when maxWait is reached; When_hausted_grow –> indicates that a new jedis instance is created, i.e. maxActive is useless. MaxWait: Indicates the maximum wait time when borrow a Jedis instance. If the wait time is exceeded, JedisConnectionException is thrown directly. TestOnBorrow: Whether to check connection availability when obtaining a Jedis instance (ping()); If true, the resulting Jedis instances are all available;
TestOnReturn: whether to check connection availability when a jedis instance is sent to the pool (ping());
TestWhileIdle: If true, an idle object evitor thread will scan the idle object. If validate fails, the object will be dropped from the pool. This item is only meaningful when timeBetweenEvictionRunsMillis is greater than 0;
TimeBetweenEvictionRunsMillis: it means the idle object evitor between two scans to sleep the number of milliseconds;
NumTestsPerEvictionRun: indicates the maximum number of objects scanned by the Idle Object evitor at a time.
MinEvictableIdleTimeMillis: an object at least stay in the idle state of the shortest time, and then you can be idle object evitor scans and expelling; This item is only meaningful when timeBetweenEvictionRunsMillis is greater than 0;
SoftMinEvictableIdleTimeMillis: on the basis of minEvictableIdleTimeMillis, joined at least minIdle target has had in the pool. If it is -1, Evicted does not expel any objects based on idle time. If minEvictableIdleTimeMillis > 0, then this setting meaningless, and is only meaningful when timeBetweenEvictionRunsMillis is greater than 0;
When lifo: borrowObject returns an object, DEFAULT_LIFO (last in first out, i.e. the most frequently used queue similar to cache) is adopted. If it is False, it indicates FIFO queue.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = The default Settings of JedisPoolConfig for some parameters are as follows: testWhileIdle=true minEvictableIdleTimeMills=60000 timeBetweenEvictionRunsMillis=30000 numTestsPerEvictionRun=-1