Spring integrated Redis, and how spring redistemplate are used

Source: Internet
Author: User
Tags aop hash redis set set xmlns

Required JAR Package
Spring-data-redis-1.6.2.release.jar
Jedis-2.7.2.jar (dependent on Commons-pool2-2.3.jar)
Commons-pool2-2.3.jar

Spring-redis.xml configuration file

<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/schem A/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/sche Ma/context/spring-context-3.0.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/sch Ema/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 "> <!--[redis-jedispoolconfig Configuration] (HTTP// 
        blog.csdn.net/liang_love_java/article/details/50510753)-<!--Jedis-2.7.2.jar dependent jar Package Commons-pool2-2.3.jar
        Jedis has implemented a resource pool based on Commons-pool2-2.3.jar itself. Configuration parameters See http://blog.csdn.net/liang_love_java/article/details/50510753--<bean id= "Jedispoolconfig" class= " Redis.clients.jedis.JedisPoolConfig "> <property name=" maxidle "value=" 1 "/> <property name=" Maxtotal "value=" 5 "/> <property name=" blockwhenexhausted "value=" true "/> <property name=" m Axwaitmillis "value=" 30000 "/> <property name=" Testonborrow "value=" true "/> </bean> & Lt;bean id= "Jedisconnectionfactory" class= "Org.springframework.data.redis.connection.jedis.JedisConnEctionfactory "> <property name=" hostName "value=" 10.1.8.200 "/> <property name=" Port "value= "6379"/> <property name= "poolconfig" ref= "Jedispoolconfig"/> <property name= "UsePool" value = "true"/> </bean> <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.sprin Gframework.data.redis.serializer.JdkSerializationRedisSerializer "/> </property> <proper Ty name= "Hashkeyserializer" > <bean class= "Org.springframework.data.redis.serializer.StringRedisSeria     
       Lizer "/> </property> <property name= "Hashvalueserializer" > <bean class= "org.springframewor K.data.redis.serializer.jdkserializationredisserializer "/> </property> </bean> </be

 Ans>

Test Code

Import Java.util.HashMap;

Import Java.util.Map;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import org.springframework.data.redis.core.HashOperations;
Import org.springframework.data.redis.core.ListOperations;
Import Org.springframework.data.redis.core.RedisTemplate;

Import org.springframework.data.redis.core.ValueOperations; public static void Main (string[] args) {Classpathxmlapplicationcontext appctx = new Classpathxmlapplicationcontex
        T ("Spring-redis.xml");
        Final redistemplate<string, object> redistemplate = Appctx.getbean ("Redistemplate", Redistemplate.class);
        Add a key valueoperations<string, object> value = Redistemplate.opsforvalue ();
        Value.set ("LP", "Hello word");
        Get the value of this key System.out.println (Value.get ("LP"));
        Add a hash set hashoperations<string, Object, object> hash = Redistemplate.opsforhash (); map<string,object> map = new Hashmap<
        String,object> ();
        Map.put ("Name", "LP");
        Map.put ("Age", "26");
        Hash.putall ("Lpmap", map);
        Get Map System.out.println (hash.entries ("Lpmap"));
        Add a list listoperations<string, object> list = Redistemplate.opsforlist ();
        List.rightpush ("Lplist", "LP");
        List.rightpush ("Lplist", "26");
        Output list 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", "26");
        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", "26", 1);
        Zset.add ("Lpzset", "178cm", 2); Output ordered set set System.out.println (Zset.rangebyscore ("LPZset ", 0, 2)); }
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.