日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

ehcache怎么删除缓存_解释SpringBoot之Ehcache 2.x缓存

發布時間:2025/4/5 javascript 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ehcache怎么删除缓存_解释SpringBoot之Ehcache 2.x缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

介紹

這里介紹Ehcache 2.X 緩存

添加基本的web項目

添加Ehcache 依賴

xml version="1.0" encoding="UTF-8"?>

xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.3.1.RELEASE

com.example

demo

0.0.1-SNAPSHOT

demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-cache

2.3.1.RELEASE

net.sf.ehcache

ehcache

2.10.6

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

添加配置文件

添加ehcache 配置緩存文件

path="java.io.tmpdir/shiro-spring-sample"/>

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

overflowToDisk="false"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="120"

/>

name="book_cache"

maxElementsInMemory="10000"

eternal="true"

overflowToDisk="true"

diskPersistent="true"

diskExpiryThreadIntervalSeconds="600"/>

注解上開啟緩存

在啟動類上添加相關的注解

package com.example.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication

@EnableCaching

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

使用緩存

創建實體類和BookService

創建實體類

package com.example.demo;

import java.io.Serializable;

public class Book implements Serializable {

private Integer id;

private String name;

private String author;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

}

添加相關的service

package com.example.demo;

import org.springframework.cache.annotation.CacheConfig;

import org.springframework.cache.annotation.CacheEvict;

import org.springframework.cache.annotation.CachePut;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Repository;

@Repository

// 使用緩存的名稱

@CacheConfig(cacheNames = "book_cache")

public class BookDao {

// 對該方法進行緩存

@Cacheable

public Book getBookById(Integer id){

System.out.println("調用Service方法");

Book book = new Book();

book.setAuthor("ming");

book.setId(id);

book.setName("ming");

return book;

}

@CachePut(key = "#book.id")

// 用在更新方法上,數據庫數據更新,緩存數據也要更新,方法的返回值自動更新在已經保存的key上

public Book updateBookById(Book book){

System.out.println("更新ById");

book.setName("ming2");

return book;

}

@CacheEvict(key = "#id")

// 用在刪除方法上,數據刪除以后,其方法也會刪除

public void deleteBookById(Integer id){

System.out.println("id刪除");

}

}

創建測試類

創建測試類,對Service方法進行測試

package com.example.demo;

import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(SpringExtension.class) //導入spring測試框架[2]

@SpringBootTest

//提供spring依賴注入

class BookDaoTest {

@Autowired

private BookDao bookDao;

@Test

public void test(){

bookDao.getBookById(1);

bookDao.getBookById(1);

bookDao.deleteBookById(1);

Book book = bookDao.getBookById(1);

System.out.println(book);

book.setName("mingming");

book.setAuthor("mingming");

book.setId(1);

bookDao.updateBookById(book);

Book book1 = bookDao.getBookById(1);

System.out.println(book1);

}

}

查看結果

小明菜市場

總結

以上是生活随笔為你收集整理的ehcache怎么删除缓存_解释SpringBoot之Ehcache 2.x缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。