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


The maximum # can keep the object idel state number


# when no return object pool, the maximum waiting time


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


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


#IP address


#port port number


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) {
This.load (MyProperties.class.getClassLoader (.GetResourceAsStream) ("redis.properties"));
Catch (IOException E) {}
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
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);