// Path: jbase-cache/src/main/java/com/jayqqaa12/jbase/cache/provider/NullCacheProvider.java
// public class NullCacheProvider implements CacheProvider {
// 
//   private static final NullCache cache=new NullCache();
// 
//   @Override
//   public void init(CacheConfig cacheConfig) {
//   }
//   @Override
//   public Cache buildCache(String region, int expire) {
//     return buildCache(region);
//   }
// 
//   @Override
//   public Cache buildCache(String region) {
//     return cache;
//   }
// 
//   @Override
//   public void stop() {
// 
//   }
// }
// 
// Path: jbase-cache/src/main/java/com/jayqqaa12/jbase/cache/provider/lettuce/LettuceCacheProvider.java
// @Slf4j
// public class LettuceCacheProvider implements CacheProvider {
// 
//   private static AbstractRedisClient redisClient;
//   private GenericObjectPool<StatefulConnection<String, byte[]>> pool;
// 
//   private LettuceByteCodec codec = new LettuceByteCodec();
// 
//   private final ConcurrentHashMap<String, Cache> regions = new ConcurrentHashMap<>();
//   private CacheConfig cacheConfig;
// 
//   @Override
//   public void init(CacheConfig cacheConfig) {
//     this.cacheConfig = cacheConfig;
// 
//     String hosts = cacheConfig.getLettuceConfig().getHosts();
// 
//     int database = cacheConfig.getLettuceConfig().getDatabase();
//     String password = cacheConfig.getLettuceConfig().getPassword();
// 
//     int clusterTopologyRefresh = cacheConfig.getLettuceConfig().getClusterTopologyRefresh();
// 
//     if (REDIS_MODE_CLUSTER.equalsIgnoreCase(cacheConfig.getLettuceConfig().getSchema())) {
//       List<RedisURI> redisURIs = new ArrayList<>();
//       String[] hostArray = hosts.split(",");
//       for (String host : hostArray) {
//         String[] redisArray = host.split(":");
//         RedisURI uri = RedisURI.create(redisArray[0], Integer.valueOf(redisArray[1]));
//         uri.setDatabase(database);
//         uri.setPassword(password);
// //        uri.setSentinelMasterId(sentinelMasterId);
//         redisURIs.add(uri);
//       }
//       redisClient = RedisClusterClient.create(redisURIs);
//       ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
//           //开启自适应刷新
//           .enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT,
//               ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)
//           .enableAllAdaptiveRefreshTriggers()
//           .adaptiveRefreshTriggersTimeout(Duration.ofMillis(clusterTopologyRefresh))
//           //开启定时刷新,时间间隔根据实际情况修改
//           .enablePeriodicRefresh(Duration.ofMillis(clusterTopologyRefresh))
//           .build();
//       ((RedisClusterClient) redisClient).setOptions(
//           ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build());
//     } else {
//       String[] redisArray = hosts.split(":");
//       RedisURI uri = RedisURI.create(redisArray[0], Integer.valueOf(redisArray[1]));
//       uri.setDatabase(database);
//       uri.setPassword(password);
//       redisClient = RedisClient.create(uri);
//     }
// 
//     try {
//       redisClient.setDefaultTimeout(Duration.ofMillis(cacheConfig.getLettuceConfig().getTimeout()));
//     } catch (Exception e) {
//       log.warn("Failed to set default timeout, using default 10000 milliseconds.", e);
//     }
// 
//     //connection pool configurations
//     GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
//     poolConfig.setMaxTotal(cacheConfig.getLettuceConfig().getMaxTotal());
//     poolConfig.setMaxIdle(cacheConfig.getLettuceConfig().getMaxIdle());
//     poolConfig.setMinIdle(cacheConfig.getLettuceConfig().getMinIdle());
// 
//     pool = ConnectionPoolSupport.createGenericObjectPool(() -> {
//       if (redisClient instanceof RedisClient) {
//         return ((RedisClient) redisClient).connect(codec);
//       } else if (redisClient instanceof RedisClusterClient) {
//         return ((RedisClusterClient) redisClient).connect(codec);
//       }
//       return null;
//     }, poolConfig);
// 
//   }
// 
//   @Override
//   public Cache buildCache(String region, int expire) throws CacheException {
//     return buildCache(region);
//   }
// 
//   @Override
//   public Cache buildCache(String region) throws CacheException {
// 
//     try {
// 
//     CacheSerializer cacheSerializer = (CacheSerializer) Class
//         .forName(cacheConfig.getCacheSerializerClass()).newInstance();
//     String namespace = this.cacheConfig.getLettuceConfig().getNamespace();
// 
//     return regions
//         .computeIfAbsent(namespace + ":" + region,
//             k -> new LettuceCache(cacheSerializer, namespace, region, pool));
//     }catch (Exception e){
//       throw  new CacheException(e);
//     }
// 
//   }
// 
//   @Override
//   public void stop() {
// 
//     pool.close();
//     regions.clear();
//     redisClient.shutdown();
// 
//   }
// }


import static org.junit.Assert.assertEquals;
import com.google.common.collect.Lists;
import com.jayqqaa12.jbase.cache.provider.NullCacheProvider;
import com.jayqqaa12.jbase.cache.provider.lettuce.LettuceCacheProvider;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import redis.embedded.RedisServer;
import java.util.concurrent.TimeUnit;
package com.jayqqaa12.jbase.cache.core;




/**
 * @author 12, {@literal <shuai.wang@leyantech.com>}
 * @date 2020-10-12.
 */
public class LettuceTest {

  private static JbaseCache jbaseCache;
  private static RedisServer redisServer;

  
  @BeforeClass
  public static void init() {

    redisServer = RedisServer.builder()
        .port(7777)
        .setting("bind localhost")
        .build() ;

    CacheConfig cacheConfig = new CacheConfig();

    cacheConfig.setProviderClassList(
        Lists.newArrayList(NullCacheProvider.class.getName(),
// Path: jbase-cache/src/main/java/com/jayqqaa12/jbase/cache/provider/NullCacheProvider.java
// public class NullCacheProvider implements CacheProvider {
// 
//   private static final NullCache cache=new NullCache();
// 
//   @Override
//   public void init(CacheConfig cacheConfig) {
//   }
//   @Override
//   public Cache buildCache(String region, int expire) {
//     return buildCache(region);
//   }
// 
//   @Override
//   public Cache buildCache(String region) {
//     return cache;
//   }
// 
//   @Override
//   public void stop() {
// 
//   }
// }
// 
// Path: jbase-cache/src/main/java/com/jayqqaa12/jbase/cache/provider/lettuce/LettuceCacheProvider.java
// @Slf4j
// public class LettuceCacheProvider implements CacheProvider {
// 
//   private static AbstractRedisClient redisClient;
//   private GenericObjectPool<StatefulConnection<String, byte[]>> pool;
// 
//   private LettuceByteCodec codec = new LettuceByteCodec();
// 
//   private final ConcurrentHashMap<String, Cache> regions = new ConcurrentHashMap<>();
//   private CacheConfig cacheConfig;
// 
//   @Override
//   public void init(CacheConfig cacheConfig) {
//     this.cacheConfig = cacheConfig;
// 
//     String hosts = cacheConfig.getLettuceConfig().getHosts();
// 
//     int database = cacheConfig.getLettuceConfig().getDatabase();
//     String password = cacheConfig.getLettuceConfig().getPassword();
// 
//     int clusterTopologyRefresh = cacheConfig.getLettuceConfig().getClusterTopologyRefresh();
// 
//     if (REDIS_MODE_CLUSTER.equalsIgnoreCase(cacheConfig.getLettuceConfig().getSchema())) {
//       List<RedisURI> redisURIs = new ArrayList<>();
//       String[] hostArray = hosts.split(",");
//       for (String host : hostArray) {
//         String[] redisArray = host.split(":");
//         RedisURI uri = RedisURI.create(redisArray[0], Integer.valueOf(redisArray[1]));
//         uri.setDatabase(database);
//         uri.setPassword(password);
// //        uri.setSentinelMasterId(sentinelMasterId);
//         redisURIs.add(uri);
//       }
//       redisClient = RedisClusterClient.create(redisURIs);
//       ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
//           //开启自适应刷新
//           .enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT,
//               ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)
//           .enableAllAdaptiveRefreshTriggers()
//           .adaptiveRefreshTriggersTimeout(Duration.ofMillis(clusterTopologyRefresh))
//           //开启定时刷新,时间间隔根据实际情况修改
//           .enablePeriodicRefresh(Duration.ofMillis(clusterTopologyRefresh))
//           .build();
//       ((RedisClusterClient) redisClient).setOptions(
//           ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build());
//     } else {
//       String[] redisArray = hosts.split(":");
//       RedisURI uri = RedisURI.create(redisArray[0], Integer.valueOf(redisArray[1]));
//       uri.setDatabase(database);
//       uri.setPassword(password);
//       redisClient = RedisClient.create(uri);
//     }
// 
//     try {
//       redisClient.setDefaultTimeout(Duration.ofMillis(cacheConfig.getLettuceConfig().getTimeout()));
//     } catch (Exception e) {
//       log.warn("Failed to set default timeout, using default 10000 milliseconds.", e);
//     }
// 
//     //connection pool configurations
//     GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
//     poolConfig.setMaxTotal(cacheConfig.getLettuceConfig().getMaxTotal());
//     poolConfig.setMaxIdle(cacheConfig.getLettuceConfig().getMaxIdle());
//     poolConfig.setMinIdle(cacheConfig.getLettuceConfig().getMinIdle());
// 
//     pool = ConnectionPoolSupport.createGenericObjectPool(() -> {
//       if (redisClient instanceof RedisClient) {
//         return ((RedisClient) redisClient).connect(codec);
//       } else if (redisClient instanceof RedisClusterClient) {
//         return ((RedisClusterClient) redisClient).connect(codec);
//       }
//       return null;
//     }, poolConfig);
// 
//   }
// 
//   @Override
//   public Cache buildCache(String region, int expire) throws CacheException {
//     return buildCache(region);
//   }
// 
//   @Override
//   public Cache buildCache(String region) throws CacheException {
// 
//     try {
// 
//     CacheSerializer cacheSerializer = (CacheSerializer) Class
//         .forName(cacheConfig.getCacheSerializerClass()).newInstance();
//     String namespace = this.cacheConfig.getLettuceConfig().getNamespace();
// 
//     return regions
//         .computeIfAbsent(namespace + ":" + region,
//             k -> new LettuceCache(cacheSerializer, namespace, region, pool));
//     }catch (Exception e){
//       throw  new CacheException(e);
//     }
// 
//   }
// 
//   @Override
//   public void stop() {
// 
//     pool.close();
//     regions.clear();
//     redisClient.shutdown();
// 
//   }
// }


// Path: jbase-cache/src/test/java/com/jayqqaa12/jbase/cache/core/LettuceTest.java
import static org.junit.Assert.assertEquals;
import com.google.common.collect.Lists;
import com.jayqqaa12.jbase.cache.provider.NullCacheProvider;
import com.jayqqaa12.jbase.cache.provider.lettuce.LettuceCacheProvider;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import redis.embedded.RedisServer;
import java.util.concurrent.TimeUnit;

package com.jayqqaa12.jbase.cache.core;




/**
 * @author 12, {@literal <shuai.wang@leyantech.com>}
 * @date 2020-10-12.
 */
public class LettuceTest {

  private static JbaseCache jbaseCache;
  private static RedisServer redisServer;

  
  @BeforeClass
  public static void init() {

    redisServer = RedisServer.builder()
        .port(7777)
        .setting("bind localhost")
        .build() ;

    CacheConfig cacheConfig = new CacheConfig();

    cacheConfig.setProviderClassList(
        Lists.newArrayList(NullCacheProvider.class.getName(),
            LettuceCacheProvider.class.getName()