Spring Data Redis 为了方便操作字符串,提供了以字符串为中心的 StringRedisTemplate 便捷类。
由于存储在 Redis 中的键和值通常是 java.lang.String,因此 Redis 模块为 RedisConnection 和 RedisTemplate 提供了两个扩展实现,分别是 StringRedisConnection(及其 DefaultStringRedisConnection 实现)和 StringRedisTemplate。
作为便捷的一站式解决方案,它们用于密集的字符串操作。除了绑定到字符串键之外,模板(template)和连接(connection)还使用了下面的 StringRedisSerializer 序列化器(字符串序列化器,可以将 Java 对象序列化为字符串),这意味着 Redis 中存储的键(key)和值(value)是人类可读的(假设在 Redis 和您的代码中使用相同的字符编码,没有出现乱码情况)。
如果你的项目是传统 Spring 项目,而非 Spring Boot 项目。那么需要在 Spring 的 XML 配置文件中配置 StringRedisTemplate 的Bean。application-content.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置 Redis 连接工厂 --> <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true"/> <!-- 配置 Redis 模板对象,设置 key 和 value 的序列化为字符串 --> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnFactory"/> </beans>
接下来编写客户端测试代码,我们使用手动的方式去加载 appication-content.xml 配置文件和获取 Bean,代码如下:
package com.hxstrive.redis;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
/**
* 简单测试 StringRedisTemplate
* @author hxstrive.com 2022/9/27
*/
public class StringRedisTemplateDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"application-content2.xml");
System.out.println("context = " + context);
// 获取 StringRedisTemplate 对象
StringRedisTemplate stringRedisTemplate = context.getBean(StringRedisTemplate.class);
System.out.println("stringRedisTemplate=" + stringRedisTemplate);
// 获取 Operations 对象
ValueOperations<String,String> ops = stringRedisTemplate.opsForValue();
ops.set("string-key", "hello world");
// 获取值
String value = ops.get("string-key");
System.out.println("value=" + value);
}
}运行示例,输出结果:
context = org.springframework.context.support.ClassPathXmlApplicationContext@20fa23c1, started on Tue Sep 27 12:58:00 CST 2022 stringRedisTemplate=org.springframework.data.redis.core.StringRedisTemplate@78691363 value=hello world
注意:如果你使用的是 Spring 项目,则可以通过 @Autowired 注解自动注入 StringRedisTemplate。
上面介绍了怎样使用 XML 配置 StringRedisTemplate,下面将介绍使用 @Configration 配置类进行配置。代码如下:
package com.hxstrive.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
/**
* Redis配置
* @author hxstrive.com 2022/2/26
*/
@Configuration
public class RedisConfig {
@Bean
public StringRedisTemplate stringStringRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}package com.hxstrive.redis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
/**
* StringRedisTemplate 测试类
* @author hxstrive.com 2022/9/27
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class StringRedisTemplateTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void simple() {
ValueOperations<String,String> ops = stringRedisTemplate.opsForValue();
// 设置值
ops.set("key", "hello world");
// 获取值
String value = ops.get("key");
System.out.println("value=" + value);
}
}运行示例,输出结果:
value=hello world
注意,与其他 Spring 模板一样,RedisTemplate 和 StringRedisTemplate 可以通过 RedisCallback 接口直接与 Redis 对话。此功能可让您完全控制 Redis,因为它直接与 RedisConnection 对话。
当使用 StringRedisTemplate 时,回调会接收 StringRedisConnection 的实例。以下示例展示了如何使用 RedisCallback 接口:
package com.hxstrive.redis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
/**
* StringRedisTemplate 测试类
* @author hxstrive.com 2022/9/27
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class StringRedisTemplateTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void useCallback() {
stringRedisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
System.out.println("connection=" + connection);
StringRedisConnection conn = (StringRedisConnection) connection;
// 设置值
conn.set("key", "test StringRedisConnection");
// 获取值
String value = conn.get("key");
System.out.println("value=" + value);
return null;
}
});
}
}运行示例,输出结果如下:
connection=org.springframework.data.redis.connection.DefaultStringRedisConnection@6403e24c value=test StringRedisConnection