| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package com.primeton.dgs.kernel.core.cache.impl;
- import com.primeton.dgs.kernel.core.cache.ICache;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.FactoryBean;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- import java.util.Properties;
- /**
- * <pre>
- *
- * Created by zhaopx.
- * User: zhaopx
- * Date: 2019/11/14
- * Time: 14:22
- *
- * </pre>
- *
- * @author zhaopx
- */
- public class CacheFactory implements FactoryBean {
- /**
- * logger
- */
- private static Logger logger = LoggerFactory.getLogger(CacheFactory.class);
- /**
- * 配置
- */
- private final Properties config;
- /**
- * Cache
- */
- private final ICache cache;
- /**
- * 默认是 ehcache
- * @param config
- */
- public CacheFactory(Properties config) {
- this(config == null ? "ehcache" : config.getProperty("type", "ehcache"), config);
- }
- public CacheFactory(String cacheType, Properties props) {
- this.config = Optional.ofNullable(props).orElse(new Properties());
- if(StringUtils.isBlank(cacheType) || "mem".equalsIgnoreCase(cacheType) ||
- "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"));
- logger.info("use redis cache {}:{}/{}", host, port, db);
- this.cache = initRedisCache(host, port, db);
- } else if("memcache".equalsIgnoreCase(cacheType)) {
- String address = this.config.getProperty("address");
- if(StringUtils.isBlank(address)) {
- throw new IllegalArgumentException("memcache address must not be blank. Usage: host1:port,host2:port");
- }
- logger.info("use memcache address {}", address);
- this.cache = initMemCache(address);
- } else {
- throw new IllegalArgumentException("unknown cache type " + cacheType);
- }
- }
- /**
- * 初始化 MemCache
- * @return
- */
- private ICache initMemCache(String hosts) {
- List<InetSocketAddress> addresses = new ArrayList<>();
- String[] addrs = hosts.split(",");
- for (String addr : addrs) {
- String[] hostAndPort = addr.split(":");
- addresses.add(new InetSocketAddress(StringUtils.trim(hostAndPort[0]),
- Integer.parseInt(StringUtils.trim(hostAndPort[1]))));
- }
- net.rubyeye.xmemcached.XMemcachedClientBuilder clientBuilder =
- new net.rubyeye.xmemcached.XMemcachedClientBuilder(addresses);
- clientBuilder.setConnectionPoolSize(2);
- clientBuilder.setCommandFactory(new net.rubyeye.xmemcached.command.TextCommandFactory());
- clientBuilder.setSessionLocator(new net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator());
- clientBuilder.setTranscoder(new net.rubyeye.xmemcached.transcoders.SerializingTranscoder());
- try {
- return new Memcached(clientBuilder.build());
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- }
- /**
- * 初始化 Redis 客户端
- * @param host
- * @param port
- * @param db
- * @return
- */
- private ICache initRedisCache(String host, int port, int db) {
- return new Redised(host, port, db);
- }
- /**
- * 初始化 EHCache 客户端
- * @return
- */
- private ICache initEhcache() {
- return new Ehcache();
- }
- @Override
- public Object getObject() throws Exception {
- return cache;
- }
- @Override
- public Class getObjectType() {
- return ICache.class;
- }
- @Override
- public boolean isSingleton() {
- return true;
- }
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("/spring/corebean/context-cache.xml");
- ICache cache = (ICache)context.getBean("cache");
- System.out.println(cache.size());
- }
- }
|