SpringBoot整合篇(一)--- (缓存)Cache(Redis)

摘要:
时间过得很快,不知不觉就即将迎来崭新而充满期望的2020年,这篇也是我新一年崭新开始的代表;我会以最高的期待要求自己迎接崭新的一年;我们身为程序猿;以性能、安全、简洁明了的代码为目标;以不以物喜,不以己悲的心态为坚守,每天都是新的挑战,让我们以最高傲的激情面对无理的需求。好啦;鸡汤已喝,能量也该满了;那么接下来就为新时代到来而奋斗吧!SpringBoot这大框架已用了一年之久了;我感觉就一句话;全在脑海里;只能自我体会。但是,我觉得每个知识的学习,都是先从实列琢磨出原理的;所以实践也是非常重要的,所以我将竭尽所能将每篇的内容融汇我所想的,我所体会到的思想。也希望大家多多提问。

一、Cache(缓存)

(1)、作用
像京东、淘宝的商品网站;如果每次我访问的商品都是走数据库的话;那肯定是不行的;如果用缓存的话;那么我再次访问的时候就不会再去查询数据库了,而是走的缓存中查;所以缓存的起步点就是为了加速访问我们的项目;当然也将会提高项目的性能问题;这也是缓存出现的目的。
(2)、介绍
目前缓存技术用到的两大缓存(JSR107、spring缓存抽象)首先我们先看JSR107缓存下图所示:
在这里插入图片描述
JSR107涉及不广泛;因为它是面向接口编程的;操作接口的限制导致开发难度加大;所以我们在企业都是使用spring缓存抽象;因为在spring3.1版本开始统一不同的缓存技术;只定义了两大接口实现缓存;如图所示:
在这里插入图片描述
当然它也支持JSR107注解式开发;大多数为了我们便于系统开发;就规范缓存的几个核心接口;在SpringBoot框架中;作为缓存究竟是如何进行的;我们又如何在springboot中使用呢。下面就以spring缓存抽象来正式测试。

二、演示

接下来;就拿代码为大家做个演示在springboot框架中如何使用。从上面的图中咋们就可得知缓存管理器;现在从springboot的autoconfigure自动配置类入手;使用IDEA工具新建项目:new -> Project -> Spring Initializr;勾选一些前期需要初始化的框架;列如:mybatis、整合web(勾选就会初始化mvc模式);这里就不一 一列举了;建好新项目后;我们就修改properties文件加上:
server.port=8080 debug=true两句话;然后根据应用启动跑起来;查看启动日志: 我们就会发现在这里插入图片描述原来该springboot启动时;默认启动配置类SimpleCacheConfiguration;这个类就是默认缓存配置类;下面就来演示默认的ConcurrentMapCacheManager使用:
先把数据库表给建立起来;我就建张dept部门表作为演示吧:
在这里插入图片描述
连接数据库:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lp_implement?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

编写domain、mapper、controller如下:

package com.lp.cache.domain;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data  //使用Lombok插件无需再写set和get方法
@NoArgsConstructor
public class Dept {
    private Integer id;  //部门ID
    private String  name; //部门名称
}

package com.lp.cache.mapper;

import com.lp.cache.domain.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/***
 * @author lp
 * @deprecated 部门的mapper类
 */

@Mapper
public interface DeptMapper {

    /**
     * 查询
     * @param id
     * @return
     */
    @Select("select * from dept WHERE id=#{id}")
    public Dept selectDept(Integer id);


    /**
     * 更新
     * @param dept
     * @return
     */
    @Update("update dept set name=#{name} WHERE id=#{id}")
    public Dept updateDept(Dept dept);

}

package com.lp.cache.controller;


import com.lp.cache.domain.Dept;
import com.lp.cache.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    DeptService deptService;

    @RequestMapping("/findById/{id}")
    public Dept findById(@PathVariable("id") Integer id){
        Dept dept = deptService.findDept(id);
        return dept;
    }

}

我们在使用注解式缓存需要将它开启:

package com.lp.cache;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@MapperScan("com.lp.cache.mapper")
@EnableCaching/*开启注解式缓存*/
public class CacheDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(CacheDemoApplication.class, args);
	}

}

关键的service层:

package com.lp.cache.service;

import com.lp.cache.domain.Dept;
import com.lp.cache.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class DeptService {
    @Autowired
    DeptMapper  deptMapper;
    
    @Cacheable(cacheNames = "dept") //将方法结果进行缓存;
    //cacheNames 属性是cache的名称;从图中咋们就知道缓存组件保存值是以key-value保存的;
    //key如果不指定,则缺省按照方法的所有参数进行组合(我这里就是id)
    public Dept findDept(Integer id){

        System.out.println("查询dept的Id:"+id);
        Dept dept = deptMapper.selectDept(id);

        return dept;
    }
}

下面就启动测试结果图:
在这里插入图片描述说明我们缓存是起作用了;那么现在就是Cache操作讲解。

三、注解操作讲解

在JSR107 的规范规则中;有注解式操作;而spring抽象缓存也支持JSR107注解式开发。它有以下常用注解式:
以上演示我们就用到了@Cacheable;我这里就不详情介绍了;这位兄弟单篇介绍注解的肯定比我详细:Spring Boot缓存注解

四、整合Redis

作为缓存热门的Redis;我就不详情介绍了;我这边就教如何整合步骤吧:
1)引入Redis-starter包

	<!--引入Redis的starter-->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
          <version>2.1.3.RELEASE</version>
      </dependency>

我们再来启动看下控制台的打印输出日志:
在这里插入图片描述默认的缓存配置类已经不匹配;查询下Redis的:
在这里插入图片描述很明显已经替换Redis类型。如何连接本地Redis服务呢;首先我们就得像本地安装Redis服务以及连接工具;我这里提供给你们:Redis本地服务链接
提取码:51ka
步骤:(1)双击install - window-Redis.bat安装本地Redis服务;并在后台管理服务将它开启。
(2)安装Redis操作Desktop界面连接工具。
连接Redis很像连接数据库;需要输入host ;我们打开Redis操作Desktop界面连接工具;输入本地后:
在这里插入图片描述
2)、修改properties文件

spring.redis.host=127.0.0.1

这样就算是已经连接上了;然后根据Redis的操作规范来进行数据的CURD(存取)就行了;那我们也要学习Redis的操作规则;先来看看Redis的操作类对象:
在这里插入图片描述
下面我就以原码service层方法上进行修改:

package com.lp.cache.service;

import com.lp.cache.domain.Dept;
import com.lp.cache.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class DeptService {

    @Autowired
    DeptMapper  deptMapper;

    @Autowired
    RedisTemplate redisTemplate;

    public Dept findDept(Integer id){

        System.out.println("查询dept的Id:"+id);
        Dept dept = deptMapper.selectDept(id);
        redisTemplate.opsForValue().append(dept,"dept");
        return dept;
    }
}

redisTemplate的操作规则学习:redisTemplate的操作规则这里更加详细的介绍了各种redisTemplate的操作方法;希望大家虚心学习;多问;多沟通,缓存的交流会就到此结束了;在此祝大家在新的一年里;所遇皆欢喜;所得皆如愿;2020,‘鼠’你最开心!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小面包CC

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

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

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

打赏作者

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

抵扣说明:

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

余额充值