package com.yiidata.intergration.web.modules.sys.cache; import com.primeton.damp.cache.ICache; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Optional; import java.util.Properties; /** *
 *
 * Created by zhaopx.
 * User: zhaopx
 * Date: 2019/11/14
 * Time: 14:22
 *
 * 
* * @author zhaopx */ @Component("cache") public class CacheFactory implements FactoryBean, InitializingBean, DisposableBean { /** * logger */ private static Logger logger = LoggerFactory.getLogger(CacheFactory.class); @Value("${cache.type: mem}") String type; @Value("${cache.redis.database: 0}") int redisDb; @Value("${cache.redis.host: localhost}") String redisHost; @Value("${cache.redis.port: 6379}") int redisPort; @Value("${cache.redis.password: }") String redisPassword; /** * 配置 */ private Properties config; /** * Cache */ private ICache cache; public CacheFactory() { } @Override public void afterPropertiesSet() throws Exception { config = new Properties(); config.setProperty("type", Optional.ofNullable(StringUtils.trimToNull(type)).orElse("mem")); config.setProperty("host", redisHost); config.setProperty("port", ""+redisPort); config.setProperty("password", redisPassword); config.setProperty("db", ""+redisDb); String cacheType = config.getProperty("type", "mem"); if (StringUtils.isBlank(cacheType) || "mem".equalsIgnoreCase(cacheType) || "memory".equalsIgnoreCase(cacheType)) { logger.info("use guava cache."); this.cache = new GuavaMemCached(); } else if ("leveldb".equalsIgnoreCase(cacheType)) { logger.info("use leveldb."); this.cache = initLevelDb(); } else if ("ehcache".equalsIgnoreCase(cacheType)) { logger.info("use ehcache."); this.cache = initEhcache(); } else if("redis".equalsIgnoreCase(cacheType)) { String host = this.config.getProperty("host"); if(StringUtils.isBlank(host)) { throw new IllegalArgumentException("redis cache host must not be blank."); } int port = Integer.parseInt(this.config.getProperty("port", "3306")); int db = Integer.parseInt(this.config.getProperty("db", "0")); String password = StringUtils.trimToNull(this.config.getProperty("password")); logger.info("use redis cache {}:{}/{}", host, port, db); this.cache = initRedisCache(host, port, password, db); } else { throw new IllegalArgumentException("unknown cache type " + cacheType); } } /** * 初始化 Redis 客户端 * @param host redis host * @param port redis port * @param password redis 密码 * @param db * @return */ private ICache initRedisCache(String host, int port, String password, int db) { return new Redised(host, port, password, db); } /** * 初始化 LevelDB 客户端 * @return */ private ICache initLevelDb() { return new LevelDB(); } /** * 初始化 EHCache 客户端 * @return */ private ICache initEhcache() { return new Ehcache(); } @Override public ICache getObject() throws Exception { return cache; } @Override public Class getObjectType() { return ICache.class; } @Override public boolean isSingleton() { return true; } @Override public void destroy() throws Exception { cache.shutdown(); } }