欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot與Spring中數(shù)據(jù)緩存Cache超詳細(xì)講解

 更新時(shí)間:2022年10月28日 09:45:14   作者:showswoller  
我們知道內(nèi)存讀取速度遠(yuǎn)大于硬盤(pán)讀取速度,當(dāng)需要重復(fù)獲取相同數(shù)據(jù)時(shí),一次一次的請(qǐng)求數(shù)據(jù)庫(kù)或者遠(yuǎn)程服務(wù),導(dǎo)致在數(shù)據(jù)庫(kù)查詢或者遠(yuǎn)程方法調(diào)用上小號(hào)大量的時(shí)間,最終導(dǎo)致程序性能降低,這就是數(shù)據(jù)緩存要解決的問(wèn)題,學(xué)過(guò)計(jì)算機(jī)組成原理或者操作系統(tǒng)的同學(xué)們應(yīng)該比較熟悉

一、Spring緩存支持

Spring框架定義了org.springframework.cache CacheManager和org.springframework.cache.Cache接口來(lái)統(tǒng)一不同的緩存技術(shù)

CacheManager常用方法如下

1、@Cacheable

該注解可以標(biāo)記在一個(gè)方法上,也可以標(biāo)記在一個(gè)類上,當(dāng)標(biāo)記在一個(gè)方法上時(shí)表示該方法是支持緩存的,當(dāng)標(biāo)記在一個(gè)類上時(shí)則表示該類所有的方法都是支持緩存的。對(duì)于一個(gè)支持緩存的方法,在方法執(zhí)行前,Spring先檢查緩存中是否存在方法返回的數(shù)據(jù),如果存在則直接返回緩存數(shù)據(jù),如果不存在,則調(diào)用方法并將方法返回值存入緩存

2、@CacheEvict

該注解用來(lái)標(biāo)注在需要清楚緩存元素的方法或類上,當(dāng)標(biāo)記在一個(gè)類上時(shí),表示其中所有方法的執(zhí)行都會(huì)觸發(fā)緩存的清除操作

3、@CachePut

該注解也可以聲明一個(gè)方法支持緩存功能

4、Caching

該注解可以在一個(gè)方法或類上同時(shí)指定多個(gè)Spring Cache相關(guān)的注解

5、CacheConfig

該注解作用在類上可以設(shè)置當(dāng)前緩存的一些公共設(shè)置

二、Spring Boot緩存支持

1:創(chuàng)建基于spring-voot-starter-cache 和spring-boot-starter-data-jpa依賴的Spring BootWeb應(yīng)用

2:配置application.properties文件 代碼如下

server.servlet.context-path=/ch6_10
###
##數(shù)據(jù)源信息配置
###
#數(shù)據(jù)庫(kù)地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
#數(shù)據(jù)庫(kù)用戶名
spring.datasource.username=root
#數(shù)據(jù)庫(kù)密碼
spring.datasource.password=root
#數(shù)據(jù)庫(kù)驅(qū)動(dòng)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
####
#JPA持久化配置
####
#指定數(shù)據(jù)庫(kù)類型
spring.jpa.database=MYSQL
#指定是否在日志中顯示SQL語(yǔ)句
spring.jpa.show-sql=true
#指定自動(dòng)創(chuàng)建、更新數(shù)據(jù)庫(kù)表等配置,update表示如果數(shù)據(jù)庫(kù)中存在持久化類對(duì)應(yīng)的表就不創(chuàng)建,不存在就創(chuàng)建對(duì)應(yīng)的表
spring.jpa.hibernate.ddl-auto=update
#讓控制器輸出的JSON字符串格式更美觀
spring.jackson.serialization.indent-output=true 

3:修改pom.xml文件 添加mysql依賴

<?xml version="1.0" encoding="UTF-8"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
-<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.ch</groupId>
<artifactId>ch6_10</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ch6_10</name>
<description>Demo project for Spring Boot</description>
-<properties>
<java.version>11</java.version>
</properties>
-<dependencies>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加MySQL依賴 -->
-<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
<!-- MySQL8.x時(shí),請(qǐng)使用8.x的連接器 -->
</dependency>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
-<build>
-<plugins>
-<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

4:創(chuàng)建持久化實(shí)體類

代碼如下

package com.ch.ch6_10.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = "student_table")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer"})
public class Student implements Serializable{
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;//主鍵
	private String sno;
	private String sname;
	private String ssex;
	public Student() {
		super();
	}
	public Student(int id, String sno, String sname, String ssex) {
		super();
		this.id = id;
		this.sno = sno;
		this.sname = sname;
		this.ssex = ssex;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSsex() {
		return ssex;
	}
	public void setSsex(String ssex) {
		this.ssex = ssex;
	}  
}

5:創(chuàng)建數(shù)據(jù)訪問(wèn)接口

package com.ch.ch6_10.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.ch.ch6_10.entity.Student;
public interface StudentRepository extends JpaRepository<Student, Integer>{
}

6:創(chuàng)建業(yè)務(wù)層 包括一個(gè)接口和一個(gè)實(shí)現(xiàn)類

接口代碼如下

package com.ch.ch6_10.service;
import com.ch.ch6_10.entity.Student;
public interface StudentService {
	public Student saveStudent(Student student);
	public void deleteCache(Student student);
	public Student selectOneStudent(Integer id);
}

實(shí)現(xiàn)類代碼如下

package com.ch.ch6_10.service;
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.stereotype.Service;
import com.ch.ch6_10.entity.Student;
import com.ch.ch6_10.repository.StudentRepository;
@Service
public class StudentServiceImpl implements StudentService{
	@Autowired
	private StudentRepository studentRepository;
	@Override
	@CachePut(value = "student", key="#student.id")
	public Student saveStudent(Student student) {
		Student s = studentRepository.save(student);
		System.out.println("為key=" + student.getId() + "數(shù)據(jù)做了緩存");
		return s;
	}
	@Override
	@CacheEvict(value = "student", key="#student.id")
	public void deleteCache(Student student) {
		System.out.println("刪除了key=" + student.getId() + "的數(shù)據(jù)緩存");
	}
	@Override
	@Cacheable(value = "student")
	public Student selectOneStudent(Integer id) {
		Student s = studentRepository.getOne(id);
		System.out.println("為key=" + id + "數(shù)據(jù)做了緩存");
		return s;
	}
}

7:創(chuàng)建控制器層

package com.ch.ch6_10.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ch.ch6_10.entity.Student;
import com.ch.ch6_10.service.StudentService;
@RestController
public class TestCacheController {
	@Autowired
	private StudentService studentService;
	@RequestMapping("/savePut")
	public Student save(Student student) {
		return studentService.saveStudent(student);
	}
	@RequestMapping("/selectAble")
	public Student select(Integer id) {
		return studentService.selectOneStudent(id);
	}
	@RequestMapping("/deleteEvict")
	public String deleteCache(Student student) {
		studentService.deleteCache(student);
		return "ok";
	}
}

8:在主類中開(kāi)啟緩存支持

package com.ch.ch6_10;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching
@SpringBootApplication
public class Ch610Application {
	public static void main(String[] args) {
		SpringApplication.run(Ch610Application.class, args);
	}
}

到此這篇關(guān)于SpringBoot與Spring中數(shù)據(jù)緩存Cache超詳細(xì)講解的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)緩存Cache內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java設(shè)計(jì)模式理解依賴于抽象不依賴具體的分析

    java設(shè)計(jì)模式理解依賴于抽象不依賴具體的分析

    這篇文章主要為大家介紹了java設(shè)計(jì)模式的規(guī)則,理解依賴于抽象不依賴具體的示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • 關(guān)于jd-gui啟動(dòng)報(bào)This?program?requires?Java?1.8+的錯(cuò)誤問(wèn)題及解決方法

    關(guān)于jd-gui啟動(dòng)報(bào)This?program?requires?Java?1.8+的錯(cuò)誤問(wèn)題及解決方法

    最近,在Mac使用上JD-GUI啟動(dòng)時(shí)總是報(bào)錯(cuò),接下來(lái)通過(guò)本文給大家介紹關(guān)于jd-gui啟動(dòng)報(bào)this?program?requires?Java?1.8+的錯(cuò)誤問(wèn)題及解決方法,需要的朋友可以參考下
    2022-05-05
  • Spring?MVC的三種異常處理方式實(shí)例詳解

    Spring?MVC的三種異常處理方式實(shí)例詳解

    在SpringMVC 中,不管是編譯異常還是運(yùn)行時(shí)異常,都可以最終由 SpringMVC提供的異常處理器進(jìn)行統(tǒng)一處理,這樣就避免了隨時(shí)隨地捕獲處理的繁瑣性,這篇文章主要介紹了Spring?MVC的三種異常處理方式?,需要的朋友可以參考下
    2024-01-01
  • Springboot接收前端的Json但是為null問(wèn)題

    Springboot接收前端的Json但是為null問(wèn)題

    這篇文章主要介紹了Springboot接收前端的Json但是為null問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>
    2024-05-05
  • elasticsearch插件開(kāi)發(fā)教程

    elasticsearch插件開(kāi)發(fā)教程

    這篇文章主要為大家詳細(xì)介紹了elasticsearch插件開(kāi)發(fā)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • JAVA使用geotools讀取shape格式文件的方法

    JAVA使用geotools讀取shape格式文件的方法

    這篇文章主要介紹了JAVA使用geotools讀取shape格式文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2017-01-01
  • Java調(diào)用明華RF讀寫(xiě)器DLL文件過(guò)程解析

    Java調(diào)用明華RF讀寫(xiě)器DLL文件過(guò)程解析

    這篇文章主要介紹了Java調(diào)用明華RF讀寫(xiě)器DLL文件過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java各種排序算法匯總(冒泡,選擇,歸并,希爾及堆排序等)

    Java各種排序算法匯總(冒泡,選擇,歸并,希爾及堆排序等)

    這篇文章主要介紹了Java各種排序算法,以大量實(shí)例形式匯總分析了Java常用的各種排序算法,包括冒泡排序、快速排序、堆排序、插入排序、希爾排序、選擇排序、歸并排序等,需要的朋友可以參考下
    2015-11-11
  • SpringBoot集成支付寶支付的實(shí)現(xiàn)示例

    SpringBoot集成支付寶支付的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot集成支付寶支付的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • SpringMVC打印請(qǐng)求參數(shù)和響應(yīng)數(shù)據(jù)最優(yōu)方案

    SpringMVC打印請(qǐng)求參數(shù)和響應(yīng)數(shù)據(jù)最優(yōu)方案

    項(xiàng)目中經(jīng)常需要打印http請(qǐng)求的參數(shù)和響應(yīng)數(shù)據(jù),本文給大家講解如何在SpringMVC打印請(qǐng)求參數(shù)和響應(yīng)數(shù)據(jù)最優(yōu)方案,感興趣的朋友跟隨小編一起看看吧
    2023-07-07

最新評(píng)論