content: /**
   * Created on 2018/8/11.
   */
  package jetcache.samples.spring;
  
  import com.alicp.jetcache.anno.CacheConsts;
  import com.alicp.jetcache.anno.config.EnableMethodCache;
  import com.alicp.jetcache.anno.support.GlobalCacheConfig;
  import com.alicp.jetcache.anno.support.JetCacheBaseBeans;
  import com.alicp.jetcache.embedded.EmbeddedCacheBuilder;
  import com.alicp.jetcache.embedded.LinkedHashMapCacheBuilder;
  import com.alicp.jetcache.redis.RedisCacheBuilder;
  import com.alicp.jetcache.support.Fastjson2KeyConvertor;
  import com.alicp.jetcache.support.JavaValueDecoder;
  import com.alicp.jetcache.support.JavaValueEncoder;
  import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import redis.clients.jedis.Jedis;
  import redis.clients.jedis.JedisPool;
  import redis.clients.jedis.util.Pool;
  
  import java.util.HashMap;
  import java.util.Map;
  
  @Configuration
  @EnableMethodCache(basePackages = "jetcache.samples.spring")
  @Import(JetCacheBaseBeans.class)
  public class JetCacheConfig {
  
      @Bean
      public Pool<Jedis> pool(){
          GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
          pc.setMinIdle(2);
          pc.setMaxIdle(10);
          pc.setMaxTotal(10);
          return new JedisPool(pc, "127.0.0.1", 6379);
      }
  
      @Bean
      public GlobalCacheConfig config(Pool<Jedis> pool){
          Map localBuilders = new HashMap();
          EmbeddedCacheBuilder localBuilder = LinkedHashMapCacheBuilder
                  .createLinkedHashMapCacheBuilder()
                  .keyConvertor(Fastjson2KeyConvertor.INSTANCE);
          localBuilders.put(CacheConsts.DEFAULT_AREA, localBuilder);
  
          Map remoteBuilders = new HashMap();
          RedisCacheBuilder remoteCacheBuilder = RedisCacheBuilder.createRedisCacheBuilder()
                  .keyConvertor(Fastjson2KeyConvertor.INSTANCE)
                  .valueEncoder(JavaValueEncoder.INSTANCE)
                  .valueDecoder(JavaValueDecoder.INSTANCE)
                  .jedisPool(pool);
          remoteBuilders.put(CacheConsts.DEFAULT_AREA, remoteCacheBuilder);
  
          GlobalCacheConfig globalCacheConfig = new GlobalCacheConfig();
          globalCacheConfig.setLocalCacheBuilders(localBuilders);
          globalCacheConfig.setRemoteCacheBuilders(remoteBuilders);
          globalCacheConfig.setStatIntervalMinutes(1);
  
          return globalCacheConfig;
      }
  }
  