diff --git a/datagear-web/pom.xml b/datagear-web/pom.xml
index 7f3799f0..70163f29 100644
--- a/datagear-web/pom.xml
+++ b/datagear-web/pom.xml
@@ -85,10 +85,6 @@
org.springframework.boot
spring-boot-starter-validation
-
- org.springframework.boot
- spring-boot-starter-cache
-
com.github.ben-manes.caffeine
caffeine
diff --git a/datagear-web/src/main/java/org/datagear/web/config/CoreConfig.java b/datagear-web/src/main/java/org/datagear/web/config/CoreConfig.java
index 9f2876f2..37b8c2fd 100644
--- a/datagear-web/src/main/java/org/datagear/web/config/CoreConfig.java
+++ b/datagear-web/src/main/java/org/datagear/web/config/CoreConfig.java
@@ -55,7 +55,6 @@ import org.datagear.management.service.AuthorizationService;
import org.datagear.management.service.DataPermissionEntityService;
import org.datagear.management.service.DataSetEntityService;
import org.datagear.management.service.DataSetResDirectoryService;
-import org.datagear.management.service.EntityService;
import org.datagear.management.service.HtmlChartWidgetEntityService;
import org.datagear.management.service.HtmlTplDashboardWidgetEntityService;
import org.datagear.management.service.RoleService;
@@ -74,7 +73,6 @@ import org.datagear.management.service.impl.HtmlTplDashboardWidgetEntityServiceI
import org.datagear.management.service.impl.RoleServiceImpl;
import org.datagear.management.service.impl.RoleUserServiceImpl;
import org.datagear.management.service.impl.SchemaServiceImpl;
-import org.datagear.management.service.impl.ServiceCache;
import org.datagear.management.service.impl.SqlHistoryServiceImpl;
import org.datagear.management.service.impl.UserPasswordEncoder;
import org.datagear.management.service.impl.UserServiceImpl;
@@ -112,7 +110,6 @@ import org.datagear.web.util.XmlDriverEntityManagerInitializer;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
@@ -145,13 +142,16 @@ public class CoreConfig implements ApplicationListener
private DataSourceConfig dataSourceConfig;
+ private ServiceCacheConfig serviceCacheConfig;
+
private Environment environment;
@Autowired
- public CoreConfig(DataSourceConfig dataSourceConfig, Environment environment)
+ public CoreConfig(DataSourceConfig dataSourceConfig, ServiceCacheConfig serviceCacheConfig, Environment environment)
{
super();
this.dataSourceConfig = dataSourceConfig;
+ this.serviceCacheConfig = serviceCacheConfig;
this.environment = environment;
}
@@ -165,6 +165,16 @@ public class CoreConfig implements ApplicationListener
this.dataSourceConfig = dataSourceConfig;
}
+ public ServiceCacheConfig getServiceCacheConfig()
+ {
+ return serviceCacheConfig;
+ }
+
+ public void setServiceCacheConfig(ServiceCacheConfig serviceCacheConfig)
+ {
+ this.serviceCacheConfig = serviceCacheConfig;
+ }
+
public Environment getEnvironment()
{
return environment;
@@ -734,68 +744,50 @@ public class CoreConfig implements ApplicationListener
return bean;
}
- @SuppressWarnings("rawtypes")
@Override
public void onApplicationEvent(ContextRefreshedEvent event)
{
ApplicationContext context = event.getApplicationContext();
- // 初始化this.authorizationResourceServices()
+ initAuthorizationResourceServices(context);
+ initServiceCaches(context);
+ initDevotedDataExchangeServices(context);
+ }
+
+ @SuppressWarnings("rawtypes")
+ protected void initAuthorizationResourceServices(ApplicationContext context)
+ {
+ Map dataPermissionEntityServices = context
+ .getBeansOfType(DataPermissionEntityService.class);
+
+ List> resourceServices = this.authorizationResourceServices();
+ for (DataPermissionEntityService, ?> dps : dataPermissionEntityServices.values())
+ resourceServices.add(dps);
+ }
+
+ @SuppressWarnings("rawtypes")
+ protected void initServiceCaches(ApplicationContext context)
+ {
+ Map entityServices = context
+ .getBeansOfType(AbstractMybatisEntityService.class);
+
+ for (AbstractMybatisEntityService es : entityServices.values())
{
- Map dataPermissionEntityServices = context
- .getBeansOfType(DataPermissionEntityService.class);
+ es.setCache(this.serviceCacheConfig.getServiceCache(es.getClass()));
- List> resourceServices = this.authorizationResourceServices();
- for (DataPermissionEntityService, ?> dps : dataPermissionEntityServices.values())
- resourceServices.add(dps);
- }
-
- // 初始化缓存
- {
- CacheManager cacheManager = context.getBean(CacheManager.class);
-
- Map entityServices = context
- .getBeansOfType(AbstractMybatisEntityService.class);
-
- for (AbstractMybatisEntityService es : entityServices.values())
+ if (es instanceof AbstractMybatisDataPermissionEntityService, ?>)
{
- es.setCache(getServiceCache(cacheManager, es.getClass()));
-
- if (es instanceof AbstractMybatisDataPermissionEntityService, ?>)
- ((AbstractMybatisDataPermissionEntityService, ?>) es)
- .setPermissionCache(getPermissionServiceCache(cacheManager, es.getClass()));
+ ((AbstractMybatisDataPermissionEntityService, ?>) es)
+ .setPermissionCache(this.serviceCacheConfig.getPermissionServiceCache(es.getClass()));
}
}
+ }
+ protected void initDevotedDataExchangeServices(ApplicationContext context)
+ {
// 处理循环依赖
- {
- List> devotedDataExchangeServices = this.devotedDataExchangeServices();
- devotedDataExchangeServices.add(this.batchDataExchangeService());
- this.dataExchangeService().setDevotedDataExchangeServices(devotedDataExchangeServices);
- }
- }
-
- @SuppressWarnings("rawtypes")
- protected ServiceCache getServiceCache(CacheManager cacheManager,
- Class extends EntityService> clazz)
- {
- return getServiceCache(cacheManager, clazz.getSimpleName());
- }
-
- @SuppressWarnings("rawtypes")
- protected ServiceCache getPermissionServiceCache(CacheManager cacheManager,
- Class extends EntityService> clazz)
- {
- return getServiceCache(cacheManager, clazz.getSimpleName() + "Permission");
- }
-
- protected ServiceCache getServiceCache(CacheManager cacheManager, String name)
- {
- ServiceCache cache = new ServiceCache(cacheManager.getCache(name));
-
- cache.setDisabled(environment.getProperty("service.cache.disabled", Boolean.class, false));
- cache.setDisabled(environment.getProperty("service.cache.serialized", Boolean.class, false));
-
- return cache;
+ List> devotedDataExchangeServices = this.devotedDataExchangeServices();
+ devotedDataExchangeServices.add(this.batchDataExchangeService());
+ this.dataExchangeService().setDevotedDataExchangeServices(devotedDataExchangeServices);
}
}
diff --git a/datagear-web/src/main/java/org/datagear/web/config/ServiceCacheConfig.java b/datagear-web/src/main/java/org/datagear/web/config/ServiceCacheConfig.java
new file mode 100644
index 00000000..b14d4a42
--- /dev/null
+++ b/datagear-web/src/main/java/org/datagear/web/config/ServiceCacheConfig.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2018 datagear.tech
+ *
+ * Licensed under the LGPLv3 license:
+ * http://www.gnu.org/licenses/lgpl-3.0.html
+ */
+
+package org.datagear.web.config;
+
+import org.datagear.management.service.impl.ServiceCache;
+import org.datagear.util.StringUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.caffeine.CaffeineCacheManager;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+
+/**
+ * 服务缓存配置。
+ *
+ * @author datagear@163.com
+ *
+ */
+@Configuration
+public class ServiceCacheConfig
+{
+ private Environment environment;
+
+ private CacheManager serviceCacheManager;
+
+ private final boolean disabled;
+
+ private final String cacheSpec;
+
+ @Autowired
+ public ServiceCacheConfig(Environment environment)
+ {
+ super();
+ this.environment = environment;
+ this.disabled = this.environment.getProperty("service.cache.disabled", Boolean.class, false);
+ this.cacheSpec = this.environment.getProperty("service.cache.spec", "");
+
+ if (!disabled)
+ this.serviceCacheManager = createServiceCacheManager();
+ }
+
+ public Environment getEnvironment()
+ {
+ return environment;
+ }
+
+ public void setEnvironment(Environment environment)
+ {
+ this.environment = environment;
+ }
+
+ public boolean isDisabled()
+ {
+ return disabled;
+ }
+
+ public String getCacheSpec()
+ {
+ return cacheSpec;
+ }
+
+ public ServiceCache getServiceCache(Class> serviceClass)
+ {
+ return getServiceCache(serviceClass.getSimpleName());
+ }
+
+ public ServiceCache getPermissionServiceCache(Class> serviceClass)
+ {
+ return getServiceCache(serviceClass.getSimpleName() + "Permission");
+ }
+
+ protected ServiceCache getServiceCache(String name)
+ {
+ ServiceCache serviceCache = new ServiceCache();
+
+ serviceCache.setDisabled(isDisabled() || this.serviceCacheManager == null);
+ serviceCache.setSerialized(false);
+ serviceCache.setShared(false);
+
+ if(this.serviceCacheManager != null)
+ serviceCache.setCache(this.serviceCacheManager.getCache(name));
+
+ return serviceCache;
+ }
+
+ protected CacheManager createServiceCacheManager()
+ {
+ CaffeineCacheManager bean = new CaffeineCacheManager();
+
+ if (!StringUtil.isEmpty(this.cacheSpec))
+ bean.setCacheSpecification(this.cacheSpec);
+
+ return bean;
+ }
+}
diff --git a/datagear-web/src/main/resources/org/datagear/web/application.properties b/datagear-web/src/main/resources/org/datagear/web/application.properties
index bc6d0d6a..c228c094 100644
--- a/datagear-web/src/main/resources/org/datagear/web/application.properties
+++ b/datagear-web/src/main/resources/org/datagear/web/application.properties
@@ -71,10 +71,13 @@ datasource.password=
#数据库方言,可选项:derby、mysql、oracle、postgresql、default,留空则表示自动判断
datasourceDialect=
-#服务层缓存是否禁用:true 禁用;false 启用
+#服务层缓存配置:
+#是否禁用缓存:true 禁用;false 启用
service.cache.disabled=false
-#服务层采用的缓存库是否支持序列化:true 支持;false 不支持,当为false时,服务层会克隆缓存对象以确保缓存不被修改
-service.cache.serialized=false
+#缓存配置项
+#maximumSize 缓存容量
+#expireAfterAccess 过期时间
+service.cache.spec=maximumSize=100,expireAfterAccess=1d
#Spring Boot配置
#-----------------------------------------