最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、Spring緩存支持

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

CacheManager常用方法如下

1、@Cacheable

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

2、@CacheEvict

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

3、@CachePut

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

4、Caching

該注解可以在一個方法或類上同時指定多個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ù)庫地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
#數(shù)據(jù)庫用戶名
spring.datasource.username=root
#數(shù)據(jù)庫密碼
spring.datasource.password=root
#數(shù)據(jù)庫驅(qū)動
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
####
#JPA持久化配置
####
#指定數(shù)據(jù)庫類型
spring.jpa.database=MYSQL
#指定是否在日志中顯示SQL語句
spring.jpa.show-sql=true
#指定自動創(chuàng)建、更新數(shù)據(jù)庫表等配置,update表示如果數(shù)據(jù)庫中存在持久化類對應(yīng)的表就不創(chuàng)建,不存在就創(chuàng)建對應(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時,請使用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)建持久化實體類

代碼如下

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ù)訪問接口

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ù)層 包括一個接口和一個實現(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);
}

實現(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:在主類中開啟緩存支持

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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java獲取圖片的大小、寬度、高度方式

    java獲取圖片的大小、寬度、高度方式

    文章介紹了如何將File對象轉(zhuǎn)換為MultipartFile對象的過程,并分享了個人經(jīng)驗,希望能為讀者提供參考
    2025-02-02
  • java實現(xiàn)微信公眾號消息推送的方法詳解

    java實現(xiàn)微信公眾號消息推送的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用java實現(xiàn)微信公眾號消息推送的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • Java8 Comparator源碼演示及解析

    Java8 Comparator源碼演示及解析

    這篇文章主要介紹了Java8 Comparator源碼演示及解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Spring Boot外部化配置實戰(zhàn)解析

    Spring Boot外部化配置實戰(zhàn)解析

    這篇文章主要介紹了Spring Boot外部化配置實戰(zhàn)解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • Java中的CopyOnWriteArrayList容器解析

    Java中的CopyOnWriteArrayList容器解析

    這篇文章主要介紹了Java中的CopyOnWriteArrayList容器解析,CopyOnWriteArrayList容器允許并發(fā)讀,讀操作是無鎖的,性能較高。至于寫操作,比如向容器中添加一個元素,則首先將當(dāng)前容器復(fù)制一份,然后在新副本上執(zhí)行寫操作,需要的朋友可以參考下
    2023-12-12
  • Java終止線程的兩種方法

    Java終止線程的兩種方法

    本文主要介紹了Java終止線程的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java加權(quán)負(fù)載均衡策略實現(xiàn)過程解析

    Java加權(quán)負(fù)載均衡策略實現(xiàn)過程解析

    這篇文章主要介紹了Java加權(quán)負(fù)載均衡策略實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Java多線程ThreadAPI詳細(xì)介紹

    Java多線程ThreadAPI詳細(xì)介紹

    這篇文章主要介紹了Java多線程ThreadAPI詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • java TreeMap源碼解析詳解

    java TreeMap源碼解析詳解

    這篇文章主要介紹了java TreeMap源碼解析詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • spring boot與redis 實現(xiàn)session共享教程

    spring boot與redis 實現(xiàn)session共享教程

    這篇文章主要介紹了spring boot與redis 實現(xiàn)session共享教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-04-04

最新評論

郸城县| 磴口县| 谢通门县| 如东县| 神木县| 荆州市| 兴海县| 疏附县| 永康市| 长宁区| 永春县| 乐平市| 邵阳县| 柘城县| 伊川县| 郓城县| 古丈县| 金寨县| 勃利县| 左权县| 宝鸡市| 孙吴县| 阿克陶县| 介休市| 同仁县| 高要市| 报价| 名山县| 永安市| 平山县| 遂溪县| 宣威市| 大庆市| 西平县| 青铜峡市| 姜堰市| 萨迦县| 龙岩市| 邹城市| 赣州市| 龙里县|