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

SpringBoot集成cache緩存的實現(xiàn)

 更新時間:2021年06月11日 08:28:33   作者:huanzi-qch  
日常開發(fā)中,緩存是解決數(shù)據(jù)庫壓力的一種方案,本文記錄springboot中使用cache緩存。需要的朋友們下面隨著小編來一起學習學習吧

前言

  日常開發(fā)中,緩存是解決數(shù)據(jù)庫壓力的一種方案,通常用于頻繁查詢的數(shù)據(jù),例如新聞中的熱點新聞,本文記錄springboot中使用cache緩存。

  官方文檔介紹:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-generic

工程結(jié)構(gòu)

代碼編寫

  pom引入依賴,引入cache緩存,數(shù)據(jù)庫使用mysql,ORM框架用jpa

<!--添加springdata-cache依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <!-- 引入ehcache支持 -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

        <!--添加springdata-jpa依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--添加MySQL驅(qū)動依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

  配置文件

server.port=10010
spring.application.name=springboot-cache

spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:/ehcache.xml

  ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">

    <!-- 磁盤緩存位置 -->
    <diskStore path="java.io.tmpdir"/>

    <!-- maxEntriesLocalHeap:堆內(nèi)存中最大緩存對象數(shù),0沒有限制 -->
    <!-- maxElementsInMemory: 在內(nèi)存中緩存的element的最大數(shù)目。-->
    <!-- eternal:elements是否永久有效,如果為true,timeouts將被忽略,element將永不過期 -->
    <!-- timeToIdleSeconds:發(fā)呆秒數(shù),發(fā)呆期間未訪問緩存立即過期,當eternal為false時,這個屬性才有效,0為不限制 -->
    <!-- timeToLiveSeconds:總存活秒數(shù),當eternal為false時,這個屬性才有效,0為不限制 -->
    <!-- overflowToDisk: 如果內(nèi)存中數(shù)據(jù)超過內(nèi)存限制,是否要緩存到磁盤上 -->
    <!-- statistics:是否收集統(tǒng)計信息。如果需要監(jiān)控緩存使用情況,應該打開這個選項。默認為關閉(統(tǒng)計會影響性能)。設置statistics="true"開啟統(tǒng)計 -->

    <!--
        默認緩存
        無過期時間,但 600 秒內(nèi)無人訪問緩存立即過期
    -->
    <defaultCache
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="600"
            timeToLiveSeconds="0"
            overflowToDisk="false">
    </defaultCache>

    <!--
        xx業(yè)務緩存
        在有效的 120 秒內(nèi),如果連續(xù) 60 秒未訪問緩存,則緩存失效。
        就算有訪問,也只會存活 120 秒。
    -->
    <cache name="myCache"
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="0"
           overflowToDisk="false">
    </cache>
</ehcache>

  先寫一個套tb_user表的CRUD代碼

@RestController
@RequestMapping("/tbUser/")
public class TbUserController {
    @Autowired
    private TbUserService tbUserService;

    //方便測試暫時改成GetMapping
    @GetMapping("list")
//    @PostMapping("list")
    public List<TbUser> list(TbUser entityVo) {
        return tbUserService.list(entityVo);
    }

    @GetMapping("get/{id}")
    public TbUser get(@PathVariable("id")Integer id) {
        return tbUserService.get(id);
    }

    //方便測試暫時改成GetMapping
    @GetMapping("save")
//    @PostMapping("save")
    public TbUser save(TbUser entityVo) {
        return tbUserService.save(entityVo);
    }

    @GetMapping("delete/{id}")
    public Integer delete( @PathVariable("id") Integer id) {
        return tbUserService.delete(id);
    }
}

  opjo實體類要實現(xiàn)序列化

@Entity
@Table(name = "tb_user")
@Data
public class TbUser implements Serializable {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;//表id

    private String username;//用戶名

    private String password;//密碼

    private Date created;//創(chuàng)建時間

    private Integer descriptionId;//關聯(lián)詳情id
}

  serviceImpl中,使用注解來開啟緩存

@Service
@Transactional
@CacheConfig(cacheNames = {"myCache"})
public class TbUserServiceImpl implements TbUserService{

    @PersistenceContext
    private EntityManager em;

    @Autowired
    private TbUserRepository tbUserRepository;

    //@Cacheable緩存數(shù)據(jù):key為userList,value為返回值List<TbUser>
    @Cacheable(key = "'userList'")
    @Override
    public List<TbUser> list(TbUser entityVo) {
        System.out.println("獲取list用戶列表緩存數(shù)據(jù),"+new Date());
        return tbUserRepository.findAll(Example.of(entityVo));
    }

    //@Cacheable緩存數(shù)據(jù):key為參數(shù)id,value為返回值TbUser
    @Cacheable(key = "#id")
    @Override
    public TbUser get(Integer id) {
        System.out.println("獲取數(shù)據(jù)緩存,key:"+id);
        Optional<TbUser> optionalE = tbUserRepository.findById(id);
        if (!optionalE.isPresent()) {
            throw new RuntimeException("ID不存在!");
        }
        return optionalE.get();
    }

    //@CachePut緩存新增的或更新的數(shù)據(jù)到緩存,其中緩存的名稱為people,數(shù)據(jù)的key是person的id
    @CachePut(key = "#entityVo.id")
    // @CacheEvict從緩存中刪除key為參數(shù)userList的數(shù)據(jù)
    @CacheEvict(key = "'userList'")
    @Override
    public TbUser save(TbUser entityVo) {
        System.out.println("新增/更新緩存,key:"+entityVo.getId());
        //entityVo傳啥存啥,會全部更新
        return tbUserRepository.save(entityVo);
    }

    //清空所有緩存
    @CacheEvict(allEntries=true)
    @Override
    public Integer delete(Integer id) {
        System.out.println("清空所有緩存");
        tbUserRepository.deleteById(id);
        return id;
    }
}

  效果演示

  http://localhost:10010/tbUser/save?id=2&username=李四

  調(diào)用save方法,key為2,value為當前tbUser對象的數(shù)據(jù)被緩存下來

  http://localhost:10010/tbUser/get/2

  當我們調(diào)用get方法時,直接獲取緩存數(shù)據(jù),控制臺啥也不打印,連serviceImpl的get方法都不進去(可以打斷點調(diào)試)

  http://localhost:10010/tbUser/save?id=2&username=王五

  當我們再次調(diào)用save方法更新username時,緩存數(shù)據(jù)也被更新

  http://localhost:10010/tbUser/get/2

  再次調(diào)用get接口,直接返回緩存數(shù)據(jù),后臺也是方法都不進去,啥也不打印

  http://localhost:10010/tbUser/delete/2

  調(diào)用delete接口,刪除數(shù)據(jù),同時刪除緩存

  再次調(diào)用get接口,發(fā)現(xiàn)緩存數(shù)據(jù)被清除,查詢數(shù)據(jù)庫

  http://localhost:10010/tbUser/list

  首次調(diào)用list接口,key為userList的,value為用戶集合數(shù)據(jù)被緩存下來,再次調(diào)用直接返回緩存數(shù)據(jù)

  當調(diào)用save接口,數(shù)據(jù)更新,刪除key為userList的緩存,再次調(diào)用list時,重新查庫并設置緩存

  我們配置了緩存發(fā)呆時間,當120秒內(nèi)未使用該緩存,立即過期,一直用就會一直存在

  我們先同時訪問兩個接口list、get,list接口2分鐘后再次訪問,get接口不能超過2分鐘是不是訪問一下,結(jié)果如預期

  PS:原先使用了這個jar包,有報錯

 <dependency>
      <groupId>org.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>3.8.1</version>
    </dependency> 

  后面改成用上面“代碼編寫”里pom中引的jnet.sf.ehcache下面的ar

后記

  緩存除了能緩解數(shù)據(jù)庫壓力,還能做用戶登錄狀態(tài)控制,例如:用戶登錄成功后cookie中保存頒發(fā)的token令牌設置永不過期,緩存存活時間也設置永不過期,發(fā)呆時間設置1天,這樣只有用戶在1天內(nèi)有訪問緩存接口,那他就可以一直保留登錄狀態(tài),直至有其他業(yè)務將token或者緩存清掉。

  springboot使用cache緩存暫時先記錄到這,后續(xù)有空再進行補充。

代碼開源

  代碼已經(jīng)開源、托管到我的GitHub、碼云:

  GitHub:https://github.com/huanzi-qch/springBoot

  碼云:https://gitee.com/huanzi-qch/springBoot

到此這篇關于SpringBoot集成cache緩存的實現(xiàn)的文章就介紹到這了,更多相關SpringBoot cache緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 淺談Java中的Filter過濾器

    淺談Java中的Filter過濾器

    本篇文章主要介紹了淺談Java中的Filter過濾器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • spring aop實現(xiàn)用戶權限管理的示例

    spring aop實現(xiàn)用戶權限管理的示例

    本篇文章主要介紹了spring aop實現(xiàn)用戶權限管理的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot使用JUL實現(xiàn)日志記錄功能

    SpringBoot使用JUL實現(xiàn)日志記錄功能

    在SpringBoot中,我們可以使用多種日志框架進行日志記錄,其中,JUL(Java Util Logging)是Java平臺自帶的日志框架,它提供了簡單的 API 和配置,可以輕松地進行日志記錄,本文將介紹如何在 SpringBoot中使用JUL進行日志記錄,并提供示例代碼
    2023-06-06
  • jdbc鏈接遠程數(shù)據(jù)庫進行修改url操作

    jdbc鏈接遠程數(shù)據(jù)庫進行修改url操作

    這篇文章主要為大家詳細介紹了jdbc鏈接遠程數(shù)據(jù)庫進行修改url操作,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 詳解idea從git上拉取maven項目詳細步驟

    詳解idea從git上拉取maven項目詳細步驟

    這篇文章主要介紹了詳解idea從git上拉取maven項目詳細步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • Mybatis批量插入更新xml方式和注解方式的方法實例

    Mybatis批量插入更新xml方式和注解方式的方法實例

    這篇文章主要給大家介紹了關于Mybatis批量插入更新xml方式和注解方式的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Mybatis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-12-12
  • Java數(shù)組隊列概念與用法實例分析

    Java數(shù)組隊列概念與用法實例分析

    這篇文章主要介紹了Java數(shù)組隊列概念與用法,結(jié)合實例形式分析了Java數(shù)組隊列相關概念、原理、用法及操作注意事項,需要的朋友可以參考下
    2020-03-03
  • Mybatis源碼解析之事務管理

    Mybatis源碼解析之事務管理

    大家好,本篇文章主要講的是Mybatis源碼解析之事務管理,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Java動態(tài)規(guī)劃之編輯距離問題示例代碼

    Java動態(tài)規(guī)劃之編輯距離問題示例代碼

    這篇文章主要介紹了Java動態(tài)規(guī)劃之編輯距離問題示例代碼,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java17和springboot3.0使用shiro報ClassNotFoundException的解決

    Java17和springboot3.0使用shiro報ClassNotFoundException的解決

    本文主要介紹了Java17和springboot3.0使用shiro報ClassNotFoundException的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-04-04

最新評論