先看看需要的jar包吧
property文件:
# Redis settings
redis.host=192.168.2.97
redis.port=6379
redis.pass=
redis.maxIdle=50
redis.minIdle=10
redis.maxTotal=100
redis.testOnBorrow=true
spring配置文件(配置文件在web-inf下):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<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>
代码接口:
package com.ecommerce.common.redis;
import com.ecommerce.platform.bean.DataItem;
public interface DataItemRedisDao {
public DataItem read(final String uid);
public void save(final DataItem dataItem);
public void delete(final String uid);
}
实现类:
package com.ecommerce.common.redis.impl;
import java.io.Serializable;
import javax.annotation.Resource;
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.ecommerce.common.redis.DataItemRedisDao;
import com.ecommerce.platform.bean.DataItem;
@Repository
public class DataItemRedisDaoImpl implements DataItemRedisDao {
@Resource
private RedisTemplate<Serializable, Serializable> redisTemplate;
public RedisTemplate<Serializable, Serializable> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(
RedisTemplate<Serializable, Serializable> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public DataItem read(final String uid) {
return redisTemplate.execute(new RedisCallback<DataItem>() {
@Override
public DataItem doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize(
"dataitem.uid." + uid);
if (connection.exists(key)) {
byte[] value = connection.get(key);
String dataValue = redisTemplate.getStringSerializer()
.deserialize(value);
DataItem dataItem = new DataItem();
dataItem.setValue(dataValue);
dataItem.setId(uid);
return dataItem;
}
return null;
}
});
}
@Override
public void save(final DataItem dataItem) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.set(
redisTemplate.getStringSerializer().serialize(
"dataitem.uid." + dataItem.getId()),
redisTemplate.getStringSerializer().serialize(
dataItem.getValue()));
return null;
}
});
}
@Override
public void delete(final String uid) {
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
connection.del(redisTemplate.getStringSerializer().serialize(
"dataitem.uid." + uid));
return null;
}
});
}
}
测试类:
package com.ecommerce.common.redis;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.ecommerce.platform.bean.DataItem;
public class Test1 {
private ApplicationContext app;
private DataItemRedisDao dataItemRedisDao;
@Before
public void before() throws Exception {
app = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/config/spring/context-redis.xml");
dataItemRedisDao = (DataItemRedisDao) app.getBean("dataItemRedisDao");
}
@Test
public void crud() {
// -------------- Create ---------------
String uid = "u123456";
String value1 = "上海";
DataItem dataItem = new DataItem();
dataItem.setId(uid);
dataItem.setValue(value1);
dataItemRedisDao.save(dataItem);
// ---------------Read ---------------
dataItem = dataItemRedisDao.read(uid);
assertEquals(value1, dataItem.getValue());
// --------------Update ------------
String value2 = "北京";
dataItem.setValue(value2);
dataItemRedisDao.save(dataItem);
dataItem = dataItemRedisDao.read(uid);
assertEquals(value2, dataItem.getValue());
// --------------Delete ------------
dataItemRedisDao.delete(uid);
dataItem = dataItemRedisDao.read(uid);
assertNull(dataItem);
}
}