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.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,163评论 0 10
  • 2017的年度大戏又拉开了序幕——高考。网上那些视频、标语等等之类的东西在激励考生。即使是已经进入大学一年的我,看...
    yuyiyu阅读 1,833评论 0 0
  • 你转身离开 去了遥远的地方 晃如两个世界 最单纯的爱意 全部揉进 没有地址的信 投进绿邮筒里 期待你的回信 晃如两个世纪
    冰未寒阅读 3,142评论 3 13
  • 【G206班M13组(战神组)第3次小组会】流程及作业安排 时间:6月30号(周六)早6:00-7:00 地点:Y...
    May9799阅读 1,007评论 0 0
  • 一直以来,我是大家眼中的瘦子。 后来,就变成大瘦子。 再后来,就像胖子一样,到哪都好像做错事一样说你,怎么这么胖(...
    月满花田阅读 2,338评论 0 1