【linux系统之redis6】redisTemplate的使用方法

新版本的application.yml配置文件

xml 复制代码
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使用方法

xml 复制代码
<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>
java 复制代码
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);
    }

}
  • 中文字符的处理

  • 依赖添加
xml 复制代码
 <dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
 </dependency>
  • redisTemplate配置类
java 复制代码
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;
    }
}
java 复制代码
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;
}
  • 测试类
java 复制代码
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);
    }

}


这样,显示就正常了

  • 手动序列化
java 复制代码
@Autowired
private StringRedisTemplate stringRedisTemplate;


@Test
void testString(){
    stringRedisTemplate.opsForValue().set("name","胡歌111");
    String name = stringRedisTemplate.opsForValue().get("name");
    System.out.println("name = " + name);

}
java 复制代码
@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数据
java 复制代码
@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);
}


总结

相关推荐
David爱编程2 分钟前
彻底搞懂容器启动、停止、调试的每一个细节!
后端·docker·容器
gb421528723 分钟前
springboot项目下面的单元测试注入的RedisConnectionFactory类redisConnectionFactory值为什么为空呢?
spring boot·后端·单元测试
why15144 分钟前
6.4 计算机网络面试题
后端·计算机网络
heart000_11 小时前
Go语言基础知识总结(超详细整理)
开发语言·后端·golang
残*影1 小时前
Spring 中注入 Bean 有几种方式?
java·后端·spring
江湖十年1 小时前
在 Go 语言中如何实现协程池
后端·面试·go
Humbunklung1 小时前
Rust 数据类型
开发语言·后端·rust
南玖yy1 小时前
深入理解 x86 汇编中的重复前缀:REP、REPZ/REPE、REPNZ/REPNE(进阶详解版)
开发语言·网络·汇编·后端·算法·bochs
寻月隐君1 小时前
Rust 所有权:从内存管理到生产力释放
后端·rust·github
小小工匠2 小时前
性能优化 - 案例篇:数据一致性
redis·性能优化·数据一致性