SpringDataRedis对jedis(以及rjc ,jredis)进行了高度封装,通过redisTemplate提供redis操作的各种实现,异常处理以及序列化,下面简单展示如何用redisTemplate来对redis的各种操作;
key-value
对简单的key-value操作,有常规的存取以及删除demo:
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue() {
redisTemplate.boundValueOps("name").set("good");
}
@Test
public void getValue() {
String value = (String) redisTemplate.boundValueOps("name").get();
System.out.println(value);
}
@Test
public void deleValue() {
redisTemplate.delete("name");
}
}
set
操作:存取,移除一个元素,删除set
package test;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setSetValue() {
redisTemplate.boundSetOps("nameSet").add("阿猫");
redisTemplate.boundSetOps("nameSet").add("艳花儿");
redisTemplate.boundSetOps("nameSet").add("阿狗");
}
@Test
public void getSetValue() {
Set<String> set = redisTemplate.boundSetOps("nameSet").members();
System.out.println(set);
}
@Test
public void removeValue() {
redisTemplate.boundSetOps("nameSet").remove("阿猫");
}
@Test
public void deleValue() {
redisTemplate.delete("nameSet");
}
}
list
不同于set ,list是有序的存储,除了存取及删除list值之外,自然就有按索引位置获取值,同样list里的元素也是可以重复存储的,自然就有删除值的时候,指定删除此值的元素个数。最后存的时候有两种方式,一种左压栈,特点最后进来的先查出来,另一种存储方式为右压栈,特点最后存进来的最后查出来。可以执行下demo看看效果
package test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
/**list不同于set,它是有序存放,springDataRedis中有两种存储方式,之一为
* 右压栈:特点取的时候,最后放进去的,最后取出来*/
@Test
public void setListByRightPush() {
redisTemplate.boundListOps("myList").rightPush("艳花儿");
redisTemplate.boundListOps("myList").rightPush("阿猫");
redisTemplate.boundListOps("myList").rightPush("阿狗");
}
/**之二为
* 左压栈:特点取的时候,最后放进去的,最先取出来*/
@Test
public void setListByLeftPush() {
redisTemplate.boundListOps("myList2").leftPush("艳花儿");
redisTemplate.boundListOps("myList2").leftPush("阿猫");
redisTemplate.boundListOps("myList2").leftPush("阿狗");
redisTemplate.boundListOps("myList2").leftPush("艳花儿");
redisTemplate.boundListOps("myList2").leftPush("阿猫");
redisTemplate.boundListOps("myList2").leftPush("阿狗");
}
@Test
public void testGetList() {
List list1 = redisTemplate.boundListOps("myList").range(0, 10);
List list2 = redisTemplate.boundListOps("myList2").range(0, 20);
System.out.println(list1 + "," + list2);
}
/**按照索引位置进行查询*/
@Test
public void searchByIndex4List() {
String a = (String) redisTemplate.boundListOps("myList").index(0);
String a1 = (String) redisTemplate.boundListOps("myList2").index(0);
System.out.println(a + "," + a1);
}
@Test
public void deleteList() {
redisTemplate.delete("myList2");
}
@Test
public void removeList() {
redisTemplate.boundListOps("myList2").remove(2, "阿狗");//删除2个阿狗的value值
redisTemplate.boundListOps("myList2").remove(1, "艳花儿");//删除1个艳花儿的value值
}
}
hash
hash里每一个都是key-value形式,所以删除和查询和key-value操作一样:
不同的是接口用的boundHashOps,demo里展示了 存值:redisTemplate.boundHashOps("myHash").put("a", "小a"); redisTemplate.boundHashOps("myHash").put("b", "小b");
获取所有keys,values:
redisTemplate.boundHashOps("myHash").keys() redisTemplate.boundHashOps("myHash").values()
根据key值获取某一个元素值:redisTemplate.boundHashOps("myHash").get("a");
根据key值删除某一个元素值:redisTemplate.boundHashOps("myHash").delete("c")
package test;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setHashValue() {
redisTemplate.boundHashOps("myHash").put("a", "小a");
redisTemplate.boundHashOps("myHash").put("b", "小b");
redisTemplate.boundHashOps("myHash").put("c", "小c");
redisTemplate.boundHashOps("myHash").put("d", "小d");
}
@Test
public void getHashValues() {
List listValues = redisTemplate.boundHashOps("myHash").values();
System.out.println(listValues);
}
@Test
public void getHashKeys() {
Set listKeys = redisTemplate.boundHashOps("myHash").keys();
System.out.println(listKeys);
}
@Test
public void getHashValue() {
String str = (String) redisTemplate.boundHashOps("myHash").get("a");
System.out.println(str);
}
@Test
public void removeHashValue() {
redisTemplate.boundHashOps("myHash").delete("c");
}
}
基础配置
1 首先在pom文件中引入spring相关依赖以及本次核心的springDataRedis和jedis jar 包
<!-- 集中定义依赖版本号 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</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-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</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.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
2 属性文件配置好redis连接信息以及获取jedis实例的连接池信息,如不在本地启动redis服务,host要调整下【文件名:resource:properties / redis-config.properties】
# Redis settings
# server IP
redis.host=127.0.0.1
# server port
redis.port=6379
# server pass
redis.pass=
# use dbIndex
redis.database=0
# \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B
redis.maxIdle=300
# \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B
redis.maxWait=3000
# \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684
redis.testOnBorrow=true
3 配置连接池信息,spring整合的jedis连接工厂以及需要注册下核心操作bean :RedisTemplate
【文件名:resource:spring / applicationContext-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:property-placeholder location="classpath*:properties/*.properties" />
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="JedisConnectionFactory" 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.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>