package com.fs.course.cache.impl;

import com.fs.course.cache.FsUserCourseCategoryCacheService;
import com.fs.course.domain.FsUserCourseCategory;
import com.fs.course.mapper.FsUserCourseCategoryMapper;
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.io.Serializable;
import java.util.concurrent.TimeUnit;

@Service
public class FsUserCourseCategoryCacheServiceImpl implements FsUserCourseCategoryCacheService {
    @Autowired
    private FsUserCourseCategoryMapper fsUserCourseCategoryMapper;
    private static final Cache<String, Long> CATEGORY_CACHE = Caffeine.newBuilder()
            .maximumSize(5000)
            .expireAfterWrite(12, TimeUnit.HOURS)
            .build();

    @Override
    public Long selectCateIdByName(String name) {
        return CATEGORY_CACHE.get(name,e->{
            FsUserCourseCategory fsUserCourseCategory = fsUserCourseCategoryMapper.selectFsUserCourseCategoryByName(name);
            if(fsUserCourseCategory == null) {
                return null;
            }
            return fsUserCourseCategory.getCateId();
        });
    }
}
