heheng
5 天以前 3aacec366786f81ec475b8577d6c857faa427252
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.gkhy.exam.common.config;
 
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.util.concurrent.TimeUnit;
 
 
public class CaffeineConfig {
 
    /**
     * 系统默认缓存TTL时间:10分钟
     */
    public static final long SYSTEM_DEFAULT_EXPIRES = 10 * 60 * 1000;
 
    @Bean
    public Cache<String,Object> caffeineCache(){
        return Caffeine.newBuilder()
                // 设置最后一次写入或访问后经过固定时间过期
                .expireAfterWrite(SYSTEM_DEFAULT_EXPIRES, TimeUnit.MILLISECONDS)
                // 初始的缓存空间大小
                .initialCapacity(100)
                // 缓存的最大条数
                .maximumSize(10000)
                .build();
    }
 
    //@Bean
    public CacheManager cacheManager() {
        // create and return an instance of your preferred cache manager, for example:
        return new ConcurrentMapCacheManager();
    }
}