To connect the Redis cache steps via the Jedis client:
1. Add Jar Package:
POM Coordinates:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.0</version>
</dependency>
The jar package is as follows: Commons-pool2-2.3.jar Jedis-2.7.0.jar
2. Single Instance connection
To connect the Redis service by creating a single-instance Jedis object, the following code:
Single Instance connection Redis
@Test public
void Testjedissingle () {
Jedis Jedis = new Jedis ("192.168.101.3", 6379);
Jedis.set ("name", "Bar");
String name = Jedis.get ("name");
SYSTEM.OUT.PRINTLN (name);
Jedis.close ();
}
3. Connect using Connection Pool
Redis connections cannot be shared through single-instance connections, you can use connection pooling to share redis connections, increase resource utilization, and use Jedispool to connect to the Redis service with the following code:
@Test public
Void Pool () {
jedispoolconfig config = new Jedispoolconfig ();
Maximum number of connections
config.setmaxtotal (+);
Maximum connection idle number
Config.setmaxidle (2);
Jedispool pool = new Jedispool (config, "192.168.101.3", 6379);
Jedis Jedis = null;
Try {
Jedis = Pool.getresource ();
Jedis.set ("name", "Lisi");
String name = Jedis.get ("name");
SYSTEM.OUT.PRINTLN (name);
} catch (Exception ex) {
ex.printstacktrace ();
} finally{
if (Jedis! = null) {
//close connection
jedis.close ()
;
}}}
4. Jedis and Spring Integration
Configure Spring configuration file Applicationcontext.xml
<!--connection Pool configuration-<bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <!--maximum number of connections-->
; <property name= "Maxtotal" value= "$"/> <!--maximum idle connections--<property name= "Maxidle" value= "ten"/> &L t;! --Maximum number of connections per release--<property name= "Numtestsperevictionrun" value= "1024x768"/> <!--release connection scanning interval (ms)--< ;p roperty name= "Timebetweenevictionrunsmillis" value= "30000"/> <!--connection min idle time-<property name= "minevict Ableidletimemillis "value=" 1800000 "/> <!--how long the connection is idle and released when idle time > This value and idle connections > maximum idle Connections is released directly--<property n Ame= "Softminevictableidletimemillis" value= "10000"/> <!--maximum wait milliseconds to get a connection, less than 0: block indeterminate time, Default-1--<property Name= "Maxwaitmillis" value= "/>" <!--check validity when getting a connection, default false--<property name= "Testonborrow" value= " True "/> <!--check validity on idle, default false--<property name=" Testwhileidle "value=" true "/> < whether blocking when!--connection is exhausted , false to report abnormal, Ture blocked untilTimeout, default true--<property name= "blockwhenexhausted" value= "false"/> </bean> <!--Redis Standalone via connection pooling--&
Gt <bean id= "Jedispool" class= "Redis.clients.jedis.JedisPool" destroy-method= "Close" > <constructor-arg index= "0" ref= "jedispoolconfig" ></constructor-arg> <constructor-arg index= "1" value= "192.168.101.3" ></ Constructor-arg> <constructor-arg index= "2" value= "7001" ></constructor-arg> </bean>
Test code:
Private ApplicationContext ApplicationContext;
@Before public
void init () {
applicationcontext = new Classpathxmlapplicationcontext (
"classpath: Applicationcontext.xml ");
}
@Test public
void Testjedispool () {
Jedispool pool = (jedispool) applicationcontext.getbean ("Jedispool");
Try {
Jedis = Pool.getresource ();
Jedis.set ("name", "Lisi");
String name = Jedis.get ("name");
SYSTEM.OUT.PRINTLN (name);
} catch (Exception ex) {
ex.printstacktrace ();
} finally{
if (Jedis! = null) {
//close connection
jedis.close ()
;
}}}