table of Contents
Create a connection pool configuration file
Creating and using a connection pool Redis
Introduction Jedis use of
Jedis operation Redis is a Java implementation of the API set, not too much should be noted, you can refer to this blog, understand the use of Jedis operating Redis related API, Java operations using Jedis redis
Redis connection Pools
Redis connection pool, it can be said Jedis connection pool because the pool is to create a connection, use and release using the Jedis API Java implementation.
Redis is not just cache, he was a database, but is a rather special database only (NoSQL), so Redis connection pool and database connection pool (such as Druid, C3P0, DBCP), the principles are similar.
Create a connection pool configuration file
Create a configuration file, the file name and path at random, I've named redis_pool.properties, stored in the classpath directory of the project, as follows:
redis_maxTotal = 30 redis_maxIdle = 15 redis_minIdle = 5 redis_ip = 127.0.0.1 redis_port = 6379
Creating and using a connection pool Redis
cn.ganlixin.test Package; Import java.io.IOException; Import a java.io.InputStream; Import the java.util.Properties; Import redis.clients.jedis.Jedis; Import redis.clients.jedis.JedisPool; Import redis.clients .jedis.JedisPoolConfig; / ** * learning Jedis connection pool usage * / public class TestJedisPool { public static void main (String [] args) throws IOException { // read Redis pool configuration file (located in the directory classpath) the InputStream = TestJedisPool.class.getClassLoader _is () the getResourceAsStream ( "redis_pool.properties");. the Properties The props = new new the Properties (); props.load (_is); // read the configuration item int maxTotal = Integer.parseInt (props.getProperty ( "redis_maxTotal")); maxIdle the Integer.parseInt = int (props.getProperty ( "redis_maxIdle")); int = MinIdle the Integer.parseInt (props.getProperty ( "redis_minIdle")); String = props.getProperty IP ( "redis_ip"); int Port = Integer .parseInt (props.getProperty ( "redis_port")); // set parameters Redis Pool JedisPoolConfig poolConfig new new JedisPoolConfig = (); poolConfig.setMaxTotal (maxTotal); poolConfig.setMaxIdle (maxIdle); poolConfig.setMinIdle (MinIdle); / / Create jedis connection pool JedisPool jedisPool = new new JedisPool (poolConfig, IP, Port); // Get jedis object from the connection pool jedis jedis jedisPool.getResource = (); // operated jedis.set ( "name", "beyond "); // return connection jedis.close (); // close the connection pool jedisPool.close (); } }
The above demonstrates how the use of Jedis connection pool, but we usually project is almost impossible to do so, we generally will Redis and Spring integration, and configure the connection pool Redis, can refer to