本地缓存caffeine使用方式

使用方式:

一、导入包

    com.github.ben-manes.caffeine

    caffeine

    2.8.6

二、增加配置类

@Configuration

public class LocalCacheConfig {

    @Value("${mkt.coupon.batch.cache.expireSeconds:60}")

    private long expireSeconds;

    @Value("${mkt.coupon.batch.cache.maximumSize:5000}")

    private long maximumSize;

    /**

     * 手动加载

     * 券批次缓存

     *

     * @return

     */

    @Bean("batchCache")

    public Cache batchCache() {

        return Caffeine.newBuilder()

                .expireAfterWrite(expireSeconds, TimeUnit.SECONDS)

                .maximumSize(maximumSize)

                .build();

    }

}

三、使用

@Resource(name = "batchCache")

private Cache batchCache;

 

@Override

public Optional getOnline(BatchId batchId) {

    AbstractBatch batch = batchCache.get(batchId.id(), k -> loadOnlineBatch(batchId));

    return Optional.ofNullable(batch);

}

你可能感兴趣的:(redis)