原文:http://chentian114.iteye.com/blog/2292323
1、通过spring-data-redis集成redis
pom.xml依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chen</groupId>
<artifactId>test_redis02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build />
<properties>
<spring.version>3.1.2.RELEASE</spring.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
</project>
spring application-reids.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:redis.properties"/>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- <property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" /> -->
</bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>
UserDaoImpl.java
package com.chen.dao; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository; import com.chen.pojo.User;
@Repository
public class UserDaoImpl implements UserDao { @Autowired
private RedisTemplate<Serializable,Serializable> redisTemplate; public void saveUser(final User user) {
redisTemplate.execute(new RedisCallback<Object>(){
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
String str= "user.uid."+user.getId();
byte[] key = redisTemplate.getStringSerializer().serialize(str);
connection.set(key,redisTemplate.getStringSerializer().serialize(user.getName()));
return null;
}
});
} public User getUser(final long id) {
return redisTemplate.execute(new RedisCallback<User>(){
@Override
public User doInRedis(RedisConnection connection) throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize("user.uid." + id);
if(connection.exists(key)) {
byte[] value = connection.get(key);
String name = redisTemplate.getStringSerializer().deserialize(value);
User user = new User();
user.setName(name);
user.setId(id);
return user;
}
return null;
}
});
} }
2、通过jedis集成redis
spring applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:redis.properties"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- <property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" /> -->
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="${redis.host}" />
<constructor-arg name="port" value="${redis.port}" />
<!-- <constructor-arg name="timeout" value="${redis.timeout}" /> -->
</bean>
</list>
</constructor-arg>
</bean> </beans>
RedisDataSourceImpl.java
package com.chen.redis; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Repository("redisDataSource")
public class RedisDataSourceImpl implements RedisDataSource { @Autowired
private ShardedJedisPool shardedJedisPool; public ShardedJedis getRedisClient() {
try{
ShardedJedis shardJedis = shardedJedisPool.getResource();
return shardJedis;
}catch(Exception e){
e.printStackTrace();
}
return null;
} public void returnResource(ShardedJedis shardedJedis) {
shardedJedisPool.returnResource(shardedJedis);
} public void returnResource(ShardedJedis shardedJedis, boolean broken) {
if(broken){
shardedJedisPool.returnBrokenResource(shardedJedis);
}else{
shardedJedisPool.returnResource(shardedJedis);
}
} }
RedisClientTemplate.java
package com.chen.redis; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import redis.clients.jedis.ShardedJedis; @Repository("redisClientTemplate")
public class RedisClientTemplate {
@Autowired
private RedisDataSource redisDataSource; public void disconnect(){
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
shardedJedis.disconnect();
} public String set(String key,String val){
String result = null ;
ShardedJedis jedis = redisDataSource.getRedisClient();
if(jedis ==null)return null;
boolean broken = false;
try{
result = jedis.set(key, val);
}catch(Exception e){
e.printStackTrace();
broken = true ;
}finally{
redisDataSource.returnResource(jedis,broken);
}
return result;
} public String get(String key){
String result = null;
ShardedJedis jedis = redisDataSource.getRedisClient();
if(jedis==null)return result;
boolean broken = false;
try{
result = jedis.get(key);
}catch(Exception e){
e.printStackTrace();
broken= true;
}finally{
redisDataSource.returnResource(jedis,broken);
}
return result;
}
}
DemoTest.java