Redis入门到实战教程(基础篇)笔记

教学来源:

Redis课程介绍导学_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1cr4y1671t?p=1一、Redis 入门

1.认识NoSQL

2.Redis在虚拟机中的安装和开机自启

Redis在虚拟机中安装和配置开机自启-CSDN博客https://blog.csdn.net/qq_69183322/article/details/138168301

3.Redis的数据结构

4.Redis的基本类型

(1) 字符串String类型

(2)哈希Hash类型

(3) 列表List类型

(4)集合Set类型

(5)有序集合类型

二、Redis的Java客户端Jedis

(1)Jedis简单示例

引入依赖:

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.7.0</version>
        </dependency>

代码示例:

package com.example.redisdemo;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;

import java.util.Map;

@SpringBootTest
class RedisDemoApplicationTests {

    private Jedis jedis;

   @BeforeEach
    void setUp(){
       jedis = new Jedis("localhost",6379);
//密码
//       jedis.auth("123");
       jedis.select(0);
   }

   @Test
    void testString(){
       String result  = jedis.set("name","张四");
       System.out.println("result="+result);

       String name = jedis.get("name");
       System.out.println("name="+name);
   }

   @Test
   void testHash(){
       jedis.hset("user:1","name","Jack");
       jedis.hset("user:1","age","21");

       Map<String,String> map = jedis.hgetAll("user:1");

       System.out.println(map);
   }

   @AfterEach
    void tearDown(){
       if(jedis !=null){
           jedis.close();
       }
   }

}

(2)Jedis连接池

在utils中写Jedis连接池的配置:

package com.example.redisdemo.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisConnectionFactory {

    private static  final JedisPool jedisPool;

    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(8);
        poolConfig.setMaxIdle(8);
        poolConfig.setMaxIdle(0);
        poolConfig.setMaxWaitMillis(1000);

        jedisPool = new JedisPool(poolConfig,"192.168.92.136",6379,1000,"123456");
    }

    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

在test中进行测试:

package com.example.redisdemo;

import com.example.redisdemo.utils.JedisConnectionFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;

import java.util.Map;

@SpringBootTest
class RedisDemoApplicationTests {

    private Jedis jedis;

   @BeforeEach
    void setUp(){
//       jedis = new Jedis("192.168.92.136",6379);
       jedis = JedisConnectionFactory.getJedis();
       jedis.auth("123456");
       jedis.select(0);
   }

   @Test
    void testString(){
       String result  = jedis.set("name","张四");
       System.out.println("result="+result);

       String name = jedis.get("name");
       System.out.println("name="+name);
   }

   @Test
   void testHash(){
       jedis.hset("user:1","name","Jack");
       jedis.hset("user:1","age","21");

       Map<String,String> map = jedis.hgetAll("user:1");

       System.out.println(map);
   }

   @AfterEach
    void tearDown(){
       if(jedis !=null){
           jedis.close();
       }
   }

}

三、SpringDataRedis

(1)快速入门实例:

1.引入依赖

        <!--        Redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
<!--        连接池依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

2.在application.yml中进行配置

server:
  port: 8080

spring:
  redis:
    host: 192.168.92.136
    port: 6379
    password: 123456
    lettuce:
      pool:
        max-active: 8 #最大连接
        max-idle: 8 #最大空闲连接
        min-idle: 0 #最小空闲连接
        max-wait: 100 #连接等待时间

3.Test中编写示例

package com.example.redisdemo;

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 {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void testString(){
        redisTemplate.opsForValue().set("name","小龙");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println("name="+name);
    }

}

(2)RedisTemplate序列化

配置序列化 config.RedisConfig

package com.example.redisdemo.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;

@Configuration
public class RedisConfig {

   @Bean
    public  RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){

       RedisTemplate<String,Object> template = new RedisTemplate<>();
       //设置连接工厂
       template.setConnectionFactory(connectionFactory);
       //创建Json序列化工具
       GenericJackson2JsonRedisSerializer jsonRedisSerializer =new GenericJackson2JsonRedisSerializer();
       //设置Key的序列化
       template.setKeySerializer(RedisSerializer.string());
       template.setHashKeySerializer(RedisSerializer.string());
       //设置Value的序列化
       template.setValueSerializer(jsonRedisSerializer);
       template.setHashValueSerializer(jsonRedisSerializer);

       return template;
   }

}

创建实体 pojo.User

package com.example.redisdemo.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private Integer age;
}

在Test中编写测试用例

package com.example.redisdemo;

import com.example.redisdemo.pojo.User;
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 {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Test
    void testString(){
        redisTemplate.opsForValue().set("name","小龙");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println("name="+name);
    }

    @Test
    void testSaveUser(){
        redisTemplate.opsForValue().set("user:100",new User("小虎",21));
        User user = (User) redisTemplate.opsForValue().get("user:100");
        System.out.println("user="+user);
    }


}

报错可能没有引入Jackson依赖

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

(3)序列化的另一种方式StringRedisTemplate

示例代码:

package com.example.redisdemo;

import com.example.redisdemo.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.StringRedisTemplate;

import java.util.Map;

@SpringBootTest
class RedisDemoApplicationTests {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //Json工具
    private static final ObjectMapper mapper = new ObjectMapper();

    @Test
    void testStringTemplate() throws JsonProcessingException{
        User user = new User("小明",20);
        //手动序列化
        String json = mapper.writeValueAsString(user);

        stringRedisTemplate.opsForValue().set("user:200",json);

        String val = stringRedisTemplate.opsForValue().get("user:200");
        //反序列化
        User user1 = mapper.readValue(val, User.class);
        System.out.println("user1:"+user1);
    }

    @Test
    void testHash(){
        stringRedisTemplate.opsForHash().put("user:400","name","小胡");
        stringRedisTemplate.opsForHash().put("user:400","age","21");
        
        Map<Object,Object> entries = stringRedisTemplate.opsForHash().entries("user:400");
        System.out.println("entries:"+entries);
    }

}
相关推荐
陌夏微秋8 分钟前
STM32单片机芯片与内部47 STM32 CAN内部架构 介绍
数据库·stm32·单片机·嵌入式硬件·架构·信息与通信
计算机学无涯1 小时前
Spring事务回滚
数据库·sql·spring
web130933203981 小时前
flume对kafka中数据的导入导出、datax对mysql数据库数据的抽取
数据库·kafka·flume
张声录11 小时前
【ETCD】【实操篇(二十)】浅谈etcd集群管理的艺术:从两阶段配置到灾难恢复的设计原则
数据库·etcd
qq_254674411 小时前
数据仓库和数据湖 数据仓库和数据库
数据库·数据仓库
--FGC--2 小时前
【第2篇】 Python与数据库基础
数据库·python·oracle
Y.O.U..3 小时前
Mysq学习-Mysql查询(4)
数据库·学习·mysql
安晴晚风3 小时前
从0开始在linux服务器上部署SpringBoot和Vue
linux·运维·前端·数据库·后端·运维开发
play_big_knife5 小时前
鸿蒙项目云捐助第二十八讲云捐助项目首页组件云数据库加载轮播图
数据库·华为·harmonyos·鸿蒙·云开发·鸿蒙开发·鸿蒙技术
qq_321665336 小时前
mysql 数据库迁移到达梦数据库
数据库·mysql