/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.config;

import com.crunii.micro.component.redis.config.RedisCacheAutoConfiguration;
import com.github.benmanes.caffeine.cache.CacheLoader;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

/**
 * @author 田平 create 2020年2月27日下午3:08:14
 */
@Configuration
@ConditionalOnMissingBean(RedisCacheAutoConfiguration.class)
public class CacheConfiguration {

	/**
	 * <pre>自定义参数</pre>
	 *
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean
	public CacheManagerCustomizer<CaffeineCacheManager> cacheManagerCustomizer() {
		return new CacheManagerCustomizer<CaffeineCacheManager>() {
			@Override
			public void customize(CaffeineCacheManager cacheManager) {
				cacheManager.setAllowNullValues(true);
				// 可以单独设置某些缓存的过期策略
			}
		};
	}

	/**
	 * <pre>caffeine refreshAfterWrite 参数需要</pre>
	 *
	 * @return
	 */
	@Bean
	public CacheLoader<Object, Object> cacheLoader() {
		CacheLoader<Object, Object> cacheLoader = new CacheLoader<Object, Object>() {
			@Nullable
			@Override
			public Object load(@NonNull Object key) throws Exception {
				return null;
			}
		};
		return cacheLoader;
	}
}
