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

import com.fs.store.domain.FsUserCourseCount;
import com.fs.store.service.IFsUserCourseCountService;
import com.fs.store.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 FsUserCourseCountCacheServiceImpl implements IFsUserCourseCountCacheService {
    private static final Cache<Long, FsUserCourseCount> USER_COURSE_COUNT_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();


    @Autowired
    private IFsUserCourseCountService fsUserCourseCountService;


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