Skip to content
Yizzuide edited this page Nov 3, 2020 · 2 revisions

Instructions(模块说明)

Light provides multi-level caching solutions such as L1 used memory cache, L2 used with Spring Data Redis. Caching strategy supported by L1 are:

  • HotDiscard: Low frequency hotspots are discarded first, which called LFU strategy.
  • TimelineDiscard: The earlier the data is discarded first, which called LRU strategy.
  • LazyExpire: Cache by setting the expiration time.

Light提供了多级缓存的解决方案,如:L1(内存缓存)、L2(Redis缓存),其中L1缓存类型支持如下:

  • HotDiscard: 热点策略,学术上叫LFU(适用于大量相同类型记录数据的情况,使用得越频繁越不容易过期)
  • TimelineDiscard: 时间线策略,学术上叫LRU(适用于大量相同类型记录数据的情况,最新使用的数据不会被丢弃)
  • LazyExpire: 懒惰过期丢弃策略(时间过期方案,适用于缓存数据会在一定时间会被更新的情况)

Dependencies(依赖)

<dependency>
  <groupId>com.github.yizzuide</groupId>
  <artifactId>milkomeda-spring-boot-starter</artifactId>
  <version>${milkomeda-last-version}</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Enable with annotation(启用模块)

@EnableLight
@SpringBootApplication
public class MilkomedaDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MilkomedaDemoApplication.class, args);
    }
}

Example of Discard the cache pool(缓存池案例)

1. Config with application.yml(在Spring Boot项目的application.yml里配置)

milkomeda:
  instances:
    # Add a cache instance name
    # 添加实例配置名
    product:
      # Set cache strategy, default is HOT
      # 设置缓存策略,默认为HOT
      strategy: TIMELINE
      # Max count which L1 can cached, over this amount will tigger disard (Not support strategy is LazyExpire)
      # 一级缓存最大个数,存储数量超过将触发丢弃策略删除(缓存策略为LazyExpire时无效)
      l1-max-count: 64
      # how match percent to remove when tigger disard  (Not support strategy is LazyExpire)
      # 当触发丢弃时,一级缓存需要删除的百分比(缓存策略为LazyExpire时无效)
      l1-discard-percent: 0.25

2. Add annotation to the data source fetch method(在数据源获取方法上添加注解)

The attribute method value of @LightCacheable is set cache instance name as config above, and key support Spring EL to set cache key for distinguish between stored objects.

注解@LightCacheable的属性方法value需要设置上面配置的缓存实例名,key支持Spring EL来设置缓存键以区分存储的对象。

 @LightCacheable(value = "product", key = "'product:' + #id")
 public Product getById(String id) {
   Product p = new Product();
   // Query a product from data source
   return p;
 }

Example of Expire Cache(过期缓存案例)

1. Config with application.yml(在Spring Boot项目的application.yml里配置)

milkomeda:
  instances:
    # Add a cache instance name
    # 添加实例配置名
    order:
      # Set cache strategy, default is HOT
      # 设置缓存策略,默认为HOT
      strategy: LazyExpire
      # Cache L1 expire time (only support strategy is LazyExpire)
      # 一级缓存过期时间(仅支持缓存策略为LazyExpire时有效)
      l1-expire: 1d
      # Cache L2 expire time
      # 二级缓存过期时间
      l2-expire: 1d
      # If set is true, the cache L1 will disable (only support strategy is LazyExpire)
      # 如果设置为ture,将禁用一级缓存(仅支持缓存策略为LazyExpire时有效)
      #only-cache-l2: true

2. Add annotation to the data source fetch method(在数据源获取方法上添加注解)

    @LightCacheable(value = "order", key = "'order:' + #id", condition = "#id != null")
    public Order findById(String id) {
        log.info("Query product id:{}", id);
        return new Order(id, "Jack", "1200", new Date());
    }

    @LightCacheEvict(value = "order", key = "'order:' + #id")
    public void  deleteById(String id) {
      log.info("Remove product id:{}", id);
    }

    @LightCachePut(value = "order", key = "'order:' + #id", condition = "#id != null")
    public Order updateById(String id) {
        log.info("Update product id:{}", id);
        return new Order(id, "Rose", "2000", new Date());
    }