Cache in Spring

Use cache in spring, need following steps:

Enable Cache

@Configuration
@EnableCaching
public class CachingConfig {
  @Bean
  public CacheManager cacheManager() {
    return new ConcurrentMapCacheManager("categoryOrders");
  }
}

The "categoryOrders" is a cache name. The ConcurrentMapCacheManager can accept multiple caches.

Bind Annotation on Method

There are several annotations:

  • @Cacheable
    The simplest way to enable caching behavior for a method is to demarcate it with @Cacheable and parameterize it with the name of the cache where the results would be stored:
  @Cacheable("categoryOrders")
   List<Order> findAllOrders() {
    return findAll();
  }

The first time the application will get all orders from database and cache it in "categoryOrders"

  • @CacheEvict
    When the data of category has been changed, we want to clean the cache. So we can bind this annotation on the method to clean the cache.
  @CacheEvict(value = "categoryOrders", allEntries = true)
  public void cleanCategoryOrderCache() {
  }

allEntries = true means clean all data.

  • @CachePut
    @CacheEvict annotation is clean all data. Sometimes, you only need to update a fraction of data. You can use @CachePut annotation.
@CachePut(value="categoryOrders")
public Order getOrder(String orderId) {...}

The difference between @CachePut and @Cacheable is that @CachePut
will invoke the method every time, but @Cacheable will invoke this method at first time.
We can also add condition in those annotations, such as:

@CachePut(value="categoryOrders", condition="#order.type==1")
public Order getOrder(String orderId) {...}
@CachePut(value="categoryOrders", unless="#order.type==3")
public Order getOrder(String orderId) {...}

There are many parameters you can define in each annotation.
The thing you need pay attention is that the theory of spring cache is AOP. So when you call the cache method in the same class, the cache will not be work. such as:

public class CategoryOrderGenerator {
  private CategoryOrderRepository categoryOrderRepository;

  @Autowired
  public CategoryOrderGenerator(CategoryOrderRepository categoryOrderRepository) {
    this.categoryOrderRepository = categoryOrderRepository;
  }

  public List<CategoryOrder> getCategoryOrder(List<String> parameters)
    return categoryOrderRepository.findAllCategoryOrders().stream()
        .filter(....)
        .map(....)
        .collect(Collectors.toList());
  }

  @Cacheable("categoryOrders")
  public List<CategoryOrder> getAll() {
    return categoryOrderRepository.findAllCategoryOrders();
  }
}

The "getCategoryOrder" method and cached "getAll()" are in same class. The "getCategoryOrder" call the "getAll()" method. So the cache will not work, because this two method are in same class. You can add the @Cacheable("categoryOrders") to findAllCategoryOrders() method in repository.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,869評(píng)論 0 10
  • 2017的年度大戲又拉開了序幕——高考。網(wǎng)上那些視頻、標(biāo)語(yǔ)等等之類的東西在激勵(lì)考生。即使是已經(jīng)進(jìn)入大學(xué)一年的我,看...
    yuyiyu閱讀 316評(píng)論 0 0
  • 你轉(zhuǎn)身離開 去了遙遠(yuǎn)的地方 晃如兩個(gè)世界 最單純的愛(ài)意 全部揉進(jìn) 沒(méi)有地址的信 投進(jìn)綠郵筒里 期待你的回信 晃如兩個(gè)世紀(jì)
    冰未寒閱讀 453評(píng)論 3 13
  • 【G206班M13組(戰(zhàn)神組)第3次小組會(huì)】流程及作業(yè)安排 時(shí)間:6月30號(hào)(周六)早6:00-7:00 地點(diǎn):Y...
    May9799閱讀 194評(píng)論 0 0
  • 一直以來(lái),我是大家眼中的瘦子。 后來(lái),就變成大瘦子。 再后來(lái),就像胖子一樣,到哪都好像做錯(cuò)事一樣說(shuō)你,怎么這么胖(...
    月滿花田閱讀 334評(píng)論 0 1

友情鏈接更多精彩內(nèi)容