SpringDataRedis
SpringData-Redis概述
SpringData-Redis是Spring大家族的一部分,提供了在Srping应用中通过简单的配置访问Redis服务,对Reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装。RedisTemplate提供了Redis各种操作、异常处理及序列化,支持发布订阅。
SpringData-Redis的功能
连接池自动管理,提供了一个高度封装的RedisTemplate
类, 针对jedis客户端中大量api进行了归类封装。
类名 | 作用 |
---|---|
SetOperations | set类型数据操作 |
ZSetOperations | zset类型数据操作 |
HashOperations | 针对map类型的数据操作 |
ListOperations | 针对list类型的数据操作 |
ValueOperations | 简单Key-Value操作 |
SpringData-Redis入门程序
首先构建一个Maven工程,SpringDataRedisDemo,jar工程,紧接着引入Spring和Jedis和SpringDataRedis的依赖,如下:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</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>
在src/main/resources建立redis-config.properties
,内容如下:
redis.host=localhost
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
在src/main/resources创建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"/>
<!-- 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>
建立TestString测试字符串操作方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestString {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSet() {
redisTemplate.boundValueOps("name").set("BNTang");
}
@Test
public void testGet() {
String name = (String) redisTemplate.boundValueOps("name").get();
System.out.println("name=" + name);
}
@Test
public void testDelete() {
redisTemplate.delete("name");
}
}
建立TestList测试List操作方法
lpush
/**
* @author BNTang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testLpush() {
redisTemplate.boundListOps("myListkey").leftPush("001");
redisTemplate.boundListOps("myListkey").leftPush("002");
redisTemplate.boundListOps("myListkey").leftPush("003");
redisTemplate.boundListOps("myListkey").leftPush("004");
}
}
rpush
/**
* @author BNTang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRpush() {
redisTemplate.boundListOps("myListkey").rightPush("001");
redisTemplate.boundListOps("myListkey").rightPush("002");
redisTemplate.boundListOps("myListkey").rightPush("003");
redisTemplate.boundListOps("myListkey").rightPush("004");
}
}
range
/**
* @author BNTang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRange() {
List<String> myListKey = redisTemplate.boundListOps("myListkey").range(0, -1);
for (String s : myListKey) {
System.out.println("value = " + s);
}
}
}
delete
建立TestHash测试hash
hash格式
put
/**
* @author BNTang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testPut() {
redisTemplate.boundHashOps("keyName1").put("name", "zs");
redisTemplate.boundHashOps("keyName1").put("age", "20");
redisTemplate.boundHashOps("keyName1").put("phone", "1374578547");
redisTemplate.boundHashOps("keyName1").put("email", "303158131@qq.com");
}
}
get
/**
* @author BNTang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testGetOne() {
String name = (String) redisTemplate.boundHashOps("keyName1").get("name");
System.out.println("name = " + name);
}
}
entries
/**
* @author BNTang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testGetAll() {
Map<String, String> testHash = (Map<String, String>) redisTemplate.boundHashOps("keyName1").entries();
Set<Map.Entry<String, String>> entries = testHash.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println("key = " + entry.getKey());
System.out.println("value = " + entry.getValue());
}
}
}
delete
/**
* @author BNTang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testDeleteOne() {
redisTemplate.boundHashOps("keyName1").delete("name");
}
}
deleteAll
/**
* @author BNTang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testDeleteOne() {
redisTemplate.delete("keyName1");
}
}
标签:
SpringData
, SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具