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

springboot 緩存@EnableCaching實例

 更新時間:2021年11月05日 09:47:40   作者:micro_hz  
這篇文章主要介紹了springboot 緩存@EnableCaching實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot 緩存@EnableCaching

很多時候系統(tǒng)的瓶頸都在一些比較復雜的IO操作,例如讀取數(shù)據庫,如果一些比較穩(wěn)定的數(shù)據,一般的解決方案就是用緩存。spring boot提供了比較簡單的緩存方案。只要使用 @EnableCaching即可完成簡單的緩存功能。

緩存的實現(xiàn)有多種實現(xiàn),ConcurentHashMapCache , GuavaCache, EnCacheCache等多種實現(xiàn),spring boot 有默認的實現(xiàn)。本文不深入源碼解讀,首先用起來。

此處我們模擬要緩存的User

class User {
 private Long id;
 private String name;
// setter getter
}

然后我們業(yè)務對象:

import javax.annotation.PostConstruct;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Component;
/**
 * @author micro
 * @date 2017年8月2日
 * @description :
 */
@Component
@EnableCaching
public class UserDao {
 private Map<Long, User> userMap;
 @PostConstruct
 public void init() {
  //模擬數(shù)據庫
  userMap = new HashMap<Long, User>();
  userMap.put(1L, new User(1L,"micro1"));
  userMap.put(2L, new User(2L, "micro2"));
 }
 
 @Cacheable("user")  // 注解key屬性可以執(zhí)行緩存對象user(可以理解為一個map)的key 
 public User getUser(Long userId) {
  System.out.println("查詢數(shù)據庫:userId ->" + userId);
  return userMap.get(userId);
 }
 
 @Cacheable(value = "nameCache", key = "#name")
 public User getUserByName(Long userId, String name) {
  System.out.println("查詢數(shù)據庫:userId ->" + userId);
  return userMap.get(userId);
 }
 
 @Cacheable("nameCache")
 public User getUserByName(String name) {
  System.out.println("查詢數(shù)據庫:userName : " + name);
  for (Long k : userMap.keySet()) {
   if (userMap.get(k).equals(name)) {
    return userMap.get(k);
   }
  }
  return null;
 }
 
 @CachePut("user") // 與Cacheable區(qū)別就是Cacheable先看緩存如果有,直接緩存換回,CachePut則是每次都會調用并且把返回值放到緩存
 public User getUser2(Long userId) {
  System.out.println("查詢數(shù)據庫:userId : " + userId);
  return userMap.get(userId);
 }
 
 @CacheEvict("user")
 public void removeFromCache(Long userId) {
  return ;
 }
}

然后我們編寫啟動類:

@SpringBootApplication
public class CacheTest implements CommandLineRunner {
 @Autowired
 private UserDao userDao; 
 public static void main(String[] args) {
  new SpringApplication(CacheTest.class).run(args);
 } 
 @Override
 public void run(String... args) throws Exception {
  System.out.println("第一次查詢");
  System.out.println(userDao.getUser(1L));
  System.out.println("第二次查詢");
  System.out.println(userDao.getUser(1L));
  userDao.removeFromCache(1L);// 移除緩存
  System.out.println("第三次查詢");
  userDao.getUser(1L);// 沒有緩存了  
  System.out.println("--------");
  // 測試不同的key緩存
  userDao.getUserByName("micro1");
  userDao.getUserByName(1L, "micro1");// 指定了參數(shù)name 為key 此次讀取緩存
 }
}

打印結果:

第一次查詢
查詢數(shù)據庫:userId ->1
User@65da01f4
第二次查詢
User@65da01f4
第三次查詢
查詢數(shù)據庫:userId ->1
--------
查詢數(shù)據庫:userName : micro1

Spring @EnableCaching的工作原理

1、開發(fā)人員使用注解@EnableCaching

2、注解@EnableCaching導入CachingConfigurationSelector

3、CachingConfigurationSelector根據注解@EnableCaching 屬性AdviceMode mode決定引入哪些配置類

  • PROXY : AutoProxyRegistrar,ProxyCachingConfiguration;
  • ASPECTJ : AspectJCachingConfiguration;

本文以mode=PROXY為例;

4、CachingConfigurationSelector導入AutoProxyRegistrar會確保容器中存在一個自動代理創(chuàng)建器(APC);

  • 用于確保目標bean需要被代理時有可用的代理創(chuàng)建器

5、ProxyCachingConfiguration向容器定義如下基礎設施bean

  • 名稱為org.springframework.cache.config.internalCacheAdvisor類型為BeanFactoryCacheOperationSourceAdvisor的bean
  • 名稱為cacheOperationSource類型為CacheOperationSource的bean

用于獲取方法調用時最終應用的Spring Cache注解的元數(shù)據

  • 名稱為cacheInterceptor類型為CacheInterceptor的bean

一個MethodInterceptor,包裹在目標bean外面用于操作Cache的AOP Advice。

6、AutoProxyRegistrar在容器啟動階段對每個bean創(chuàng)建進行處理,如果該bean中有方法應用了Spring Cache注解,為其創(chuàng)建相應的代理對象,包裹上面定義的BeanFactoryCacheOperationSourceAdvisor bean;

7、使用了Spring Cache注解的bean方法被調用,其實調用首先發(fā)生在代理對象上,先到達cacheInterceptor,然后才是目標bean方法的調用;

  • cacheInterceptor既處理調用前緩存操作,也處理調用返回時緩存操作

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

阿拉善右旗| 景泰县| 三穗县| 荔浦县| 隆化县| 瑞金市| 祁连县| 万宁市| 望都县| 平潭县| 剑川县| 闵行区| 阳谷县| 改则县| 交城县| 富锦市| 舒城县| 固始县| 伊川县| 聊城市| 泸水县| 海安县| 韶山市| 阳泉市| 巴马| 合作市| 丹凤县| 西畴县| 晋州市| 含山县| 太湖县| 绥化市| 安庆市| 郧西县| 永寿县| 天门市| 砀山县| 防城港市| 逊克县| 阳曲县| 高尔夫|