package com.fs.company.cache.impl;

import com.fs.company.cache.ICompanyTagCacheService;
import com.fs.company.domain.CompanyTag;
import com.fs.company.service.ICompanyTagService;
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.Map;
import java.util.concurrent.TimeUnit;

@Service
public class CompanyTagCacheServiceImpl implements ICompanyTagCacheService {

    @Autowired
    private ICompanyTagService companyTagService;
    private static final Cache<Long,String> COMPANY_TAG_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(5, TimeUnit.MINUTES)
            .build();

    private static final Cache<Long,Map<Long, CompanyTag>> COMPANY_USER_TAG_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(5, TimeUnit.MINUTES)
            .build();
    @Override
    public String findUserTagByUserId(Long key) {
        return COMPANY_TAG_CACHE.get(key,e-> companyTagService.findUserTagByUserId(key));
    }

    @Override
    public Map<Long, CompanyTag> queryAllTagMap() {
        return COMPANY_USER_TAG_CACHE.get(0L, e-> companyTagService.queryAllTagMap());
    }
}
