package com.sagacloud.cache;

import com.sagacloud.cache.cachedObject.pojo.ObjectTableObjectMap;
import com.sagacloud.cache.cachedObject.QueryObjects;
import com.sagacloud.cache.cachedObject.pojo.VenderInfoMap;
import com.sagacloud.search.AllObjs;
import com.sagacloud.utils.OssUtils;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;

import java.time.Duration;

/**
 * Created by wo on 7/5/18.
 */
public class EhCaches {
    public static final String ALL_OBJS_UNDER_PROJECT = "allObjsUnderProject";
    public static final String CACHE_FOR_QUERY = "queryCache";
    public static final String OBJECT_TABLE_CACHE = "ObjectTableCache";
    public static final String VENDER_CACHE = "VenderCache";

    private static CacheManager cacheManager;

    static {
        //设置缓存区大小的用法
        /*cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .withCache(ALL_OBJS_UNDER_PROJECT, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, AllObjs.class,
                        ResourcePoolsBuilder.newResourcePoolsBuilder().heap(20, MemoryUnit.MB))
                        .build())
                .build(true);*/

        cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .withCache(ALL_OBJS_UNDER_PROJECT, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, AllObjs.class,
                        ResourcePoolsBuilder.heap(20)).build())
                .withCache(CACHE_FOR_QUERY, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, QueryObjects.class,
                        ResourcePoolsBuilder.heap(10)).build())
                // 对象表的缓存
                .withCache(OBJECT_TABLE_CACHE, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, ObjectTableObjectMap.class,
                        ResourcePoolsBuilder.heap(10)).build())
                // 厂商库信息缓存
                .withCache(VENDER_CACHE, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, VenderInfoMap.class,
                        ResourcePoolsBuilder.heap(10)).build())
                .build(true);

    }

    public static <K, V> Cache<K, V> getCache(String var1, Class<K> var2, Class<V> var3){
        return cacheManager.getCache(var1, var2, var3);
    }
}
