Spring-Data-Redis

Spring-Data-Redis

操作 Redis 的 api

Springboot 下使用 Spring-Data-Redis

插入数据 key 乱码

alt

解决办法:

查看 自动配置类 ctrl + f

alt

        //自定义序列化方式
        RedisSerializer<String> stringSerializer = new StringRedisSerializer(Charset.forName("GBK"));
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(stringSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(stringSerializer);
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author CodeHaywire
 */
@SpringBootApplication
@ComponentScan({"com.example.config"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
package com.example.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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.nio.charset.Charset;

/**
 * @ClassName : RedisSerializerConfig
 * @Description : 自定义序列化
 * @Author : CodeHaywire
 * @Created at 2022/4/24 17:14
 */
@Configuration
public class RedisSerializerConfig {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //自定义序列化方式
        RedisSerializer<String> stringSerializer = new StringRedisSerializer(Charset.forName("GBK"));
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(stringSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(stringSerializer);
        return template;
    }
}


package com.example.demo;

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;
import org.springframework.data.redis.core.ValueOperations;

@SpringBootTest
class DemoApplicationTests {
    //注入  RedisTemplate 线程是安全的

    @Autowired


    private RedisTemplate redisTemplate;

    @Test
    void contextLoads() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("你好", "你好");
        System.out.println(valueOperations.get("你好"));
    }
}

cmd 查看 key 中文乱码:不使用 redis-cli

换成 redis-cli --raw

E:\idea-workspace\learn-spring-data-redis>redis-cli
127.0.0.1:6379> keys *
1) "\xc4\xe3\xba\xc3"
E:\idea-workspace\learn-spring-data-redis>redis-cli --raw
127.0.0.1:6379> keys *
你好
127.0.0.1:6379> get 你好
你好

tip:正常情况下也不会 key 使用中文

乱码影响查看,但是不影响编码

点击查看 api 文档

RedisTemplate的使用方法      important

String类型的常用方法

        //键值同时操作redis  很像cmd操作redis
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("name", "ZanSan");
        //先绑定键再操作redis的值 假如user需要多次操作,key可以不重复写 set("name", "ZanSan");
        BoundValueOperations user = redisTemplate.boundValueOps("name");
        user.set("ZanSan");

以上两种都可以插入数据,哪个方便哪个来

package com.example.demo;

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.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

@SpringBootTest
class DemoApplicationTests {
    //注入  线程是安全的

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void contextLoads() {
        BoundValueOperations user = redisTemplate.boundValueOps("name");
        //设置过期时间 10s过期
        user.set("CodeHaywire",10, TimeUnit.SECONDS);
    }
}

C:\Users\CodeHaywire>redis-cli --raw

127.0.0.1:6379> keys *
name
127.0.0.1:6379> get name
CodeHaywire

127.0.0.1:6379> keys *   #这是10s后的操作

127.0.0.1:6379>
全部评论

相关推荐

08-03 18:31
中南大学
我最后看了一下半成品,还不如找个网站用AI一键生成的:
不服输的鑫:自己写第一份简历还是有点难搞,也是花了100多找人写😅
简历被挂麻了,求建议
点赞 评论 收藏
分享
学一下吧现在太菜了:和简历没关系,你是清华的他就要了。多投投就行了
点赞 评论 收藏
分享
Twilight_m...:表格简历有点难绷。说说个人看法: 1.个人基本情况里好多无意义信息,什么婚姻状况、健康状况、兴趣爱好、户口所在地、身份证号码、邮政编码,不知道的以为你填什么申请表呢。 2.校内实践个人认为对找工作几乎没帮助,建议换成和测开有关的项目,实在没得写留着也行。 3.工作经历完全看不出来是干什么的,起码看着和计算机没啥关系,建议加强描述,写点你在工作期间的实际产出、解决了什么问题。 4.个人简述大而空,看着像AI生成,感觉问题最大。“Python,C,C++成为我打造高效稳定服务的得力工具”、“我渴望凭借自身技术知识与创新能力,推动人工智能技术的应用发展,助力社会实现智能化转型”有种小学作文的美感。而且你确定你个人简述里写的你都会嘛?你AI这块写的什么“深入研究”,发几篇顶会的硕博生都不一定敢这么写。而且你AI这块的能力和软测也完全无关啊。个人简述建议写你对哪些技术栈、哪些语言、哪些生产工具的掌握,写的有条理些,而且最好是和测开强相关的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务