6、redis之使用spring-data-redis的Template
POM:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.yzl</groupId> 6 <artifactId>redis.first</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 10 <properties> 11 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 12 </properties> 13 14 <dependencies> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <version>4.9</version> 19 <scope>test</scope> 20 </dependency> 21 22 <dependency> 23 <groupId>redis.clients</groupId> 24 <artifactId>jedis</artifactId> 25 <!-- 2.7.1在和spring的spring-data-redis 1.4.1集成时会报找不到方法的错,原因是JedisShardInfo类的属性由timeout变成了soTimeout了 --> 26 <!-- <version>2.7.1</version> --> 27 <version>2.6.2</version> 28 </dependency> 29 30 <dependency> 31 <groupId>log4j</groupId> 32 <artifactId>log4j</artifactId> 33 <version>1.2.17</version> 34 </dependency> 35 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-core</artifactId> 39 <version>4.2.0.RELEASE</version> 40 </dependency> 41 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-context-support</artifactId> 45 <version>4.2.0.RELEASE</version> 46 </dependency> 47 48 <dependency> 49 <groupId>org.springframework.data</groupId> 50 <artifactId>spring-data-redis</artifactId> 51 <version>1.4.1.RELEASE</version> 52 </dependency> 53 </dependencies> 54 </project>
spring配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ss="http://www.springframework.org/schema/security" 4 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 12 13 <context:property-placeholder location="classpath:redis-pool.properties"/> 14 15 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" /> 16 17 <bean class="redis.clients.jedis.JedisPool"> 18 <constructor-arg index="0" ref="jedisPoolConfig" /> 19 <constructor-arg index="1" value="${redis.ip}" /> 20 <constructor-arg index="2" value="${redis.port}" /> 21 </bean> 22 23 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 24 <!-- 具体可配置项可以参见此类的源码 --> 25 <property name="hostName" value="${redis.ip}" /> 26 </bean> 27 28 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 29 <property name="connectionFactory" ref="jedisConnectionFactory" /> 30 </bean> 31 32 <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 33 <property name="connectionFactory" ref="jedisConnectionFactory" /> 34 </bean> 35 </beans>
测试类:
1 package com.yzl; 2 3 import java.io.Serializable; 4 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.support.ClassPathXmlApplicationContext; 10 import org.springframework.dao.DataAccessException; 11 import org.springframework.data.redis.connection.RedisConnection; 12 import org.springframework.data.redis.core.RedisCallback; 13 import org.springframework.data.redis.core.RedisTemplate; 14 import org.springframework.data.redis.core.StringRedisTemplate; 15 16 /** 17 * RedisApp之spring redisTemplate的测试类 18 * 19 * @author yangzhilong 20 * @see [相关类/方法](可选) 21 * @since [产品/模块版本] (可选) 22 */ 23 public class RedisApp3Test { 24 private ApplicationContext app; 25 private RedisTemplate<Serializable, Serializable> redisTemplate; 26 private StringRedisTemplate stringRedisTemplate; 27 28 @Before 29 public void before(){ 30 app = new ClassPathXmlApplicationContext("spring-config.xml"); 31 redisTemplate = (RedisTemplate)app.getBean("redisTemplate"); 32 stringRedisTemplate = app.getBean(StringRedisTemplate.class); 33 } 34 35 @Test 36 public void test(){ 37 System.out.println("use StringRedisTemplate save value"); 38 //保存 39 stringRedisTemplate.execute(new RedisCallback<String>() { 40 public String doInRedis(RedisConnection connection) throws DataAccessException { 41 connection.set(stringRedisTemplate.getStringSerializer().serialize("name"), stringRedisTemplate.getStringSerializer().serialize("hello")); 42 return null; 43 } 44 }); 45 46 //取值 47 String value = stringRedisTemplate.execute(new RedisCallback<String>() { 48 public String doInRedis(RedisConnection connection) throws DataAccessException { 49 return stringRedisTemplate.getStringSerializer().deserialize(connection.get(stringRedisTemplate.getStringSerializer().serialize("name"))); 50 } 51 52 }); 53 54 System.out.println("use StringRedisTemplate get value :" + value); 55 56 System.out.println("use RedisTemplate append value"); 57 //追加 58 redisTemplate.execute(new RedisCallback<String>() { 59 public String doInRedis(RedisConnection connection) throws DataAccessException { 60 connection.append(redisTemplate.getStringSerializer().serialize("name"), redisTemplate.getStringSerializer().serialize(" redis")); 61 return null; 62 } 63 }); 64 65 //取值 66 value = redisTemplate.execute(new RedisCallback<String>() { 67 68 public String doInRedis(RedisConnection connection) throws DataAccessException { 69 // TODO Auto-generated method stub 70 return redisTemplate.getStringSerializer().deserialize(connection.get(redisTemplate.getStringSerializer().serialize("name"))); 71 } 72 }); 73 System.out.println("use RedisTemplate get value :" + value); 74 } 75 76 @After 77 public void after(){ 78 System.out.println("end~~~"); 79 } 80 }
运行结果:
1 use StringRedisTemplate save value 2 2015-08-14 15:23:27,241 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Opening RedisConnection 3 2015-08-14 15:23:27,379 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Closing Redis Connection 4 2015-08-14 15:23:27,381 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Opening RedisConnection 5 2015-08-14 15:23:27,382 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Closing Redis Connection 6 use StringRedisTemplate get value :hello 7 use RedisTemplate append value 8 2015-08-14 15:23:27,382 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Opening RedisConnection 9 2015-08-14 15:23:27,404 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Closing Redis Connection 10 2015-08-14 15:23:27,405 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Opening RedisConnection 11 2015-08-14 15:23:27,405 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Closing Redis Connection 12 use RedisTemplate get value :hello redis 13 end~~~