CacheFactory.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.yiidata.intergration.web.modules.sys.cache;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.DisposableBean;
  6. import org.springframework.beans.factory.FactoryBean;
  7. import org.springframework.beans.factory.InitializingBean;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.stereotype.Component;
  10. import java.util.Optional;
  11. import java.util.Properties;
  12. /**
  13. * <pre>
  14. *
  15. * Created by zhaopx.
  16. * User: zhaopx
  17. * Date: 2019/11/14
  18. * Time: 14:22
  19. *
  20. * </pre>
  21. *
  22. * @author zhaopx
  23. */
  24. @Component("cache")
  25. public class CacheFactory implements FactoryBean, InitializingBean, DisposableBean {
  26. /**
  27. * logger
  28. */
  29. private static Logger logger = LoggerFactory.getLogger(CacheFactory.class);
  30. @Value("${cache.type: mem}")
  31. String type;
  32. @Value("${cache.redis.database: 0}")
  33. int redisDb;
  34. @Value("${cache.redis.host: localhost}")
  35. String redisHost;
  36. @Value("${cache.redis.port: 6379}")
  37. int redisPort;
  38. /**
  39. * 配置
  40. */
  41. private Properties config;
  42. /**
  43. * Cache
  44. */
  45. private ICache cache;
  46. public CacheFactory() {
  47. }
  48. @Override
  49. public void afterPropertiesSet() throws Exception {
  50. config = new Properties();
  51. config.setProperty("type", Optional.ofNullable(StringUtils.trimToNull(type)).orElse("mem"));
  52. config.setProperty("host", redisHost);
  53. config.setProperty("port", ""+redisPort);
  54. config.setProperty("db", ""+redisDb);
  55. String cacheType = config.getProperty("type", "ehcache");
  56. if(StringUtils.isBlank(cacheType) || "mem".equalsIgnoreCase(cacheType) ||
  57. "ehcache".equalsIgnoreCase(cacheType)) {
  58. logger.info("use ehcache.");
  59. this.cache = initEhcache();
  60. } else if("redis".equalsIgnoreCase(cacheType)) {
  61. String host = this.config.getProperty("host");
  62. if(StringUtils.isBlank(host)) {
  63. throw new IllegalArgumentException("redis cache host must not be blank.");
  64. }
  65. int port = Integer.parseInt(this.config.getProperty("port", "3306"));
  66. int db = Integer.parseInt(this.config.getProperty("db", "0"));
  67. logger.info("use redis cache {}:{}/{}", host, port, db);
  68. this.cache = initRedisCache(host, port, db);
  69. } else {
  70. throw new IllegalArgumentException("unknown cache type " + cacheType);
  71. }
  72. }
  73. /**
  74. * 初始化 Redis 客户端
  75. * @param host
  76. * @param port
  77. * @param db
  78. * @return
  79. */
  80. private ICache initRedisCache(String host, int port, int db) {
  81. return new Redised(host, port, db);
  82. }
  83. /**
  84. * 初始化 EHCache 客户端
  85. * @return
  86. */
  87. private ICache initEhcache() {
  88. return new Ehcache();
  89. }
  90. @Override
  91. public Object getObject() throws Exception {
  92. return cache;
  93. }
  94. @Override
  95. public Class getObjectType() {
  96. return ICache.class;
  97. }
  98. @Override
  99. public boolean isSingleton() {
  100. return true;
  101. }
  102. @Override
  103. public void destroy() throws Exception {
  104. cache.shutdown();
  105. }
  106. }