package com.fs.store.cache.impl;

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

    private static final Cache<String, FsStoreOrder> ORDER_CODE_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(1, TimeUnit.MINUTES)
            .build();

    private final FsStoreOrderMapper fsStoreOrderMapper;
    @Override
    public FsStoreOrder selectFsStoreOrderById(Long id) {
        return CACHE.get(id,e->fsStoreOrderMapper.selectFsStoreOrderById(id));
    }

    @Override
    public FsStoreOrder selectFsStoreOrderByOrderCode(String orderCode) {
        return ORDER_CODE_CACHE.get(orderCode,e->fsStoreOrderMapper.selectFsStoreOrderByOrderCode(orderCode));
    }
}
