package com.fs.store.cache.impl;

import com.fs.store.cache.IFsUserCacheService;
import com.fs.store.domain.FsUser;
import com.fs.store.service.IFsUserService;
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;

@RequiredArgsConstructor
@Service
@Slf4j
public class IFsUserCacheServiceImpl implements IFsUserCacheService {
    private final IFsUserService fsUserService;
    private static final Cache<Long, FsUser> USER_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();

    @Override
    public FsUser selectFsUserById(Long userId) {
        return USER_CACHE.get(userId,e->fsUserService.selectFsUserById(userId));
    }
}
