package com.ssyf.plugs.cache.config;

import com.ssyf.plugs.cache.properties.CacheProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.io.ClassPathResource;

/**
 * ehcache配置
 * @author: huwei
 * @date: 2020/6/15 9:42
 * @version: 1.0.0
 */
@Configuration
@ConditionalOnProperty(value = "cache.ehcache-enabled")
public class EhcacheConfig {

    @Autowired
    private CacheProperties ecacheProperties;

    @Bean
    @DependsOn("ehCacheManagerFactoryBean")
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean) {
        EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager(bean.getObject());
        return ehCacheCacheManager;
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource(this.ecacheProperties.getConfig()));
        //共享eh的缓存
        cacheManagerFactoryBean.setShared(this.ecacheProperties.isEhcacheShare());
        return cacheManagerFactoryBean;
    }
}
