net.ymate.platform.cache.Caches Maven / Gradle / Ivy
/*
* Copyright 2007-2019 the original author or authors.
*
* 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 net.ymate.platform.cache;
import net.ymate.platform.cache.impl.DefaultCacheConfig;
import net.ymate.platform.cache.support.CacheableProxy;
import net.ymate.platform.core.IApplication;
import net.ymate.platform.core.IApplicationConfigureFactory;
import net.ymate.platform.core.IApplicationConfigurer;
import net.ymate.platform.core.YMP;
import net.ymate.platform.core.beans.proxy.IProxyFactory;
import net.ymate.platform.core.module.IModule;
import net.ymate.platform.core.module.IModuleConfigurer;
import net.ymate.platform.core.module.impl.DefaultModuleConfigurer;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 缓存管理器
*
* @author 刘镇 ([email protected]) on 14-10-16
*/
public final class Caches implements IModule, ICaches {
private static volatile ICaches instance;
private IApplication owner;
private ICacheConfig config;
private boolean initialized;
public static ICaches get() {
ICaches inst = instance;
if (inst == null) {
synchronized (Caches.class) {
inst = instance;
if (inst == null) {
instance = inst = YMP.get().getModuleManager().getModule(Caches.class);
}
}
}
return inst;
}
public Caches() {
}
public Caches(ICacheConfig config) {
this.config = config;
}
@Override
public String getName() {
return ICaches.MODULE_NAME;
}
@Override
public void initialize(IApplication owner) throws Exception {
if (!initialized) {
//
YMP.showModuleVersion("ymate-platform-cache", this);
//
this.owner = owner;
this.owner.getEvents().registerEvent(CacheEvent.class);
//
if (config == null) {
IApplicationConfigureFactory configureFactory = owner.getConfigureFactory();
if (configureFactory != null) {
IApplicationConfigurer configurer = configureFactory.getConfigurer();
IModuleConfigurer moduleConfigurer = configurer == null ? null : configurer.getModuleConfigurer(MODULE_NAME);
if (moduleConfigurer != null) {
config = DefaultCacheConfig.create(configureFactory.getMainClass(), moduleConfigurer);
} else {
config = DefaultCacheConfig.create(configureFactory.getMainClass(), DefaultModuleConfigurer.createEmpty(MODULE_NAME));
}
}
if (config == null) {
config = DefaultCacheConfig.defaultConfig();
}
}
if (!config.isInitialized()) {
config.initialize(this);
}
//
IProxyFactory proxyFactory = owner.getBeanFactory().getProxyFactory();
if (proxyFactory != null) {
proxyFactory.registerProxy(new CacheableProxy());
}
//
initialized = true;
}
}
@Override
public boolean isInitialized() {
return false;
}
@Override
public void close() throws Exception {
if (initialized) {
initialized = false;
//
config.getCacheProvider().close();
config = null;
owner = null;
}
}
@Override
public IApplication getOwner() {
return owner;
}
@Override
public ICacheConfig getConfig() {
return config;
}
@Override
public boolean isMultilevel() {
return ICache.MULTILEVEL.equalsIgnoreCase(config.getCacheProvider().getName());
}
@Override
public Object get(String cacheName, Object key) {
ICache cache = config.getCacheProvider().getCache(cacheName);
if (cache != null) {
return cache.get(key);
}
return null;
}
@Override
public Object get(Object key) {
return get(config.getDefaultCacheName(), key);
}
@Override
public Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy