• Redis+整合SpringDataRedis


    Nosql和缓存的背景

    数据库架构设计的发展史
    第一阶段:单库:随着访问量的增加出现了性能问题
    第二阶段:缓存:通过缓存,缓解数据库的压力,优化数据结构和索引
    第三阶段:读写分离:数据写入的压力增加,主从复制和读写分离的方案进入视野
    第四阶段:分库分表:主表的写压力过高,开始使用InnoDB引擎,以及分库分表技术

    核心问题:
    传统数据库的扩展性差(需要复杂的技术来实现),大数据下IO压力大,表结构更加困难

    持久化方式

    持久化:将数据(如内存中的对象)保存到可永久保存的存储设备中

    方式一:RDB方式
    在指定的时间间隔内对数据进行快照存储。先将数据集写入临时文件,写入成功后,再替换之前的文件,用二进制压缩存储,是一次的全量备份
    方式二:AOF方式
    以日志文本的形式记录服务器所处理的每一个数据更改指令,然后通过重放来恢复数据,是连续的增量备份

    Redis整合SpringDataRedis

    Spring Data是Spring公司的顶级项目,里面包含了N多个二级子项目,这些子项目都是相当独立的项目。每个子项目是对不同API的封装
    所有Spring Boot整合Spring Data xxx的启动器都叫做spring-boot-starter-data-xxx
    Spring Data好处很方便操作对象类型。
    把Redis不同值的类型放到一个opsForxxx方法中
    opsForValue:String值
    opsForList:列表List
    opsForHash:哈希表Hash
    opsForZSet:有序集合Sorted Set
    opsForSet:集合
    使用步骤:
    1.添加依赖

     
             <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-redisartifactId>
                <version>2.5.0version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.配置文件

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/userdb?characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=123456
    
    mybatis.type-aliases-package=com.zuxia.entity
    mybatis.mapper-locations=classpath:mapper/*.xml
    
    #配置redis 服务参数
    spring.redis.database=1
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.password=
    
    spring.redis.jedis.pool.max-active=80
    spring.redis.jedis.pool.max-wait=-1
    
    spring.redis.jedis.pool.max-idle=50
    
    spring.redis.jedis.pool.min-idle=30
    
    spring.redis.timeout=500
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.编写配置类

    @Configuration
    public class RedisConfig {
        @Bean
        public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
            RedisTemplate<String ,Object> redisTemplate=new RedisTemplate<>();
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
            redisTemplate.setConnectionFactory(factory);
            return redisTemplate;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. Controller类
    package com.zuxia.controller;
    
    import com.sun.org.apache.xpath.internal.operations.Mod;
    import com.zuxia.entity.Product;
    import com.zuxia.service.IProduct;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class ProductController {
        @Autowired
        private IProduct iProduct;
        @GetMapping("/show")
        public String select(Integer id, Model model){
            Product product = iProduct.findByID(id);
            model.addAttribute("product",product);
            return "show";
        }
        @RequestMapping("/del")
        @ResponseBody
        public String del(Integer id, Model model){
            int i=iProduct.del(id);
            if(i>0){
                return "删除成功";
            }
            return "删除失败";
        }
        @RequestMapping("/update ")
        @ResponseBody
        public String update(Product product, Model model){
            int i=iProduct.update(product);
            if(i>0){
                return "修改成功";
            }
            return "修改失败";
        }
        @RequestMapping("/add")
        @ResponseBody
        public String Add(Product product, Model model){
            int i=iProduct.insert(product);
            if(i>0){
                return "增加成功";
            }
            return "曾加失败";
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    1. Service类
    package com.zuxia.service.impl;
    
    import com.zuxia.entity.Product;
    import com.zuxia.mapper.ProductMapper;
    import com.zuxia.service.IProduct;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.stereotype.Service;
    
    
    @Service
    public class ProductServiceImpl implements IProduct {
        @Autowired(required = false)
        private ProductMapper productMapper;
        @Autowired
        private RedisTemplate<String,Object> redisTemplate;
        @Override
        public Product findByID(Integer id) {
            String key="product:"+id;
            //先从redis中获取数据
            if(redisTemplate.hasKey(key)){//判断id是否在缓存中存在
                System.out.println("执行缓存");
                //设置value的值
                redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Product>(Product.class));
                //从缓存中获取数据
                Product product=(Product) redisTemplate.opsForValue().get(key);
                return product;
            }
            System.out.println("执行mysql数据查询");
            Product product=productMapper.findByID(id);
            //将查到的值存入缓存中
            redisTemplate.opsForValue().set(key,product);
            return product;
        }
    
        @Override
        public int del(Integer id) {
            int i=productMapper.del(id);
            String key="prodoct:"+id;
            //判断缓存中是否存在
            boolean ret=  redisTemplate.hasKey(key);
            if(ret){
                System.out.println("缓存中的数据删除了");
                redisTemplate.delete(key);
            }
            return i;
        }
    
        @Override
        public int update(Product product) {
    
            int i=productMapper.update(product);
            if(i>0){
               String key="product"+product.getId();
               if(redisTemplate.hasKey(key)){
                   System.out.println("缓存的数据更改了");
                   redisTemplate.opsForValue().set(key,product);
               }
            }
            return i;
        }
    
        @Override
        public int insert(Product product) {
            return productMapper.insert(product);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
  • 相关阅读:
    细说react源码中的合成事件
    【Spring注解大全】
    QT网络协议知识体系(一)
    我们应该用中文编程
    Java技术栈中的核心组件:Spring框架的魔力
    idea必装的插件Maven Help(解决依赖冲突)
    【算法专题突破】滑动窗口 - 水果成篮(13)
    学习 RabbitMQ 这一篇就够了
    JavaWeb(四)
    【Redis】数据过期策略和数据淘汰策略
  • 原文地址:https://blog.csdn.net/qq_45429856/article/details/121362831