成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringBoot集成Ehcache使用教程

開發 架構
Spring 緩存注解是基于Spring AOP切面,必須走代理才能生效。同類調用或者子類調用父類帶有緩存注解的方法時屬于內部調用,沒有走代理,所以注解不會生效。所以在使用@Cacheable時,一定要放在在service的實現類中進行調用。

前言

在平時做項目都要用到緩存,方便臨時存儲一些數據,加快訪問速度。如果項目比較小,搭建redis服務,后期在維護上比較麻煩。今天分享一個SpringBoot集成Ehcache實現緩存的教程,適合中小項目中使用。

準備工作

1、maven中導入依賴

<!--開啟Springboot cache 緩存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache 緩存 -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>

2、啟動類上增加緩存注解

@MapperScan("com.zhangls.ehcache.dao.**")
@SpringBootApplication
@EnableCaching
public class EhcacheApplication {
public static void main(String[] args) {
SpringApplication.run(EhcacheApplication.class, args);
}
}

3、配置Ehcache

在resources下增加ehcache.xml文件,配置如下:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
<service>
<!--開啟service注解-->
<jsr107:defaults enable-statistics="true"/>
</service>
<!-- user 為該緩存名稱 對應@Cacheable的屬性cacheNames-->
<cache alias="UserCache">
<!-- 指定緩存 key 類型,對應@Cacheable的屬性key -->
<key-type>java.lang.String</key-type>
<!-- 配置value類型 -->
<value-type>com.zhangls.ehcache.entity.User</value-type>
<expiry>
<!-- 緩存 ttl,單位為分鐘,現在設置的是1分鐘 -->
<ttl unit="minutes">1</ttl>
</expiry>
<resources>
<!-- 分配資源大小 -->
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap>
</resources>
</cache>
<!--這里可以配置N個 。。。。 不同的cache 根據業務情況配置-->
</config>

4、application.yml中配置

spring:
cache:
jcache:
config: classpath:ehcache.xml

注意事項

1.Ehcache 會在一定的規則下會序列化后存儲到硬盤上,因此緩存對象必須支持序列化。

public class User implements Serializable{}

2.Spring定義了緩存接口Cache和管理緩存控制器 CacheManager,路徑為:

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

使用方法--手動管理方式

@Autowired
private CacheManager cacheManager;
@GetMapping("/addCache")
public String addCache() {
User user = new User();
user.setUsername("九天銀河聊編程");
user.setAge(34);
Cache cache = cacheManager.getCache("UserCache");
cache.put("user", user);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
return sdf.format(now) + ": " + "保存成功";
}

@GetMapping("/getCache")
public String getCache() {
Cache cache = cacheManager.getCache("UserCache");
Cache.ValueWrapper res = cache.get("user");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
if (null != res) {
User user = (User) res.get();//這里獲取 ehcache.xml 中 <cache> value-type 定義的類型,可以直接強轉。
return sdf.format(now) + ": " + "姓名:" + user.getUsername() + ",年齡:" + user.getAge();
}
return sdf.format(now) + ": " + "沒有找到緩存!";
}

運行結果

執行:127.0.0.1:8080/ehcache/addCache。

執行:127.0.0.1:8080/ehcache/getCache。

1分鐘后執行127.0.0.1:8080/ehcache/getCache,緩存失效。

使用方法--@Cacheable 注解方式

service代碼:

@Service
public class ImPersonServiceImpl implements ImPersonService{
@Resource
private PersonMapper personMapper;
@Override
@Cacheable(cacheNames = "PersonCache", key = "#personId")
public ImPerson selectByPrimaryKey(String personId){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
System.out.println(sdf.format(now) + ": 未命中緩存,請求數據庫");
return personMapper.selectByPrimaryKey(personId);
}
}

controller代碼:

    @GetMapping("/getCachePerson")
public ImPerson getCachePerson() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date start = new Date();
System.out.println(sdf.format(start) + ":執行開始------");
ImPerson person = imPersonService.selectByPrimaryKey("1");
Date end = new Date();
System.out.println(sdf.format(end) + ":執行結束------");
return person;
}

執行兩次:127.0.0.1:8080/ehcache/getCachePerson。

控制臺只打印一次SQL信息,說明第二次請求從緩存中獲取。

@Cacheable屬性說明

  • cacheNames/value :用來指定緩存組件的名字。
  • key :緩存數據時使用的 key,可以用它來指定。默認是使用方法參數的值。(這個 key 你可以使用 spEL 表達式來編寫)。
  • keyGenerator :key 的生成器。 key 和 keyGenerator 二選一使用。
  • cacheManager :可以用來指定緩存管理器。從哪個緩存管理器里面獲取緩存。
  • condition :可以用來指定符合條件的情況下才緩存,如下表示id>1的進行緩存。
 @Cacheable(cacheNames = "PersonCache", condition  = "#id > 1")
  • unless :否定緩存。當 unless 指定的條件為 true ,方法的返回值就不會被緩存。當然你也可以獲取到結果進行判斷。(通過 #result 獲取方法結果)
  • sync :是否使用異步模式。

踩坑說明

Spring 緩存注解是基于Spring AOP切面,必須走代理才能生效。同類調用或者子類調用父類帶有緩存注解的方法時屬于內部調用,沒有走代理,所以注解不會生效。所以在使用@Cacheable時,一定要放在在service的實現類中進行調用。

責任編輯:姜華 來源: 今日頭條
相關推薦

2023-02-14 07:47:20

SpringBootEhcache

2022-03-15 08:22:31

Ehcachespring緩存

2021-09-26 05:02:00

緩存Ehcache用法

2014-12-31 09:56:29

Ehcache

2014-12-31 09:45:48

EhCache

2009-09-21 17:23:49

Hibernate使用

2023-10-12 08:00:48

2021-05-26 06:22:34

SpringBootJPA后端開發

2021-06-05 07:34:00

SpringBootMybatis用法

2021-07-11 07:05:28

RedisSpringBoot用法

2020-08-19 17:56:46

緩存Redis集中式

2011-08-23 09:50:29

LuaPlusLua 腳本

2021-04-21 09:04:43

開發SpringBoot框架

2019-04-23 08:42:42

EhcacheMemcacheRedis

2022-04-06 08:29:26

Kafka通信中間件

2021-03-22 08:06:59

SpringBootSentinel項目

2022-04-15 07:21:12

架構開源模板引擎

2010-07-20 13:12:11

Perl數組

2013-05-17 10:48:40

GoogleAndroid Stu

2024-04-29 18:55:16

緩存Spring性能
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品久久久久久一区二区三区 | 国产精品久久国产精品99 | 国产欧美精品一区二区色综合朱莉 | 黄色精品| 一本一道久久a久久精品综合 | 国产一区视频在线 | 成人精品在线观看 | 成人国产毛片 | 日本精品视频在线 | 亚洲欧美日韩网站 | 欧美日韩在线一区二区 | 亚洲vs天堂 | 国产三级| 成人黄色在线观看 | 国产成人99久久亚洲综合精品 | 麻豆久久久久久久 | 国产亚洲精品成人av久久ww | 日韩1区2区 | 99色播 | 亚洲欧美国产毛片在线 | 国产一区2区 | 亚洲精品日韩在线 | 成人精品鲁一区一区二区 | 激情的网站 | 日韩欧美国产精品一区 | 久久久久成人精品免费播放动漫 | 国产美女一区二区 | 99在线播放 | 欧洲一区二区在线 | 精品国产乱码久久久久久蜜柚 | 狠狠撸在线视频 | 祝你幸福电影在线观看 | 欧美jizzhd精品欧美巨大免费 | 一区二区三区免费 | 成人午夜精品一区二区三区 | 欧美日韩电影一区 | av一区二区三区 | 日韩一区二区三区视频 | 另类专区亚洲 | 国产精久久久 | 国产7777 |