Redis笔记

概述

Redis是一个内存数据库,也就是指存储到内存条上的数据,而MySQL是一个外存数据库,将数据库存储在硬盘(外存)中

一、Linux安装

  1. 下载完毕后,解压压缩包(自己找目录)

    tar -zxvf redis.版本.tar.gz
    
  2. 搭建基本环境

    //保证Redis正常运行
    yum install gcc -c++
    
    //查看版本
    g++ -v
    
    //安装Redis所需要的环境
    make
    
    make install
    
    
  3. Redis默认安装在/usr/local/bin

  4. 将Redis.config文件复制到该目录下

  5. 将redis.config中的 daemonize no,改为yes

  6. 运行

    cd /usr/local/bin
    redis-server /usr/local/bin/myconfig/redis.conf
    

二、基础语法

1.基础语法(命令小写) 作用

set key value 设置一个key
get key 获取一个key对应value
exists key 查询key是否存在
move key n(n是数字) 将当前key移动到指定的几号数据库中
keys * 查询当前数据库中全部的key
expire key time 设置当前key的过期时间
ttl key 查询当前key的存活时间
type key 查看key的数据类型
flushdb 清空当前数据库信息(慎用)
flushall 空16个数据库中的全部信息(慎用)
select n 选择数据库

2.五种基本类型

  1. string(字符串)

  2. hash(哈希)

  3. list(列表)

    链表

  4. set(集合)

    通过散列表保证自己每个字符不相同

  5. zset(有序集合)

三、连接服务器Redis

  1. 确定在服务器自身访问正常

  2. 关闭服务器防火墙

  3. 开通redis(6379)端口号

  4. 注释 掉bind 127.0.0.1

  5. 修改 protected-mode yesno

  6. 设置密码

    requirepass 密码
    
  7. 引入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
    
  8. 配置yml/properties

    spring:
      redis:
        host: localhost # Redis服务器地址
        database: 0 # Redis数据库索引(默认为0)
        port: 6379 # Redis服务器连接端口
        password: root # Redis服务器连接密码(默认为空)
        jedis:
          pool:
            max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
            max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
            max-idle: 8 # 连接池中的最大空闲连接
            min-idle: 0 # 连接池中的最小空闲连接
        timeout: 3000ms # 连接超时时间(毫秒)
    
  9. 测试

    import org.junit.Test;
    import org.junit.runner.RunWith;
    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 org.springframework.data.redis.core.ValueOperations;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ApplicationTest {
        @Autowired
        private StringRedisTemplate redisTemplate;
    	/*
    	存数据
    	*/
        @Test
        public void set() {
            ValueOperations<String,String> ops = redisTemplate.opsForValue();    // redisTemplate.opsForValue的目的就是表明是以key,value形式储存到Redis数据库中数据的
            ops.set("userName","jack");// 到这里就表明Redis数据库中存储了key为userName,value为jack
        }
    
        /*
         取数据
         */
        @Test
        public void get() {
            ValueOperations<String,String> ops = redisTemplate.opsForValue();  // 表明取的是key,value型的数据
            Object userName = ops.get("userName");  // 获取Redis数据库中key为address1对应的value数据
            System.out.println(userName);
        }
    
    }
    
  10. 注解的使用

    @Cacheable(key = "#tel",value = "code" )
    public String getCode(String tel) {
            Random random = new Random();
            int code = random.nextInt(9999);
            if (code<1000){
                code=code+1000;
            }
            String arrays = String.valueOf(code);
            redisTemplate.opsForValue().set(tel,arrays);
            return redisTemplate.opsForValue().get(tel);
    }
    /*
    该注解的键为 code::tel传过来的值
    		值为,return出去的值
    */
    
  11. 插入值类型不对

        @Autowired
        private RedisTemplate<String,String> redisTemplate;
    
        public String getCode(String tel) {
            Random random = new Random();
            int code = random.nextInt(9999);
            if (code<1000){
                code=code+1000;
            }
            String arrays = String.valueOf(code);
            /根据值和键设置,放入redis
            redisTemplate.opsForValue().set(tel,arrays);
            //根据键获取值
            return redisTemplate.opsForValue().get(tel);
        }
    

Redis三大件

1.缓存穿透

  • 描述:

    ​ 当访问一个空的key时,先走缓存,缓存不存在,再去数据库也不存在该key,无法添加到缓存中,那么每次访问该key,就会导致服务器压力过大

  • 解决办法:

    ​ 1. 可以在redis中添加一个空key,防止进入数据库服务器

    ​ 2.可以使用布隆过滤器将空key过滤掉

2.缓存击穿

  • 描述:

    ​ 在一个热点key过期,导致大量请求直接访问数据库,可能导致数据库崩塌

  • 解决办法:

    ​ 1.使用热点数据不过期,定时更新缓存

    ​ 2.加互斥锁,让一个线程去访问数据库,其他线程从缓存中获取

3.缓存雪崩

  • 描述:

    ​ 在系统运行时,突然缓存服务器宕机了,或者大量的key过期,导致短时间内所有请求去访问数据库,导致数据库服务器压力过大

  • 解决办法:

    ​ 1.将key的过期时间打散

    ​ 2.加互斥锁

posted @   DawsonDragon  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示