目录
概述
spring中的框架,用来操作redis,封装了(Jedis,Jredis,RJC);t提供了各种操作;异常处理及序列化,支持发布订阅,并对spring3.1 cache进行了实现;
功能:
- 连接池自动管理,提供了一个高度封装的"RedisTemplate"类
- 针对jedis客户断种大量api进行了归类封装,将同易类型操作封装为operation接口
- ValueOperations: 简单K-V操作
- setOperations: set 类型数据操纵
- ZsetOperations: zset类型数据操作
- HashOperations: 针对map类型的数据操作
- ListOperations: 针对list类型的数据操作
maven工程demo搭建
目录结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.bufanli</groupId>
<artifactId>spring-data-redis-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 集中定义依赖版本号 -->
<properties>
<spring.version>4.3.18.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>
<!--redis依赖-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.11.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
redis-config.properties
# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
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"
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">
<context:property-placeholder location="classpath*:properties/*.properties" />
<!-- redis 相关配置 jedis包配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲数-->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--连接时最大等待毫秒数-->
<property name="maxWaitMillis" value="${redis.maxWait}" />
<!--在提取redis实例时是否提前进行验证操作,true获取实例是可用的-->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!--jedis连接工厂类-->
<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"/>
<!--redis操作模板,将jedis连接工厂类注入到工程中-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>
TestValue.java 简单值
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;
/**
* k v
* @author BuShuangLi
* @date 2019/5/9
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-redis.xml"})
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存值
*/
@Test
public void testSetValue(){
redisTemplate.boundValueOps("name").set("bufanli");
}
/**
* 取值
*/
@Test
public void testGetValue(){
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
//控制台打印 bufanli
}
/**
* 删除
*/
@Test
public void testDeleteValue(){
redisTemplate.delete("name");
}
}
TestSet.java set 集合
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;
import java.util.Set;
/**
* @author BuShuangLi
* @date 2019/5/9
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-redis.xml"})
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
/**
* 往set集合中存值
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("nameSet").add("曹操");
redisTemplate.boundSetOps("nameSet").add("刘备");
redisTemplate.boundSetOps("nameSet").add("孙权");
}
/**
* 从set集合中获取值
*/
@Test
public void getValue(){
Set nameSet = redisTemplate.boundSetOps("nameSet").members();
System.out.println(nameSet);
}
/**
* 从set集合中获取值
*/
@Test
public void removeValue(){
//删除其中一个
Long remove = redisTemplate.boundSetOps("nameSet").remove("刘备");
Set nameSet = redisTemplate.boundSetOps("nameSet").members();
//打印[曹操, 孙权]
System.out.println(nameSet);
//删除全部
redisTemplate.delete("nameSet");
Set nameSet2 = redisTemplate.boundSetOps("nameSet").members();
//[]
System.out.println(nameSet2);
}
}
TestList.java list 集合
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;
import java.util.List;
/**
* @author BuShuangLi
* @date 2019/5/9
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
/**
* 往list集合中存值
* 右压栈,后添加的对象排在后面
*/
@Test
public void setValue1(){
redisTemplate.boundListOps("nameList1").rightPush("刘备");
redisTemplate.boundListOps("nameList1").rightPush("关羽");
redisTemplate.boundListOps("nameList1").rightPush("张飞");
}
/**
* 往list集合中存值
* 左压栈,后添加的对象排在前面
*/
@Test
public void setValue2(){
redisTemplate.boundListOps("nameList2").leftPush("刘备");
redisTemplate.boundListOps("nameList2").leftPush("关羽");
redisTemplate.boundListOps("nameList2").leftPush("张飞");
}
/**
* 查询方法
*/
@Test
public void getValue(){
//range(开始下标,结束下标);
List nameList1 = redisTemplate.boundListOps("nameList1").range(0, 10);
//右压栈 打印 [刘备, 关羽, 张飞]
System.out.println(nameList1);
List nameList2 = redisTemplate.boundListOps("nameList2").range(0, 10);
//左压栈 打印 [张飞, 关羽, 刘备]
System.out.println(nameList2);
}
@Test
public void searchByIndex(){
//查询第二个
Object nameList2 = redisTemplate.boundListOps("nameList2").index(1);
System.out.println(nameList2);
}
/**
* 删除
*/
public void deleteValue(){
//remove(移除的个数,移除的value); 如果有两个要移除的value移除个数是1 那么只是删除一个,删除的是靠前的
redisTemplate.boundListOps("nameList2").remove(1,"张飞");
//删除全部
redisTemplate.delete("nameList2");
}
}
TestHash.java map键值对
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;
import java.util.List;
import java.util.Set;
/**
* @author BuShuangLi
* @date 2019/5/9
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext-redis.xml"})
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存键值对
*/
@Test
public void setValue(){
redisTemplate.boundHashOps("nameHash").put("a","唐僧");
redisTemplate.boundHashOps("nameHash").put("b","悟空");
redisTemplate.boundHashOps("nameHash").put("c","八戒");
redisTemplate.boundHashOps("nameHash").put("d","沙僧");
}
/**
* 取键值对
*/
@Test
public void getValue(){
//获得所有的key
Set nameHash = redisTemplate.boundHashOps("nameHash").keys();
//打印: [a, b, c, d]
System.out.println(nameHash);
//获取所有value
List nameHash1 = redisTemplate.boundHashOps("nameHash").values();
//打印: [唐僧, 悟空, 八戒, 沙僧]
System.out.println(nameHash1);
//根据key获取值
Object o = redisTemplate.boundHashOps("nameHash").get("a");
//打印: 唐僧
System.out.println(o);
//移除某个key的值
redisTemplate.boundHashOps("nameHash").delete("c");
//打印: [唐僧, 悟空, 沙僧]
System.out.println(redisTemplate.boundHashOps("nameHash").values());
}
}