009_Redis_RedisTemplate入门

009_Redis_RedisTemplate入门

1、创建一个springboot项目
springboot注入依赖优点慢呀!困了😴,有好心人知道创建springboot怎么样才能使导入依赖快一点呀!!!!!!求告知!
在这里插入图片描述
写一下配置文件。

spring:
  redis:
    host: 10.223.31.215
    port: 6379
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: 100ms

编写测试类

package com.ym.redisdemo;

import lombok.val;
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;

@SpringBootTest
class RedisDemoApplicationTests {

    //注入Redis
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    void contextLoads() {
        //写入一条String数据
        redisTemplate.opsForValue().set("name","李易峰");
        //获取String数据
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println("name="+name);
    }

}

单元测试
在这里插入图片描述

2、SpringDataRedis的使用步骤

  • 引入依赖
  • 配置redis地址信息
  • 本地注入RedisTemplate

3、在客户端工具查询刚刚插入的String数据
在这里插入图片描述
数据在存储的时候被序列化了,默认JDK序列化方式被存储。

4、SpringDataRedis的序列化方式

RedisTemplate可以接受任意Object作为值写入Redis,只不过写入之前会把Object对象序列化成字节的形式,默认采用的是JDK序列化,也就是上图我们看到的结果。其存在的缺点如下。

  • 可读性差
  • 内存占用较大

5、序列化操作

package com.ym.redisdemo.com.ym.redis.config;

import lombok.val;
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: LYM
 * @description redis配置类
 * @version: V1.0
 * @date: 2022/4/20 23:34
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        //创建对象
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        //设置连接工厂
        template.setConnectionFactory(connectionFactory);
        //设置key的序列化
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer=new GenericJackson2JsonRedisSerializer();
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        //设置value的序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        //返回
        return template;
    }
}

在这里插入图片描述
测试对象

    @Test
    void testSaveUser(){
        //写入数据
        redisTemplate.opsForValue().set("user:100",new User("李易峰",18));
        User o = (User) redisTemplate.opsForValue().get("user:100");
        System.out.println(o);
    }

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值