【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);
}


总结

相关推荐
怒放吧德德1 小时前
Python3基础:基础实战巩固,从“会用”到“活用”
后端·python
jiunian_cn1 小时前
【Redis】数据库管理操作
数据库·redis·缓存
惊讶的猫1 小时前
Redis 哨兵(Sentinel)介绍
redis·redis哨兵
猫头虎2 小时前
基于信创openEuler系统安装部署OpenTeleDB开源数据库的实战教程
数据库·redis·sql·mysql·开源·nosql·database
静听山水2 小时前
Redis核心数据结构-ZSet
数据结构·redis
苏三说技术2 小时前
xxl-job 和 elastic-job,哪个更好?
后端
Dontla2 小时前
黑马大模型RAG与Agent智能体实战教程LangChain提示词——6、提示词工程(提示词优化、few-shot、金融文本信息抽取案例、金融文本匹配案例)
redis·金融·langchain
難釋懷2 小时前
秒杀优化-基于阻塞队列实现秒杀优化
redis·缓存
三小河2 小时前
Agent Skill与Rules的区别——以Cursor为例
前端·javascript·后端
三小河2 小时前
前端视角详解 Agent Skill
前端·javascript·后端