Spring-data-redis usage

Source: Internet
Author: User

One, jar package support


<!--Redis support--><dependency><groupid>redis.clients</groupid><artifactid>jedis</ Artifactid><version>2.1.0</version></dependency><dependency><groupid> Org.springframework.data</groupid><artifactid>spring-data-redis</artifactid><version> 1.1.0.release</version></dependency><!--Redis Support--



Second, spring configuration



<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance "xmlns:util=" Http://www.springframework.org/schema/util "Xmlns:context = "Http://www.springframework.org/schema/context" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-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/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframework.org/schema/util/HTTP Www.springframework.org/schema/util/spring-util-3.0.xsdhttp://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-3.0.xsd "> <!--configuring Jredis connection pooling--><bean id=" Jedispool"class=" Redis.clients.jedis.JedisPoolConfig "> <property name=" maxactive "value=" + "/> <prope Rty name= "Maxidle" value= "/>" <property name= maxwait "value="/> "<property" name= testOn Borrow "value=" true "/> </bean><bean id=" jedisconnectionfactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <property name=" hostName "value=" $ {redis.host} "/> <property name=" port "value=" ${redis.port} "/> <property name=" Poolconfig "ref= "Jedispool"/> </bean> <bean id= "redistemplate" class= "Org.springframework.data.redis.core.RedisTemplat E "> <property name=" connectionfactory "ref=" jedisconnectionfactory "></property> </bean> < Bean id= "stringredistemplate" class= "Org.springframework.data.redis.core.StringRedisTemplate" > <property Name= "ConnectionFactory" ref= "Jedisconnectionfactory" ></property> </bean> </beans> 



Three, Unit test configuration



Package Com.iflashbuy.service.test.base;import Org.junit.runner.runwith;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.abstracttransactionaljunit4springcontexttests;import Org.springframework.test.context.junit4.springjunit4classrunner;import org.springframework.test.context.transaction.TransactionConfiguration; @ContextConfiguration (locations = {" Classpath:app-*.xml "}) @TransactionConfiguration (Defaultrollback = True) @RunWith (Springjunit4classrunner.class) public class Basetest extends Abstracttransactionaljunit4springcontexttests {}



Iv. Code



Package Com.iflashbuy.service.limanman;import Javax.inject.inject;import org.apache.commons.lang.StringUtils; Import Org.junit.assert;import Org.junit.test;import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.data.redis.core.redistemplate;import Org.springframework.data.redis.core.stringredistemplate;import Com.iflashbuy.service.product.dto.ProductDTO; Import Com.iflashbuy.service.test.base.basetest;public class Redistest extends Basetest {@InjectStringRedisTemplate        Strredistemplate, @Autowired private redistemplate<string, productdto> redistemplate; @Testpublic void Teststringredistemplate () {Strredistemplate.opsforvalue (). Append ("name", "Fengshu"); String name = Strredistemplate.opsforvalue (). Get ("name"); Logger.info (name); Assert.asserttrue (Stringutils.isnotempty (name)); Strredistemplate.delete ("name"); name = Strredistemplate.opsforvalue (). Get ("name"); Assert.asserttrue (Stringutils.isempty (name));} @Testpublic void Testredistemplate () {PrOductdto productdto = new Productdto ();p Roductdto.setid ("1"); Redistemplate.opsforset (). Add (Productdto.getid (), productdto);p roductdto = Redistemplate.opsforset (). Pop ("1"); Assert.asserttrue ("1". Equals (Productdto.getid ()));}}


V. Stringredistemplate and Redistemplate differences

Redistemplate supports Pojo objects, The stringredistemplate Key-value pair operates on a string.



Spring-data-redis usage

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.