package com.fs.hisStore.service.cache.impl;

import com.fs.hisStore.domain.FsUserCourseCountScrm;
import com.fs.hisStore.service.IFsUserCourseCountScrmService;
import com.fs.hisStore.service.cache.IFsUserCourseCountCacheService;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
@Slf4j
public class FsUserCourseCountCacheScrmServiceImpl implements IFsUserCourseCountCacheService {
    private static final Cache<Long, FsUserCourseCountScrm> USER_COURSE_COUNT_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();


    @Autowired
    private IFsUserCourseCountScrmService fsUserCourseCountService;


    @Override
    public FsUserCourseCountScrm findByUserId(Long userId) {
        return USER_COURSE_COUNT_CACHE.get(userId,e->{
            FsUserCourseCountScrm fsUserCourseCount = fsUserCourseCountService.findByUserId(userId);
            return fsUserCourseCount;
        });
    }
}
