CacheFactory.java 3.9 KB

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