package com.fs.store.cache.impl;

import com.fs.store.cache.IFsStoreProductCacheService;
import com.fs.store.cache.IFsWarehouseCacheService;
import com.fs.store.domain.FsStoreProduct;
import com.fs.store.domain.FsWarehouses;
import com.fs.store.service.IFsWarehousesService;
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 FsWarehouseCacheServiceImpl implements IFsWarehouseCacheService {
    private final IFsWarehousesService fsWarehousesService;
    /**
     * 使用Caffeine本地缓存
     * maximumSize: 最大缓存条数
     * expireAfterWrite: 写入后多久过期（分钟）
     */
    private static final Cache<Long, FsWarehouses> CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(1, TimeUnit.MINUTES)
            .build();


    @Override
    public FsWarehouses selectFsWarehousesById(Long id) {
        return CACHE.get(id, e -> fsWarehousesService.selectFsWarehousesById(id));
    }
}
