Working with Redis using Spring Data Redis (standalone version)

Source: Internet
Author: User
Tags connection pooling serialization

Jedis is a Java-connected Redis client, and spring is encapsulated based on Jedis, providing a concise way to operate Redis. That's spring Data Redis. In fact, later research found that Spring Data Redis integration more than jedits this one, there are many, which can be specified by injecting the connection factory.

The following steps are required to use spring Data Redis:

1, first establish a connection factory, this connection factory is used to set IP, port, account password and so on.

2. Set up session by connecting factory.

3, then inject the session into the code to use.

Implementation steps:

1, POM

<Dependency>              <groupId>Org.springframework.data</groupId>              <Artifactid>Spring-data-redis</Artifactid>              <version>${spring-data-redis-version}</version>  </Dependency>  <Dependency>              <groupId>Redis.clients</groupId>              <Artifactid>Jedis</Artifactid>              <version>${redis.clients-jedis-version}</version>   </Dependency>  
The Spring-data-redis needs to rely on the Jedis package, in fact the Spring-data-redis Pom file automatically contains the Jedis configuration. http://www.mvnrepository.com/find the desired version here.

2. jedisconnectionfactory Establish Redis Connection factory

Similar to database connection pooling, Redis clients also establish a connection factory

Import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;  @Bean  public  jedisconnectionfactory jedisconnectionfactory () {          new  Jedisconnectionfactory ();            Connfactory.sethostname ("127.0.0.1");          Connfactory.setport (6379);          Connfactory.setusepool (true); // using connection pooling          return connfactory;  }

4, Redis's redistemplate

With the Redis connection factory, a specific Redis session is necessary.

Importorg.springframework.data.redis.core.RedisTemplate; ImportOrg.springframework.data.redis.serializer.StringRedisSerializer; @Bean PublicRedistemplate<string, string>Redis () {redistemplate<string, string> redistemplate =NewRedistemplate<string, string>();          Redistemplate.setconnectionfactory (Jedisconnectionfactory ()); Redistemplate.setkeyserializer (NewStringredisserializer ());//the serialization adapter for keyRedistemplate.setvalueserializer (NewStringredisserializer ());//the value of the serialization adapter can also be written by itself, most of the scene stringredisserializer enough to meet the requirements.           returnredistemplate; }

4. Operation of Redis

Import org.springframework.beans.factory.annotation.Autowired;   Import org.springframework.data.redis.core.RedisTemplate;   Import org.springframework.stereotype.Component;    @Component  Publicclass  Cachedemo {      
Private Redistemplate<string, string> redis; Public void Set (String key,string value) { redis.opsforvalue (). Set (key, value); } }
5. Operation

Redis.opsforvalue (): encapsulation operation string

Redis.opsforlist (): Package Operation list

Redis.opsforset (): encapsulation operation set

Redis.opsforzset (): encapsulation operation Sorted Set

Redis.opsforhash (): Package Operation Hash

6. XML-based configuration

The above is an annotation-based way to inject the connection factory and session, if it is an XML-based configuration, this can be set.

    <BeanID= "Jedisconnectionfactory"class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        < Propertyname= "HostName"value= "${redis1.host}" />        < Propertyname= "Port"value= "${redis1.port}" />    </Bean>        <!--<bean id= "redistemplate" class= "org.springframework.data.redis.core.StringRedisTemplate" > <property n Ame= "ConnectionFactory" ref= "jedisconnectionfactory"/> </bean> -        <BeanID= "Redisops"class= "Com.xjj.spring.data.XjjStringRedisOps">        < Propertyname= "ConnectionFactory"ref= "Jedisconnectionfactory" />    </Bean>

Description: Xjjstringredisops is the session in its own package

Reference:

http://haoran-10.iteye.com/blog/2261703 (the above content to turn from this article, observe the last sentence, looks like this blogger some worries!) )

Http://www.itkeyword.com/doc/240592287730467262/redis-spring-data-jedisjavajunit

Working with Redis using Spring Data Redis (standalone version)

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.