package com.fs.store.cache.impl;

import com.fs.store.cache.IFsStoreProductCacheService;
import com.fs.store.domain.FsStoreProduct;
import com.fs.store.service.IFsStoreProductService;
import com.fs.system.cache.impl.SysDictDataCacheServiceImpl;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@RequiredArgsConstructor
public class IFsStoreProductCacheServiceImpl implements IFsStoreProductCacheService {
    private final IFsStoreProductService fsStoreProductService;
    /**
     * 使用Caffeine本地缓存
     * maximumSize: 最大缓存条数
     * expireAfterWrite: 写入后多久过期（分钟）
     */
    private static final Cache<Long, FsStoreProduct> CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(1, TimeUnit.MINUTES)
            .build();

    @Override
    public FsStoreProduct selectFsStoreProductById(Long productId) {
        return CACHE.get(productId, e -> fsStoreProductService.selectFsStoreProductById(productId));
    }
}
