Spring Integrated Redis client Jedis (Java)

Source: Internet
Author: User

Jedis is a Redis Java client, and spring configures the Redis connection pool as a bean.

"Redis.clients.jedis.JedisPool", which is a Redis connection pool for stand-alone environments.

1. Maven imports the relevant package:

    <!--redis Dependencies--    <dependency>      <groupId>redis.clients</groupId>      < artifactid>jedis</artifactid>      <version>2.9.0</version>    </dependency>

2, the following is the configuration of the Redis connection pool in a single machine environment:

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " >

<!--introducing the properties configuration file for Jedis--
<!--If you have multiple data sources that need to be managed by <context:property-placeholder and do not want to be placed in a configuration file, be sure to add ignore-unresolvable= "true"-
<context:property-placeholder location= "Classpath:properties/redis.properties" ignore-unresolvable= "true"/ >

<!--related configuration--> for Jedis connection pool;
<bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" >
<!--new version is Maxtotal, the old version is maxactive-->
<property name= "Maxtotal";
<value>${ Redis.pool.maxactive}</value>
</property>
<property name= "Maxidle";
<value>${ Redis.pool.maxidle}</value>
</property>
<property name= "Testonborrow" value= "true"/>
<property name= "Testonreturn" value= "true"/>
</bean>

<bean id= "Jedispool" class= "Redis.clients.jedis.JedisPool" >
<constructor-arg name= "Poolconfig" ref= "Jedispoolconfig"/>
<constructor-arg name= "host" value= "${redis.host}"/>
<constructor-arg name= "Port" value= "${redis.port}" type= "int"/>
<constructor-arg name= "Timeout" value= "${redis.timeout}" type= "int"/>
<constructor-arg name= "Password" value= "${redis.password}"/>
<constructor-arg name= "Database" value= "${redis.database}" type= "int"/>
</bean>
</beans>

3, the corresponding Classpath:properties/redis.properties.xml is:

#最大分配的对象数redis. pool.maxactive=200# the maximum number of objects that can hold the Idel state redis.pool.maxidle=50redis.pool.minidle= 10redis.pool.maxwaitmillis=20000# when no object is returned in the pool, the maximum wait time redis.pool.maxwait=300# format: redis://:[password]@[server address]:[Port]/[db Index]redis.uri = Redis://:[email Protected]:6379/0redis.host = 127.0.0.1redis.port = 6379redis.timeout= 30000redis.password = 12345redis.database = 0

4, Redisutil

Package com.ctid.business.redis.util;

Import Javax.annotation.Resource;

Import org.springframework.stereotype.Component;

Import Redis.clients.jedis.Jedis;
Import Redis.clients.jedis.JedisPool;
@Component
public class Redisutil {

@Resource
Private Jedispool jedispool;//Injection Jedispool

public string get (string key) {//Gets the value of the specified key. If key does not exist, return nil
Jedis Jedis = Jedispool.getresource ();
String string = Jedis.get (key);
Jedis.close ();
return string;
}

public string Set (string key, String value) {//Set some string value
Jedis Jedis = Jedispool.getresource ();
String string = Jedis.set (key, value);
Jedis.close ();
return string;
}

public string Hget (string hkey, String key) {//Gets the value of the specified field in the hash table
Jedis Jedis = Jedispool.getresource ();
String string = Jedis.hget (hkey, key);
Jedis.close ();
return string;
}

Public long Hset (string hkey, String key, String value) {//To assign a value to a field in a hash table
Jedis Jedis = Jedispool.getresource ();
Long result = Jedis.hset (hkey, key, value);
Jedis.close ();
return result;
}

Public long incr (String key) {//Adds a number value stored in key, if key does not exist, then the value of key is first initialized to
0, and then perform the incr operation
Jedis Jedis = Jedispool.getresource ();
Long result = JEDIS.INCR (key);
Jedis.close ();
return result;
}

Public long expire (String key, int second) {//Set expiry time for key
Jedis Jedis = Jedispool.getresource ();
Long result = Jedis.expire (key, second);
Jedis.close ();
return result;
}

Public long TTL (String key) {//Returns the remaining expiration time of key in seconds
Jedis Jedis = Jedispool.getresource ();
Long result = Jedis.ttl (key);
Jedis.close ();
return result;
}

Public long del (String key) {//deleted by key
Jedis Jedis = Jedispool.getresource ();
Long result = Jedis.del (key);
Jedis.close ();
return result;
}

Public long Hdel (string hkey, String key) {//delete one or more specified fields in the hash table key
Jedis Jedis = Jedispool.getresource ();
Long result = Jedis.hdel (hkey, key);
Jedis.close ();
return result;
}

}

5. Redisutil Call

Package Com.ctid.business.redis.testIpv6;

Import Javax.annotation.Resource;

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.springframework.stereotype.Service;

Import Com.ctid.business.redis.util.RedisUtil;

@Service
public class TestIpv6 {

private static final Log LOGGER = Logfactory.getlog (Testipv6.class);
@Resource
Private Redisutil Redisutil;

public void TestIpv6 () {
Logger.info ("redis======================================");
String RESULT1 = redisutil.set ("Test", "1111");
Logger.info ("Redis Setting Value:" + result1);
String result2 = redisutil.get ("test");
Logger.info ("Redis Query Value:" + result2);

}

}

Spring Integrated Redis client Jedis (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.