package com.fs.company.cache.impl;

import com.fs.company.cache.ICompanyCacheService;
import com.fs.company.cache.ICompanyUserCacheService;
import com.fs.company.domain.Company;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
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.Component;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@RequiredArgsConstructor
public class CompanyUserCacheServiceImpl implements ICompanyUserCacheService {

    private final ICompanyUserService companyUserService;

    private static final Cache<Long, CompanyUser> USER_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();
    @Override
    public CompanyUser selectCompanyUserById(Long userId) {
        return USER_CACHE.get(userId,e-> companyUserService.selectCompanyUserByUserId(userId));
    }
}
