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

SpringBoot中默認緩存實現方案的示例代碼

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

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

一. Spring Boot實現默認緩存

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

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

2. 添加依賴包

<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進行緩存,一共有10種緩存方案
 jpa:
 database: mysql
 show-sql: true #開發(fā)階段,打印要執(zhí)行的sql語句.
 hibernate:
 ddl-auto: update

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

主要是在該類上添加@EnableCaching注解,開啟緩存功能。

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實體類

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倉庫類

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服務類

定義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);
 
}

實現UserServiceImpl類

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;
 
 //普通的緩存+數據庫查詢代碼實現邏輯:
 //User user=RedisUtil.get(key);
 // if(user==null){
 // user=userDao.findById(id);
 // //redis的key="product_item_"+id
 // RedisUtil.set(key,user);
 // }
 // return user;
 
 /**
 * 注解@Cacheable:查詢的時候才使用該注解!
 * 注意:在Cacheable注解中支持EL表達式
 * redis緩存的key=user_1/2/3....
 * redis的緩存雪崩,緩存穿透,緩存預熱,緩存更新...
 * condition = "#result ne null",條件表達式,當滿足某個條件的時候才進行緩存
 * unless = "#result eq null":當user對象為空的時候,不進行緩存
 */
 @Cacheable(value = "user", key = "#id", unless = "#result eq null")
 @Override
 public User findById(Long id) {
 
 return userRepository.findById(id).orElse(null);
 }
 
 /**
 * 注解@CachePut:一般用在添加和修改方法中
 * 既往數據庫中添加一個新的對象,于此同時也往redis緩存中添加一個對應的緩存.
 * 這樣可以達到緩存預熱的目的.
 */
 @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)建入口類

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. 完整項目結構

11. 啟動項目進行測試

我們首先調用添加接口,往數據庫中添加一條數據。

可以看到數據庫中,已經成功的添加了一條數據。

然后測試一下查詢接口方法。

此時控制臺打印的User對象的hashCode如下:

我們再多次執(zhí)行查詢接口,發(fā)現User對象的hashCode值不變,說明數據都是來自于緩存,而不是每次都重新查詢。

至此,我們就實現了Spring Boot中默認的緩存方案。

總結

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

相關文章

  • java線性表的存儲結構及其代碼實現

    java線性表的存儲結構及其代碼實現

    這篇文章主要為大家詳細介紹了Java數據結構學習筆記第一篇,線性表的存儲結構及其代碼實現,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Java?9?中的模塊Module系統(tǒng)

    Java?9?中的模塊Module系統(tǒng)

    Java?9?引入的模塊是在Java包(package)的基礎上又引入的一個新的抽象層,基于package這一點很重要,這里需要強調一下,接下來通過本文給大家介紹Java?9?中的模塊Module系統(tǒng),感興趣的朋友一起看看吧
    2022-03-03
  • Java LinkedList集合功能實例解析

    Java LinkedList集合功能實例解析

    這篇文章主要介紹了Java LinkedList集合功能實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • 一文教你如何更改IDEA已有項目的路徑/名稱

    一文教你如何更改IDEA已有項目的路徑/名稱

    由于IDEA項目路徑中有中文、空格等特殊符號,影響正常使用,想要修改路徑名稱,怎么正確修改IDEA項目名稱,使其正常運行呢?所以本文小編講給大家詳細的介紹了更改IDEA已有項目的路徑/名稱解決方案,需要的朋友可以參考下
    2023-11-11
  • Java 中Object的wait() notify() notifyAll()方法使用

    Java 中Object的wait() notify() notifyAll()方法使用

    這篇文章主要介紹了Java 中Object的wait() notify() notifyAll()方法使用的相關資料,需要的朋友可以參考下
    2017-05-05
  • Java實現驗證碼的產生和驗證

    Java實現驗證碼的產生和驗證

    這篇文章主要為大家詳細介紹了Java實現驗證碼的產生和驗證,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2012-01-01
  • Springboot整合第三方登錄功能的實現示例

    Springboot整合第三方登錄功能的實現示例

    本文主要介紹了Springboot整合第三方登錄功能的實現示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java多線程死鎖示例

    Java多線程死鎖示例

    這篇文章主要介紹了Java多線程死鎖,結合實例形式分析了Java多線程出現死鎖的相關原因與操作注意事項,需要的朋友可以參考下
    2018-08-08
  • ant打包jar文件腳本分享

    ant打包jar文件腳本分享

    本文介紹的ant腳本是用來打包jar文件,做完JAVA應用一定會用到這個,需要的朋友可以參考下
    2014-03-03
  • springbooot使用google驗證碼的功能實現

    springbooot使用google驗證碼的功能實現

    這篇文章主要介紹了springbooot使用google驗證碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05

最新評論

普兰县| 五华县| 囊谦县| 广水市| 塔河县| 土默特右旗| 马尔康县| 江西省| 延吉市| 万年县| 确山县| 广安市| 伊川县| 盐源县| 集贤县| 巨鹿县| 从江县| 珲春市| 高邑县| 科尔| 合川市| 鹤岗市| 封开县| 洪泽县| 双城市| 武宣县| 肃北| 闽清县| 泾川县| 上犹县| 巴青县| 马山县| 措勤县| 巴里| 抚顺县| 洪洞县| 盐源县| 贵定县| 高清| 通州区| 武邑县|