package com.fs.company.cache.impl;

import com.fs.company.cache.ICompanyDeptCacheService;
import com.fs.company.service.ICompanyDeptService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CompanyDeptCacheServiceImpl implements ICompanyDeptCacheService {
    private static final Cache<Long, String> COMPANY_DEPT_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(10, TimeUnit.MINUTES)
            .build();
    @Autowired
    private ICompanyDeptService companyDeptService;
    @Override
    public String getDeptNameById(Long deptId) {
        if(deptId == null) {
            return "-";
        }
        return COMPANY_DEPT_CACHE.get(deptId,e-> companyDeptService.selectDeptNameById(deptId));
    }
}
