PL-5127 Temporary cache for non-cached configs on web

This commit is contained in:
Yuriy Artamonov 2016-05-27 19:35:52 +04:00
parent e469e2e3cf
commit f403e9bd51
7 changed files with 217 additions and 29 deletions

View File

@ -21,9 +21,10 @@ import com.haulmont.cuba.client.ClientConfiguration;
import com.haulmont.cuba.client.sys.config.ConfigPersisterClientImpl;
import com.haulmont.cuba.core.config.Config;
import com.haulmont.cuba.core.config.ConfigHandler;
import com.haulmont.cuba.core.config.ConfigPersister;
import com.haulmont.cuba.core.global.Configuration;
import org.springframework.stereotype.Component;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -37,7 +38,7 @@ public class ConfigurationClientImpl implements ClientConfiguration {
public <T extends Config> T getConfig(Class<T> configInterface) {
ConfigHandler handler = handlersCache.get(configInterface);
if (handler == null) {
handler = new ConfigHandler(new ConfigPersisterClientImpl(false), configInterface);
handler = new ConfigHandler(createConfigPersister(false), configInterface);
handlersCache.put(configInterface, handler);
}
ClassLoader classLoader = configInterface.getClassLoader();
@ -47,8 +48,12 @@ public class ConfigurationClientImpl implements ClientConfiguration {
@Override
public <T extends Config> T getConfigCached(Class<T> configInterface) {
ConfigHandler handler = new ConfigHandler(new ConfigPersisterClientImpl(true), configInterface);
ConfigHandler handler = new ConfigHandler(createConfigPersister(true), configInterface);
Object proxy = Proxy.newProxyInstance(configInterface.getClassLoader(), new Class[]{configInterface}, handler);
return configInterface.cast(proxy);
}
protected ConfigPersister createConfigPersister(boolean caching) {
return new ConfigPersisterClientImpl(caching);
}
}

View File

@ -18,41 +18,20 @@
package com.haulmont.cuba.desktop.sys.config;
import com.haulmont.cuba.client.ClientConfiguration;
import com.haulmont.cuba.core.config.Config;
import com.haulmont.cuba.core.config.ConfigHandler;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.haulmont.cuba.client.sys.ConfigurationClientImpl;
import com.haulmont.cuba.core.config.ConfigPersister;
/**
* Desktop specific implementation of Configuration that uses application scope cache for db properties.
*
* @see com.haulmont.cuba.desktop.sys.config.DesktopConfigStorageCache
*
*/
public class DesktopConfigurationImpl implements ClientConfiguration {
protected Map<Class, ConfigHandler> handlersCache = new ConcurrentHashMap<>();
public class DesktopConfigurationImpl extends ConfigurationClientImpl implements ClientConfiguration {
protected DesktopConfigStorageCache configStorageCache = new DesktopConfigStorageCache();
@Override
public <T extends Config> T getConfig(Class<T> configInterface) {
ConfigHandler handler = handlersCache.get(configInterface);
if (handler == null) {
handler = new ConfigHandler(new DesktopConfigPersisterImpl(configStorageCache, false), configInterface);
handlersCache.put(configInterface, handler);
}
ClassLoader classLoader = configInterface.getClassLoader();
Object proxy = Proxy.newProxyInstance(classLoader, new Class[]{configInterface}, handler);
return configInterface.cast(proxy);
}
@Override
public <T extends Config> T getConfigCached(Class<T> configInterface) {
ConfigHandler handler = new ConfigHandler(new DesktopConfigPersisterImpl(configStorageCache, true), configInterface);
Object proxy = Proxy.newProxyInstance(configInterface.getClassLoader(), new Class[]{configInterface}, handler);
return configInterface.cast(proxy);
protected ConfigPersister createConfigPersister(boolean caching) {
return new DesktopConfigPersisterImpl(configStorageCache, caching);
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2008-2016 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.web.cache;
import com.haulmont.cuba.client.sys.cache.CachingStrategy;
import com.haulmont.cuba.core.app.ConfigStorageService;
import org.springframework.stereotype.Component;
import javax.inject.Inject;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
@Component(WebConfigCacheStrategy.NAME)
public class WebConfigCacheStrategy implements CachingStrategy {
public static final String NAME = "cuba_WebConfigCacheStrategy";
protected volatile Map<String, String> cachedProperties = Collections.unmodifiableMap(new HashMap<>());
@Inject
protected ConfigStorageService configStorageService;
protected volatile long lastUsedTs = 0;
protected volatile long updateIntervalMs = 60 * 1000;
protected ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
@Override
public void init() {
}
@Override
public Object getObject() {
return cachedProperties;
}
@Override
public Object loadObject() {
Map<String, String> cachedPropertiesFromServer = Collections.unmodifiableMap(configStorageService.getDbProperties());
cachedProperties = cachedPropertiesFromServer;
lastUsedTs = System.currentTimeMillis();
return cachedPropertiesFromServer;
}
@Override
public ReadWriteLock lock() {
return readWriteLock;
}
@Override
public boolean needToReload() {
return System.currentTimeMillis() - lastUsedTs > updateIntervalMs;
}
public long getUpdateIntervalMs() {
return updateIntervalMs;
}
public void setUpdateIntervalMs(long updateIntervalMs) {
this.updateIntervalMs = updateIntervalMs;
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2008-2016 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.web.sys.config;
import com.haulmont.cuba.client.sys.cache.ClientCacheManager;
import com.haulmont.cuba.core.app.ConfigStorageService;
import com.haulmont.cuba.core.config.AppPropertyEntity;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.web.cache.WebConfigCacheStrategy;
import java.util.List;
import java.util.Map;
public class WebConfigStorageCache implements ConfigStorageService {
@Override
public Map<String, String> getDbProperties() {
return getClientCacheManager().getCached(WebConfigCacheStrategy.NAME);
}
@Override
public String getDbProperty(String name) {
return getDbProperties().get(name);
}
@Override
public void setDbProperty(String name, String value) {
getService().setDbProperty(name, value);
getClientCacheManager().refreshCached(WebConfigStorageCache.NAME);
}
protected ConfigStorageService getService() {
return AppBeans.get(ConfigStorageService.NAME);
}
protected ClientCacheManager getClientCacheManager() {
return AppBeans.get(ClientCacheManager.NAME);
}
@Override
public List<AppPropertyEntity> getAppProperties() {
return getService().getAppProperties();
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2008-2016 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.web.sys.config;
import com.haulmont.cuba.client.sys.config.ConfigPersisterClientImpl;
import com.haulmont.cuba.core.app.ConfigStorageService;
public class WebConfigStoragePersisterImpl extends ConfigPersisterClientImpl {
private WebConfigStorageCache configStorageCache;
public WebConfigStoragePersisterImpl(WebConfigStorageCache configStorageCache, boolean caching) {
super(caching);
this.configStorageCache = configStorageCache;
}
@Override
protected ConfigStorageService getConfigStorage() {
return configStorageCache;
}
}

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2008-2016 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.web.sys.config;
import com.haulmont.cuba.client.ClientConfiguration;
import com.haulmont.cuba.client.sys.ConfigurationClientImpl;
import com.haulmont.cuba.core.config.ConfigPersister;
public class WebConfigurationImpl extends ConfigurationClientImpl implements ClientConfiguration {
protected WebConfigStorageCache configStorageCache = new WebConfigStorageCache();
@Override
protected ConfigPersister createConfigPersister(boolean caching) {
return new WebConfigStoragePersisterImpl(configStorageCache, caching);
}
}

View File

@ -40,6 +40,8 @@
<property name="encryptionModule" ref="${cuba.passwordEncryptionModule}"/>
</bean>
<bean id="cuba_Configuration" class="com.haulmont.cuba.web.sys.config.WebConfigurationImpl"/>
<!-- MBeans registration -->
<bean id="cuba_web_MBeanExporter" class="com.haulmont.cuba.core.sys.jmx.MBeanExporter" lazy-init="false">