Spring, Spring-data-redis integrated use

Source: Internet
Author: User

One, Redis is an open source, using the ANSI C language, support network, memory can be persisted based on the log-type, Key-value database, and provide a variety of language API.

From March 15, 2010 onwards, the development work of Redis is hosted by VMware. Since May 2013, the development of Redis has been sponsored by pivotal.

At present: I believe many people know the application and universality of Redis, because of its flexibility, let us in the use of more handy.

The basic usage of Spring-data-redis is described here, and it is necessary to rely on package Jedis support

Second, the deployment of Redis

1, the specific deployment configuration view: http://www.cnblogs.com/ll409546297/p/6993778.html

2, there are several places to note (mainly configuration redis.conf)

1) because I am the daemon thread startup, background self-booting. Need to modify daemonize to Yes

Daemonize Yes

2) because the cache server, is generally an external server, in use when you need to set the password and comment out the bind 127.0.0.1. Otherwise, the pool and password exceptions will be thrown when you use them.

Password settings: Requirepass 123456 (Find the relevant note to dismiss and change the password)

Comment out bind 127.0.0.1

3, the basic test will be able to (you can restart the test a bit)

4, the deployment of Redis here is basically complete.

Third, Pom.xml (Guide package)

<Dependency>      <groupId>Org.springframework.data</groupId>      <Artifactid>Spring-data-redis</Artifactid>      <version>1.8.4.RELEASE</version>    </Dependency>    <Dependency>      <groupId>Redis.clients</groupId>      <Artifactid>Jedis</Artifactid>      <version>2.9.0</version>    </Dependency>

Iv. Configuration Spring-redis.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"Xmlns:jee= "Http://www.springframework.org/schema/jee"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:util= "Http://www.springframework.org/schema/util"XMLNS:JPA= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.3.xsd Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/ Spring-jdbc-4.3.xsd Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/ Spring-jee-4.3.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.3.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/ Spring-jpa-1.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.3.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.3.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/uTil/spring-util-4.3.xsd " >      <!--Configure connection Pooling -   <BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig">           < Propertyname= "Maxtotal"value= "Ten"></ Property>          < Propertyname= "Maxidle"value= "Ten"></ Property>          < Propertyname= "Minidle"value= "2"></ Property>          < Propertyname= "Maxwaitmillis"value= "15000"></ Property>          < Propertyname= "Minevictableidletimemillis"value= "300000"></ Property>          < Propertyname= "Numtestsperevictionrun"value= "3"></ Property>          < Propertyname= "Timebetweenevictionrunsmillis"value= "60000"></ Property>          < Propertyname= "Testonborrow"value= "true"></ Property>          < Propertyname= "Testonreturn"value= "true"></ Property>          < Propertyname= "Testwhileidle"value= "true"></ Property>         </Bean>   <!--Connection Factory -    <BeanID= "Jedisconnfactory"class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory">           < Propertyname= "HostName"value= "192.168.5.100"/>           < Propertyname= "Port"value= "6379"/>           < Propertyname= "Password"value= "123456"/>           < Propertyname= "Usepool"value= "true"/>           < Propertyname= "Poolconfig"ref= "Jedispoolconfig"/>   </Bean>   <!--for data interaction -   <BeanID= "Redistemplate"class= "Org.springframework.data.redis.core.RedisTemplate">           < Propertyname= "ConnectionFactory"ref= "Jedisconnfactory"/>   </Bean></Beans>

V. Testing

Jedisshardinfo  New jedisshardinfo ("192.168.5.100", 6379);        Jedisshardinfo.setpassword ("123456"); // Password Authentication        New Jedis (jedisshardinfo);        Jedis.set ("fucking", "fucking");        System.out.println (Jedis.get ("fucking"));

Vi. Use of

@Autowired Private Redistemplate<string, string> redistemplate;redistemplate.opsforvalue (). Set ("test", "Test " = Redistemplate.opsforvalue (). Get ("test"); System.out.println (obj);

Note: Injections here are not necessarily the only one, can be different types

Spring, Spring-data-redis integrated use

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.