Home » NoSQL » Redis+linux custom pool (three)

Redis+linux custom pool (three)

 Redis+linux custom pool (three)

, import package:

1, jedis-2.8.0.jar

2, commons-collections-3.2.2.jar

3, commons-logging.jar

4, commons-pool2-2.4.2.jar


two: the pool is to create a redis connection pool, helps to speed up the access speed of redis
.
1, first create a redis.properties file stored inside the pool value

The number of objects allocated # maximum

Redis.pool.maxActive=1024

The maximum # can keep the object idel state number

Redis.pool.maxIdle=200

# when no return object pool, the maximum waiting time

Redis.pool.maxWait=1000

# when calling the borrow Object method, whether to check the validity of the

Redis.pool.testOnBorrow=true

# when calling the return Object method, whether to check the validity of the

Redis.pool.testOnReturn=true

#IP address

Redis.ip=192.168.14.215

#port port number

Redis.port=6379


2, create a MyProperties to load in the pool.
Import java.io.IOException;
Import java.util.Properties;
Public class MyProperties extends {Properties
Private static MyProperties mp;
Private (MyProperties) {
{try
This.load (MyProperties.class.getClassLoader (.GetResourceAsStream) ("redis.properties"));
Catch (IOException E) {}
(e.printStackTrace);
}
}
Public static MyProperties (getInstance) {
If (mp==null) {
Mp=new (MyProperties);
}
Return mp;
}
}
3, as long as the load test: because the pool once, all the static state
Import redis.clients.jedis.Jedis;
Import redis.clients.jedis.JedisPool;
Import redis.clients.jedis.JedisPoolConfig;
Public class {Test
Private static JedisPool pool;
/ / pool configuration
Static{
JedisPoolConfig config=new (JedisPoolConfig);
The MyProperties / / get the maximum value...
Config.setMaxTotal (Integer.valueOf (MyProperties.getInstance) (.GetProperty ("redis.pool.maxActive")));
Config.setMaxIdle (Integer.valueOf (MyProperties.getInstance) (.GetProperty ("redis.pool.maxIdle")));
Config.setMaxWaitMillis (Integer.valueOf (MyProperties.getInstance) (.GetProperty ("redis.pool.maxWait")));
Config.setTestOnBorrow (Boolean.valueOf (MyProperties.getInstance) (.GetProperty ("redis.pool.testOnBorrow")));
Config.setTestOnReturn (Boolean.valueOf (MyProperties.getInstance) (.GetProperty ("redis.pool.testOnReturn")));
Pool=new JedisPool (config, MyProperties.getInstance) (.GetProperty ("redis.ip"), (Integer.valueOf (MyProperties.getInstance) (.GetProperty ("redis.port"))));
}
Public static void main (String[] args) {
Jedis jedis= (pool.getResource); / / get the resources from the pool, that is to get a connection.
String keys= "name";
Jedis.del (keys);
Jedis.set (keys, YJL);
String value=jedis.get (keys);
System.out.println (value);
}
}

Latest