一、SpringDataRedis概述
SpringDataRedis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:
1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
二、快速入门Demo
1、Maven pom配置
4.2.4.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
junit
junit
4.9
redis.clients
jedis
2.8.1
org.springframework.data
spring-data-redis
1.7.2.RELEASE
2、redis-config.properties配置文件
# Redis settings
redis.host=xxx.xxx.xxx.xxx
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
3、applicationContext-redis.xml配置文件
4、值操作Test
package com.springDataRedis.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("test1");
}
/**
* 获取值 --- 查
*/
@Test
public void getValue(){
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
}
/**
* 删除值 --- 删
*/
@Test
public void deleteValue(){
redisTemplate.delete("name");
}
}
5、Set类型操作
package com.springDataRedis.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;
/**
* Set类型操作
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存值 --- 增
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("nameset").add("曹操");
redisTemplate.boundSetOps("nameset").add("刘备");
redisTemplate.boundSetOps("nameset").add("孙权");
}
/**
* 获取值 --- 查
*/
@Test
public void getValue(){
Set set = redisTemplate.boundSetOps("nameset").members();
System.out.println(set);
}
/**
* 删除某个值 --- 删
*/
@Test
public void removeValue(){
redisTemplate.boundSetOps("nameset").remove("孙权");
}
/**
* 删除所有值 --- 删
*/
@Test
public void delete(){
redisTemplate.delete("nameset");
}
}
6、List类型操作
package com.springDataRedis.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;
/**
* List类型操作
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
/*
* 右压栈 : 后添加的元素排在后边
*/
@Test
public void testSetValue1(){
redisTemplate.boundListOps("namelist1").rightPush("刘备");
redisTemplate.boundListOps("namelist1").rightPush("关羽");
redisTemplate.boundListOps("namelist1").rightPush("张飞");
}
/**
* 显示右压栈的值
*/
@Test
public void testGetValue1(){
List list = redisTemplate.boundListOps("namelist1").range(0, 10);
System.out.println(list);
}
@Test
public void delete(){
redisTemplate.delete("namelist1");
}
/**
* 左压栈
*/
@Test
public void testSetValue2(){
redisTemplate.boundListOps("namelist2").leftPush("刘备");
redisTemplate.boundListOps("namelist2").leftPush("关羽");
redisTemplate.boundListOps("namelist2").leftPush("张飞");
}
/**
* 显示左压栈的值
*/
@Test
public void testGetValue2(){
List list = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(list);
}
/**
* 根据index获取值
*/
@Test
public void searchByIndex(){
String str = (String) redisTemplate.boundListOps("namelist2").index(1);
System.out.println(str);
}
/**
* 删除值
*/
@Test
public void removeValue(){
redisTemplate.boundListOps("namelist1").remove(0, "刘备");
}
}
7、Map类型操作
package com.springDataRedis.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 testSetValue(){
redisTemplate.boundHashOps("namehash").put("a", "唐僧");
redisTemplate.boundHashOps("namehash").put("b", "悟空");
redisTemplate.boundHashOps("namehash").put("c", "八戒");
redisTemplate.boundHashOps("namehash").put("d", "沙僧");
}
/**
* 获取所有的key
*/
@Test
public void testGetKes(){
Set keys = redisTemplate.boundHashOps("namehash").keys();
System.out.println(keys);
}
/**
* 获取所有的值
*/
@Test
public void testGetValues(){
List list = redisTemplate.boundHashOps("namehash").values();
System.out.println(list);
}
/**
* 根据KEY取值
*/
@Test
public void searchValueByKey(){
String str = (String) redisTemplate.boundHashOps("namehash").get("b");
System.out.println(str);
}
/**
* 移除某个小key的值
*/
@Test
public void removeValue(){
redisTemplate.boundHashOps("namehash").delete("c");
}
}