服务缓存配置改为采用ServiceCacheConfig而非spring-boot-starter-cache,便于更规范服务缓存配置

This commit is contained in:
datagear 2021-08-17 23:05:19 +08:00
parent 9f2dbf51b2
commit 004b40b9b7
4 changed files with 153 additions and 62 deletions

View File

@ -85,10 +85,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>

View File

@ -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<ContextRefreshedEvent>
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<ContextRefreshedEvent>
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<ContextRefreshedEvent>
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<String, DataPermissionEntityService> dataPermissionEntityServices = context
.getBeansOfType(DataPermissionEntityService.class);
List<DataPermissionEntityService<?, ?>> resourceServices = this.authorizationResourceServices();
for (DataPermissionEntityService<?, ?> dps : dataPermissionEntityServices.values())
resourceServices.add(dps);
}
@SuppressWarnings("rawtypes")
protected void initServiceCaches(ApplicationContext context)
{
Map<String, AbstractMybatisEntityService> entityServices = context
.getBeansOfType(AbstractMybatisEntityService.class);
for (AbstractMybatisEntityService es : entityServices.values())
{
Map<String, DataPermissionEntityService> dataPermissionEntityServices = context
.getBeansOfType(DataPermissionEntityService.class);
es.setCache(this.serviceCacheConfig.getServiceCache(es.getClass()));
List<DataPermissionEntityService<?, ?>> resourceServices = this.authorizationResourceServices();
for (DataPermissionEntityService<?, ?> dps : dataPermissionEntityServices.values())
resourceServices.add(dps);
}
// 初始化缓存
{
CacheManager cacheManager = context.getBean(CacheManager.class);
Map<String, AbstractMybatisEntityService> 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<DevotedDataExchangeService<?>> 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<DevotedDataExchangeService<?>> devotedDataExchangeServices = this.devotedDataExchangeServices();
devotedDataExchangeServices.add(this.batchDataExchangeService());
this.dataExchangeService().setDevotedDataExchangeServices(devotedDataExchangeServices);
}
}

View File

@ -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;
}
}

View File

@ -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配置
#-----------------------------------------