【linux系统之redis6】redisTemplate的使用方法
新版本的application.yml配置文件
spring:
data:
redis:
host: 192.168.1.102
port: 6379
lettuce:
pool:
max-active: 8
min-idle: 1
max-idle: 0
max-wait: 100
redisTemplate使用方法
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
package com.gaofeng;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@Slf4j
@SpringBootTest
class Springboot07RedisTemplateApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void testString() {
// 写入一条数据
redisTemplate.opsForValue().set("name","胡歌");
Object name = redisTemplate.opsForValue().get("name");
log.info("name is {}",name);
}
}
- 中文字符的处理
- 依赖添加
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
- redisTemplate配置类
package com.gaofeng.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.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* @author gaofeng
* @date 2025-01-08 - 20:42
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
// 创建redisTemplate对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 设置连接工厂
template.setConnectionFactory(redisConnectionFactory);
// 创建JSON序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// 设置key的序列化
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
// 设置value的序列化
template.setValueSerializer(jsonRedisSerializer);
template.setHashKeySerializer(jsonRedisSerializer);
// 返回
return template;
}
}
package com.gaofeng.redis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author gaofeng
* @date 2025-01-08 - 20:56
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private Integer age;
}
- 测试类
package com.gaofeng;
import com.gaofeng.redis.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@Slf4j
@SpringBootTest
class Springboot07RedisTemplateApplicationTests {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
void testString() {
// 写入一条数据
redisTemplate.opsForValue().set("name","胡歌");
Object name = redisTemplate.opsForValue().get("name");
log.info("name is {}",name);
}
@Test
void testSaveUser(){
redisTemplate.opsForValue().set("user:1",new User("gaofeng",30));
User user = (User) redisTemplate.opsForValue().get("user:1");
log.info("user is {} ",user);
}
}
这样,显示就正常了
- 手动序列化
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void testString(){
stringRedisTemplate.opsForValue().set("name","胡歌111");
String name = stringRedisTemplate.opsForValue().get("name");
System.out.println("name = " + name);
}
@Test
void testSaveUser() throws JsonProcessingException {
User user = new User("胡歌222", 40);
String json = mapper.writeValueAsString(user);
stringRedisTemplate.opsForValue().set("user:200",json);
String jsonUser = stringRedisTemplate.opsForValue().get("user:200");
//
User user1 = mapper.readValue(jsonUser, User.class);
System.out.println("user1 = " + user1);
}
这样我们就去掉了自动序列化多余的@class字段
- hash数据
@Test
void testHash(){
stringRedisTemplate.opsForHash().put("user:400","name","gaofeng");
stringRedisTemplate.opsForHash().put("user:400","age","30");
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("user:400");
System.out.println(entries);
}