net.ymate.platform.cache.AbstractCacheProvider 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.commons.ReentrantLockHelper;
import net.ymate.platform.commons.util.RuntimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author 刘镇 ([email protected]) on 2018/11/8 11:43 PM
*/
public abstract class AbstractCacheProvider implements ICacheProvider {
private static final Log LOG = LogFactory.getLog(AbstractCacheProvider.class);
private ICaches owner;
private ICacheManager cacheManager;
private boolean initialized;
private final Map caches = new ConcurrentHashMap<>();
/**
* 初始化
*
* @throws Exception 可能产生的任何异常
*/
protected void onInitialize() throws Exception {
cacheManager = getOwner().getConfig().getCacheManager();
}
/**
* 销毁
*
* @throws Exception 可能产生的任何异常
*/
protected void onDestroy() throws Exception {
}
protected ICacheManager getCacheManager() {
return cacheManager;
}
@Override
public void initialize(ICaches owner) throws Exception {
if (!initialized) {
this.owner = owner;
//
onInitialize();
//
initialized = true;
}
}
@Override
public boolean isInitialized() {
return initialized;
}
@Override
public void close() throws Exception {
if (initialized) {
initialized = false;
//
onDestroy();
//
Iterator> cacheIt = caches.entrySet().iterator();
while (cacheIt.hasNext()) {
ICache cache = cacheIt.next().getValue();
cache.close();
cacheIt.remove();
}
cacheManager = null;
}
}
@Override
public ICaches getOwner() {
return owner;
}
@Override
public ICache createCache(String name, final ICacheEventListener listener) {
try {
final String cacheName = cacheManager.cacheNameSafety(name);
return ReentrantLockHelper.putIfAbsentAsync(caches, cacheName, () -> onCreateCache(cacheName, listener));
} catch (Exception e) {
if (LOG.isWarnEnabled()) {
LOG.warn(StringUtils.EMPTY, RuntimeUtils.unwrapThrow(e));
}
}
return null;
}
/**
* 创建缓存对象,若已存在则直接返回
*
* @param cacheName 缓存名称
* @param listener 缓存元素过期监听器接口实现
* @return 返回缓存对象
*/
protected abstract ICache onCreateCache(String cacheName, ICacheEventListener listener);
@Override
public ICache getCache(String name) {
return getCache(name, true);
}
@Override
public ICache getCache(String name, boolean create) {
return getCache(name, create, getOwner().getConfig().getCacheEventListener());
}
@Override
public ICache getCache(String name, boolean create, ICacheEventListener listener) {
try {
final String cacheName = cacheManager.cacheNameSafety(name);
return ReentrantLockHelper.putIfAbsentAsync(caches, cacheName, () -> create ? onCreateCache(cacheName, listener) : null);
} catch (Exception e) {
if (LOG.isWarnEnabled()) {
LOG.warn(StringUtils.EMPTY, RuntimeUtils.unwrapThrow(e));
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy