Java Connection Redis
One, import jar package
What are the commands for Redis, Jedis? How to set up a firewall
Run the following code on Linux:
Single instance: Jedis instance:
PackageCom.jedis.demo;Importorg.junit.Test;ImportRedis.clients.jedis.Jedis; Public classDemo1 {/** Single Instance connection to Redis database **/@Test Public voidrun () {//parameters: IP address, port numberJedis jedis=NewJedis ("192.168.239.137", 6379); Jedis.set ("Name", "Zhang San"); System.out.println (The value of "name" is: "+jedis.get (" name ")); }}
Jedis Connection Pool
/** Jedis Connection Pool **/@Test Public voidrun2 () {//1. Setting the connection pool configuration object jedispoolconfig config=new jedispoolconfig (); //set the maximum number of connections in the pool "optional" Config.setmaxtotal (); //set the maximum number of connections retained in the pool when idle "optional" Config.setmaxidle (ten); //Setting up Connection Objects jedispool pool=new jedispool (config, "192.168.239.137", 6379); //get the Connection object in the pool Jedis jedis= Pool.getresource (); System.out.println (The value of "name" is: "+jedis.get (" name ")); //connect to the return pool jedis.close (); }
Extract Connection Pooling Tool
To facilitate the use of connection pooling, extract the connection pooling tool:
PackageCom.jedis.demo;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig; Public classJedisutill {//Define a Connection pool object () Private Final StaticJedispool POOL; //initializing a connection pool object inside a static block of code Static { //1. Setting the connection pool configuration objectJedispoolconfig config=NewJedispoolconfig (); //set the maximum number of connections in the pool "optional"Config.setmaxtotal (50); //set the maximum number of connections retained in the pool when idle "optional"Config.setmaxidle (10); //Setting up Connection ObjectsPool=NewJedispool (config, "192.168.239.137", 6379); } /** Get connections from the pool **/ Public StaticJedis Getjedis () {returnPool.getresource (); }}
Redis Learning (5)-jedis (Java Operations Redis database technology)