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

SpringBoot中默認(rèn)緩存實(shí)現(xiàn)方案的示例代碼

 更新時(shí)間:2020年08月13日 17:25:55   作者:qfchenjunbo  
這篇文章主要介紹了SpringBoot中默認(rèn)緩存實(shí)現(xiàn)方案,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在上一節(jié)中,我?guī)Т蠹覍W(xué)習(xí)了詳解SpringBoot集成Redis來(lái)實(shí)現(xiàn)緩存技術(shù)方案,尤其是結(jié)合Spring Cache的注解的實(shí)現(xiàn)方案,接下來(lái)在本章節(jié)中,我?guī)Т蠹彝ㄟ^(guò)代碼來(lái)實(shí)現(xiàn)。

一. Spring Boot實(shí)現(xiàn)默認(rèn)緩存

1. 創(chuàng)建web項(xiàng)目

我們按照之前的經(jīng)驗(yàn),創(chuàng)建一個(gè)web程序,并將之改造成Spring Boot項(xiàng)目,具體過(guò)程略。

2. 添加依賴(lài)包

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
 
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
</dependency>
 
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

3. 創(chuàng)建application.yml配置文件

server:
 port: 8080
spring:
 application:
 name: cache-demo
 datasource:
 driver-class-name: com.mysql.cj.jdbc.Driver
 username: root
 password: syc
 url: jdbc:mysql://localhost:3306/spring-security?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&serverTimezone=UTC
 #cache:
 #type: generic #由redis進(jìn)行緩存,一共有10種緩存方案
 jpa:
 database: mysql
 show-sql: true #開(kāi)發(fā)階段,打印要執(zhí)行的sql語(yǔ)句.
 hibernate:
 ddl-auto: update

4. 創(chuàng)建一個(gè)緩存配置類(lèi)

主要是在該類(lèi)上添加@EnableCaching注解,開(kāi)啟緩存功能。

package com.yyg.boot.config;
 
import org.springframework.cache.annotation.EnableCaching;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/14
 * @Description Description
 * EnableCaching啟用緩存
 */ 
@Configuration
@EnableCaching
public class CacheConfig {
}

5. 創(chuàng)建User實(shí)體類(lèi)

package com.yyg.boot.domain;
 
import lombok.Data;
import lombok.ToString;
 
import javax.persistence.*;
import java.io.Serializable;
 
@Entity
@Table(name="user")
@Data
@ToString
public class User implements Serializable {
 
 //IllegalArgumentException: DefaultSerializer requires a Serializable payload
 // but received an object of type [com.syc.redis.domain.User]
 
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;
 
 @Column
 private String username;
 
 @Column
 private String password;
 
}

6. 創(chuàng)建User倉(cāng)庫(kù)類(lèi)

package com.yyg.boot.repository;
 
import com.yyg.boot.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface UserRepository extends JpaRepository<User,Long> {
}

7. 創(chuàng)建Service服務(wù)類(lèi)

定義UserService接口

package com.yyg.boot.service;
 
import com.yyg.boot.domain.User;
 
public interface UserService {
 
 User findById(Long id);
 
 User save(User user);
 
 void deleteById(Long id);
 
}

實(shí)現(xiàn)UserServiceImpl類(lèi)

package com.yyg.boot.service.impl;
 
import com.yyg.boot.domain.User;
import com.yyg.boot.repository.UserRepository;
import com.yyg.boot.service.UserService;
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;
 
@Service
public class UserServiceImpl implements UserService {
 
 @Autowired
 private UserRepository userRepository;
 
 //普通的緩存+數(shù)據(jù)庫(kù)查詢(xún)代碼實(shí)現(xiàn)邏輯:
 //User user=RedisUtil.get(key);
 // if(user==null){
 // user=userDao.findById(id);
 // //redis的key="product_item_"+id
 // RedisUtil.set(key,user);
 // }
 // return user;
 
 /**
 * 注解@Cacheable:查詢(xún)的時(shí)候才使用該注解!
 * 注意:在Cacheable注解中支持EL表達(dá)式
 * redis緩存的key=user_1/2/3....
 * redis的緩存雪崩,緩存穿透,緩存預(yù)熱,緩存更新...
 * condition = "#result ne null",條件表達(dá)式,當(dāng)滿(mǎn)足某個(gè)條件的時(shí)候才進(jìn)行緩存
 * unless = "#result eq null":當(dāng)user對(duì)象為空的時(shí)候,不進(jìn)行緩存
 */
 @Cacheable(value = "user", key = "#id", unless = "#result eq null")
 @Override
 public User findById(Long id) {
 
 return userRepository.findById(id).orElse(null);
 }
 
 /**
 * 注解@CachePut:一般用在添加和修改方法中
 * 既往數(shù)據(jù)庫(kù)中添加一個(gè)新的對(duì)象,于此同時(shí)也往redis緩存中添加一個(gè)對(duì)應(yīng)的緩存.
 * 這樣可以達(dá)到緩存預(yù)熱的目的.
 */
 @CachePut(value = "user", key = "#result.id", unless = "#result eq null")
 @Override
 public User save(User user) {
 return userRepository.save(user);
 }
 
 /**
 * CacheEvict:一般用在刪除方法中
 */
 @CacheEvict(value = "user", key = "#id")
 @Override
 public void deleteById(Long id) {
 userRepository.deleteById(id);
 }
 
}

8. 創(chuàng)建Controller接口方法

package com.yyg.boot.web;
 
import com.yyg.boot.domain.User;
import com.yyg.boot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
 
 @Autowired
 private UserService userService;
 
 @PostMapping
 public User saveUser(@RequestBody User user) {
 return userService.save(user);
 }
 
 @GetMapping("/{id}")
 public ResponseEntity<User> getUserById(@PathVariable("id") Long id) {
 User user = userService.findById(id);
 log.warn("user="+user.hashCode());
 HttpStatus status = user == null ? HttpStatus.NOT_FOUND : HttpStatus.OK;
 return new ResponseEntity<>(user, status);
 }
 
 @DeleteMapping("/{id}")
 public String removeUser(@PathVariable("id") Long id) {
 userService.deleteById(id);
 return "ok";
 }
 
}

9. 創(chuàng)建入口類(lèi)

package com.yyg.boot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class CacheApplication {
 
 public static void main(String[] args) {
 SpringApplication.run(CacheApplication.class, args);
 }
 
}

10. 完整項(xiàng)目結(jié)構(gòu)

11. 啟動(dòng)項(xiàng)目進(jìn)行測(cè)試

我們首先調(diào)用添加接口,往數(shù)據(jù)庫(kù)中添加一條數(shù)據(jù)。

可以看到數(shù)據(jù)庫(kù)中,已經(jīng)成功的添加了一條數(shù)據(jù)。

然后測(cè)試一下查詢(xún)接口方法。

此時(shí)控制臺(tái)打印的User對(duì)象的hashCode如下:

我們?cè)俣啻螆?zhí)行查詢(xún)接口,發(fā)現(xiàn)User對(duì)象的hashCode值不變,說(shuō)明數(shù)據(jù)都是來(lái)自于緩存,而不是每次都重新查詢(xún)。

至此,我們就實(shí)現(xiàn)了Spring Boot中默認(rèn)的緩存方案。

總結(jié)

到此這篇關(guān)于SpringBoot中默認(rèn)緩存實(shí)現(xiàn)方案的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot默認(rèn)緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • RocketMQ NameServer 核心源碼解析

    RocketMQ NameServer 核心源碼解析

    這篇文章主要為大家介紹了RocketMQ NameServer 核心源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 阿里云主機(jī)上安裝jdk 某庫(kù)出現(xiàn)問(wèn)題的解決方法

    阿里云主機(jī)上安裝jdk 某庫(kù)出現(xiàn)問(wèn)題的解決方法

    今天安裝jdk到阿里云服務(wù)上,首先看下阿里云是32位還是64位的,如果是32位下載32位的包,如果是64位的下載64位的包,下面與大家分享下安裝過(guò)程中遇到問(wèn)題的解決方法
    2013-06-06
  • Javaweb獲取表單數(shù)據(jù)的多種方式

    Javaweb獲取表單數(shù)據(jù)的多種方式

    這篇文章主要為大家詳細(xì)介紹了Javaweb獲取表單數(shù)據(jù)的多種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • java使用nio2拷貝文件的示例

    java使用nio2拷貝文件的示例

    這篇文章主要介紹了java使用nio2拷貝文件的示例,需要的朋友可以參考下
    2014-04-04
  • 詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法

    詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法

    在本篇文章中我們給大家詳細(xì)分享了springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法,有需要的朋友們可以學(xué)習(xí)參考下。
    2018-10-10
  • Java中Stringbuilder和正則表達(dá)式示例詳解

    Java中Stringbuilder和正則表達(dá)式示例詳解

    Java語(yǔ)言為字符串連接運(yùn)算符(+)提供特殊支持,并為其他對(duì)象轉(zhuǎn)換為字符串,字符串連接是通過(guò)StringBuilder(或StringBuffer)類(lèi)及其append方法實(shí)現(xiàn)的,這篇文章主要給大家介紹了關(guān)于Java中Stringbuilder和正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下
    2024-02-02
  • 詳細(xì)解釋什么是 Spring Bean(示例詳解)

    詳細(xì)解釋什么是 Spring Bean(示例詳解)

    Spring Bean 是由 Spring IoC 容器管理的對(duì)象實(shí)例,也是 Spring 框架的基本組件之一,本文通過(guò)示例代碼介紹Spring Bean 的作用域(Bean Scope)的相關(guān)使用方法,感興趣的朋友一起看看吧
    2023-09-09
  • Jdbctemplate多數(shù)據(jù)源配置方法詳解

    Jdbctemplate多數(shù)據(jù)源配置方法詳解

    這篇文章主要介紹了Jdbctemplate多數(shù)據(jù)源配置方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Spring中@RequestMapping、@PostMapping、@GetMapping的實(shí)現(xiàn)

    Spring中@RequestMapping、@PostMapping、@GetMapping的實(shí)現(xiàn)

    RequestMapping、@PostMapping和@GetMapping是三個(gè)非常常用的注解,本文就來(lái)介紹一下這三種注解的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • SpringCloud Gateway實(shí)現(xiàn)限流功能詳解

    SpringCloud Gateway實(shí)現(xiàn)限流功能詳解

    SpringCloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目,它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的統(tǒng)一的 API 路由管理方式。這篇文章主要介紹了SpringCloud Gateway實(shí)現(xiàn)限流,需要的朋友可以參考下
    2022-11-11

最新評(píng)論