eyb:Redis学习(4)


目录:

(1)SpringDataRedis项目搭建

(2)SpringDataRedis序列化模板

(3)SpringDataRedis操作string类型数据

(4)SpringDataRedis操作hash类型数据

(5)SpringDataRedis操作list类型数据

(6)SpringDataRedis操作Set类型数据

(7)SpringDataRedis操作sortedset类型数据

(8)SpringDataRedis获取所有key+设置key的失效时间

(9)SpringDataRadis整合哨兵

(10)缓存击穿

(11)缓存穿透 

(12)缓存雪崩


 

lettuce的学习: 

(1)SpringDataRedis项目搭建

创建springboot项目:

删除没用的目录:看起来简洁一点

 

 

 

 这里使用Redis默认的lettuce:需要另外引入连接池依赖

引入依赖:pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.xxxx</groupId>
    <artifactId>springdataredis-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springdataredis-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--spring-data-redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--commmons-pool2对象池依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <!--web组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--test组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

   

</project>

配置文件application.yml:

spring:
  redis:
    # Redis服务器地址
    host: 192.168.67.128
    # Redis服务端口
    port: 6379
    # 设置redis服务器密码
    password: root
    # 选择那个库,默认是0
    database: 0
    # 连接超时时间
    timeout: 10000ms
    jedis:
      pool:
        # 最大连接数,默认18
        max-active: 1024
        # 最大连接阻塞时间,单位为毫秒,默认-1ms
        max-wait: 10000ms
        # 最大空闲连接,默认8
        max-idle: 200
        # 最小空闲连接
        min-idle: 5

连接Redis:

 

package com.xxxx.springdataredisdemo;

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 SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void initConnt() {
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("name","lulu");
    }

}

存入的数据:发现存入的数据看不懂

修改测试类:

package com.xxxx.springdataredisdemo;

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 SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Test
    public void initConnt() {
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("name","lulu");
    }

}

 

package com.xxxx.springdataredisdemo;

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

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void initConnt() {
        //ValueOperations ops = redisTemplate.opsForValue();
        //ops.set("name","lulu");

        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        stringStringValueOperations.set("age","20");
        String age = stringStringValueOperations.get("age");
        System.out.println(age);
    }

}

 

 (2)SpringDataRedis序列化模板

 使用自定义序列化模板

创建RedisConfig

package com.xxxx.springdataredisdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        //为string类型的key序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //为string类型的value设置序列化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //为hash类型的key序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //为hash类型的value设置序列化
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        //放连接工厂
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        return redisTemplate;
    }
}
package com.xxxx.springdataredisdemo;

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

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

   

    //测试序列化
    @Test
    public void  testSerial(){
        User user= new User();
        user.setId(1);
        user.setName("wangwu");
        user.setAge(20);
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("user",user);
        //获取
        Object user1 = ops.get("user");
        System.out.println(user1);
    }

}

 

 (3)SpringDataRedis操作string类型数据

package com.xxxx.springdataredisdemo;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;


    //操作string类型
    @Test
    public void testString(){
        //opsforValue()就是string类型
        ValueOperations ops = redisTemplate.opsForValue();
        //添加一条数
        ops.set("name","zhansan");
        //获取一条数据
        String name =(String) ops.get("name");
        System.out.println(name);
        //层级关系存储数据
        ops.set("user:01","lisi");

        //一次添加多条数据
        Map<String,String> map=new HashMap<>();
        map.put("age","20");
        map.put("address","sh");
        ops.multiSet(map);
        //一次获取多条数据
        List<String> keys=new ArrayList<>();
        keys.add("name");
        keys.add("age");
        keys.add("address");
        List list = ops.multiGet(keys);
        list.forEach(System.out::println);

        //删除数据
        redisTemplate.delete("name");
    }

}

 

(4)SpringDataRedis操作hash类型数据

 

package com.xxxx.springdataredisdemo;

import com.xxxx.springdataredisdemo.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.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;



    //操作hash类型数据
    @Test
    public void testHash(){
        //opsForHash类型
        HashOperations hashOperations = redisTemplate.opsForHash();
        //添加一条数据 第一个参数hash的key 第二个参数hash的key 第三个参数hash的value
        hashOperations.put("student","name","zhangsan");
        //获取一条数据 第一个参数Redis的key 第二个参数hash的key
        String name =(String) hashOperations.get("student", "name");
        System.out.println(name);

        //添加多条数据
        Map<String,String> map=new HashMap<>();
        map.put("age","20");
        map.put("address","bj");
        hashOperations.putAll("student",map);
        //一次获取多条数据
        List<String> keys=new ArrayList<>();
        keys.add("name");
        keys.add("age");
        keys.add("address");
        List students = hashOperations.multiGet("student", keys);
        students.forEach(System.out::println);

        //获取hash类型的所有数据
        Map<String,String> entries = hashOperations.entries("student");
        entries.entrySet().forEach(e->{
            System.out.println(e.getKey()+"==="+e.getValue());
        });

        //hash自带删除
        hashOperations.delete("student","name");
    }

}

 

(5)SpringDataRedis操作list类型数据

 

package com.xxxx.springdataredisdemo;

import com.xxxx.springdataredisdemo.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.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;



    //操作list类型数据
    @Test
    public void testList(){
        ListOperations listOperations = redisTemplate.opsForList();
        //左添加
        listOperations.leftPush("peoples","wangwu");
        listOperations.leftPush("peoples","lisi");
        /*
        * 左添加
        * 第一个参数:redis的key
        * 第二个参数:被左添加的数据
        * 第三个参数:添加的数据,添加到第二个参数的左边
        * */
        listOperations.leftPush("peoples","wangwu","zhangsan");

        //右添加
        listOperations.rightPush("peoples","zhaoliu");
        //获取数据
        List list = listOperations.range("students", 0, 2);
        list.forEach(System.out::println);

        //获取总条数
        Long size = listOperations.size("peoples");
        System.out.println(size);

        
        /*//删除数据
        listOperations.remove("peoples",1,"lisi");

        //左弹出(删除)
        listOperations.leftPop("peoples");
        //右弹出(删除)
        listOperations.rightPop("peoples");*/

    }

}

 

 

package com.xxxx.springdataredisdemo;

import com.xxxx.springdataredisdemo.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.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;



    //操作list类型数据
    @Test
    public void testList(){
        ListOperations listOperations = redisTemplate.opsForList();
        /*
        //左添加
        listOperations.leftPush("peoples","wangwu");
        listOperations.leftPush("peoples","lisi");
        /*
        * 左添加
        * 第一个参数:redis的key
        * 第二个参数:被左添加的数据
        * 第三个参数:添加的数据,添加到第二个参数的左边
        * */
        listOperations.leftPush("peoples","wangwu","zhangsan");

        //右添加
        listOperations.rightPush("peoples","zhaoliu");
        //获取数据
        List list = listOperations.range("peoples", 0, 3);
        list.forEach(System.out::println);

        //获取总条数
        Long size = listOperations.size("peoples");
        System.out.println(size);
        */

        //删除数据
        listOperations.remove("peoples",1,"lisi");

        //左弹出(删除)
        listOperations.leftPop("peoples");
        //右弹出(删除)
        listOperations.rightPop("peoples");

    }

}

 (6)SpringDataRedis操作Set类型数据

 

package com.xxxx.springdataredisdemo;

import com.xxxx.springdataredisdemo.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.*;

import java.util.*;

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

  

    //操作set类型数据
    @Test
    public void testSet(){
        //set类型
        SetOperations setOperations = redisTemplate.opsForSet();
        //添加数据
        //setOperations.add("letters","aaa","bbb","ccc","ddd","eee");
        String[] letters=new String[]{"aaa","bbb","ccc","ddd","eee"};
        setOperations.add("letters",letters);
        //获取数据
        Set set = setOperations.members("letters");
        set.forEach(System.out::println);

        //删除数据
        setOperations.remove("letters","aaa","bbb");

    }

}

 

  (7)SpringDataRedis操作sortedset类型数据

 

package com.xxxx.springdataredisdemo;

import com.xxxx.springdataredisdemo.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.*;

import java.util.*;

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;


    //操作sortedset类型数据
    @Test
    public void sortedSet(){
        //sortedset类型
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        ZSetOperations.TypedTuple<Object> objectTypedTuple1=new DefaultTypedTuple<>("zhandsan",7D);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2=new DefaultTypedTuple<>("lisi",3D);
        ZSetOperations.TypedTuple<Object> objectTypedTuple3=new DefaultTypedTuple<>("wangwu",5D);
        ZSetOperations.TypedTuple<Object> objectTypedTuple4=new DefaultTypedTuple<>("zhaoliu",1D);
        ZSetOperations.TypedTuple<Object> objectTypedTuple5=new DefaultTypedTuple<>("tianqi",6D);

        Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        tuples.add(objectTypedTuple3);
        tuples.add(objectTypedTuple4);
        tuples.add(objectTypedTuple5);
        //添加数据
        zSetOperations.add("score",tuples);
        //获取数据
        Set score = zSetOperations.range("score", 0, 4);
        score.forEach(System.out::println);
        //获取总条数
        Long size = zSetOperations.size("score");
        System.out.println(size);

        //set删除数据
        zSetOperations.remove("score","zhangsan","lisi");

    }

}

 

 (8)SpringDataRedis获取所有key+设置key的失效时间

获取所有key :

package com.xxxx.springdataredisdemo;

import com.xxxx.springdataredisdemo.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.*;

import java.util.*;

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

   

    //获取所有keys
    @Test
    public void testAllKey(){
        //获取当前数据库所有的key
        Set keys = redisTemplate.keys("*");
        keys.forEach(System.out::println);
    }

}

 

 

 失效时间:

package com.xxxx.springdataredisdemo;

import com.xxxx.springdataredisdemo.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.*;

import java.util.*;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class SpringdataredisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

 

    //失效时间
    @Test
    public void testExpire(){
        ValueOperations ops = redisTemplate.opsForValue();
        //方法一,添加key的时候,设置失效时间
        ops.set("code","test",30,TimeUnit.SECONDS);

        //方法二,为已存在的key设置失效时间
        redisTemplate.expire("address",30,TimeUnit.SECONDS);

        //查看失效时间
        Long code = redisTemplate.getExpire("code");
        System.out.println(code);

    }

}

(9)SpringDataRadis整合哨兵

配置文件加:

 配置类加:RedisConfig:

 

 (10)缓存击穿

 

 

(11)缓存穿透

 (11)缓存雪崩

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值