spring-data集成redis

先看看需要的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);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值