Spring integrated Redis easy to use

Source: Internet
Author: User

1. Brief introduction

Redis is based on C language development.

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type).

Redis is a cache database (one-sided understanding) that can either cache or persist data to disk!

 2.pom.xml introducing the relevant jar (please note that there was an error with the jar version problem)

<dependency> <groupId>org.apache.commons</groupId> <artifactid>commons-pool2</ Artifactid> <version>2.2</version></dependency><dependency> <groupId> Org.springframework.data</groupid> <artifactId>spring-data-redis</artifactId> <version>    1.7.5.release</version></dependency><dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version></dependency>


3.spring-redis.xml configuration file, configuring key Bean Redistemplate

<?xml version= "1.0"  encoding= "UTF-8"? >  <beans xmlns= "http// Www.springframework.org/schema/beans "  xmlns:context=" Http://www.springframework.org/schema/context "  xmlns:p= "http://www.springframework.org/schema/p"   xmlns:mvc= "http://www.springframework.org/ Schema/mvc " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "  xmlns:tx="/HTTP/ Www.springframework.org/schema/tx " xmlns:util=" Http://www.springframework.org/schema/util "   xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"   xsi:schemalocation= "http// www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/ spring-beans-3.0.xsd   http://www.springframework.org/schema/context   http:// www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/ Schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    http://www.springframework.org/schema/tx    http:/ /www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/ Schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    http ://www.springframework.org/schema/util    http://www.springframework.org/schema/util/ Spring-util-3.0.xsd ">    <!--    <context: property-placeholder location= "Classpath:redis-config.properties"/>           -->             <bean id= "Jedispoolconfig"  class= "Redis.clients.jedis.JedisPoolConfig" >      <property name= "Maxidle"  value= "${redis.maxidle}"  />      <property name= "Maxtotal"  value= "${redis.maxtotal}"  />     <property name= "blockWhenExhausted"   Value= "true"  />     <property name= "Maxwaitmillis"  value= "${ Redis.maxwaitmillis} " />     <property name=" TestOnBorrow " value = "${redis.testonborrow}"  />     </bean>    <bean  id= "Jedisconnectionfactory"  class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory ">     < Property name= "HostName"  value= "${redis.hostname}"  />     < Property name= "Port"  value= "${redis.port}"/>     <property name= "Poolconfig"  ref= "Jedispoolconfig"  />     <property name= " Usepool " value=" true "/>   </bean>    &nbsP;<bean id= "Redistemplate"  class= "Org.springframework.data.redis.core.RedisTemplate" >       <property name= "ConnectionFactory"   ref= " Jedisconnectionfactory " />      <property name=" KeySerializer " >        <bean class= " Org.springframework.data.redis.serializer.StringRedisSerializer " />       </property>       <property name= "ValueSerializer" >         <bean class= " Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer " />       </property>      <property name= "HashKeySerializer" >         <bean class= " Org.springframework.data.redis.serializer.StrIngredisserializer "/>       </property>       <property name= "Hashvalueserializer" >         <bean class= "Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>        </property>    </bean>      </beans>



The configuration file used in the above Redis-config.properteis

Redis.maxidle=1redis.maxtotal=5redis.maxwaitmillis=30000redis.testonborrow=trueredis.hostname= 127.0.0.1redis.port=6379


The 4.redis has 4 key interfaces as follows


Private valueoperations<k, v> valueops;

Private listoperations<k, v> listops;

Private setoperations<k, v> setops;

Private zsetoperations<k, v> zsetops;



data types corresponding to Redis:string (String), hash (hash), list, set (set), and Zset (sorted set: Ordered set)


Use the following code:

Add string Valueoperations<string, string> value = this.redistemplate.opsforvalue (); Value.set ("Hello",  "nasty"); System.out.println (Value.get ("Hello"));//Add   a  hash collection hashoperations<string, object,  Object>  hash =redistemplate.opsforhash (); Hash.put ("Wal-Mart", "Fruit",  "apple"); Hash.put ("Wal-Mart", "beverage ", " Red Bull "); System.out.println (Hash.entries ("Wal-Mart"));//Add a list  collection listoperations<string, object> list  = redistemplate.opsforlist (); List.rightpush ("Course",  "Chinese"); List.rightpush ("Course",  " Englise "); System.out.println (List.range ("Lplist",  0, 1));//Add   a  set  set setoperations<string,  object> set = redistemplate.opsforset (); Set.add ("Lpset",  "LP"); Set.add ("LpSet",   Set.add ("Lpset",  "178cm");//Output  set  set System.out.println (Set.members ("Lpset");// Add an ordered  set  set zsetoperations<string, object> zset =  Redistemplate.opsforzset (); Zset.add ("Lpzset",  "LP",  0) Zset.add ("Lpzset",  "n",  2); Zset.add ("Lpzset",  "178cm",  1);//output order  set  set System.out.println (Zset.rangebyscore ("Lpzset", &NBSP;0,&NBSP;2));




This article is from the "Bula Juncun" blog, make sure to keep this source http://5148737.blog.51cto.com/5138737/1976501

Spring integrated Redis easy to 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.