Spring-data-redis配置及使用示例

Spring-data-redis配置及使用示例

一、添加maven依赖

<!-- redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.0.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
<scope>provided</scope>
</dependency>
<!-- redis -->

二、redis.properties

##applicationContext-redis.xml
redis.ip=192.168.1.156
redis.port=6379 
redis.timeout=5000
redis.maxIdle=10
redis.minIdle=1
redis.maxTotal=30 
redis.maxWaitMillis=5000  
#testOnBorrow true 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true

三、apllicationContext-redis.xml

<?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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
                        http://www.springframework.org/schema/util/spring-util-4.0.xsd"
default-lazy-init="true">
<description>Redis配置</description>
<context:property-placeholder location="classpath*:redis.properties" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="timeout" value="${redis.timeout}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>

</beans>

四、使用示例

package cn.com.easy.redis;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * redis 测试
 * 
 * @author nibili 2015年5月13日
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext-redis-demo.xml")
public class RedisTest {
@Resource(name = "redisTemplate")
private RedisTemplate<String, String> template;
private String stringKey = "stringKey";
private String listKey = "listKey";
//private String hashKey = "hashKey";
//private String setKey = "setKey";
//private String sortSetKey = "sortSetKey";
/**
 * 
 * 
 * @auth nibili 2015年5月13日
 */
@Test
public void sendString() {
try {
template.boundValueOps(stringKey).set("hello", 10, TimeUnit.SECONDS);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
 * 
 * 
 * @auth nibili 2015年5月13日
 */
@Test
public void getString() {
try {
String temp = template.boundValueOps(stringKey).get();
System.out.println("key:" + stringKey + ",value:" + temp);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Test
public void pushList() {
try {
template.boundListOps(listKey).leftPush("list1");
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Test
public void getList() {
try {
System.out.println(template.boundListOps(listKey).leftPop());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

五、redis几种数据结构的使用

@Resource(name = "redisTemplate")
private RedisTemplate<String, String> template;

tedisTemplate 集合了,所有数据类型的操作,先通过绑定类型 key,然后再进行操作:

template.boundValueOps(stringKey);
template.boundHashOps(key);
template.boundListOps(key);
template.boundSetOps(key);
template.boundZSetOps(key);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值