lodsve.cache.oscache.OscacheCacheManager Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package lodsve.cache.oscache;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
import lodsve.cache.properties.CacheProperties.Oscahce.CacheConfig;
import org.springframework.cache.Cache;
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Oscache CacheManager.
*
* @author sunhao(sunhao.java @ gmail.com)
* @version V1.0, 2018-1-10-0010 16:26
*/
public class OscacheCacheManager extends AbstractTransactionSupportingCacheManager {
private GeneralCacheAdministrator admin;
private List cacheConfigs;
private ConcurrentMap cacheMap = new ConcurrentHashMap<>();
@Override
public void afterPropertiesSet() {
if (!CollectionUtils.isEmpty(cacheConfigs)) {
for (CacheConfig config : cacheConfigs) {
String name = config.getName();
int expire = config.getExpire();
cacheMap.putIfAbsent(config.getName(), new OscacheCache(name, expire, admin));
}
}
super.afterPropertiesSet();
}
@Override
protected Collection extends Cache> loadCaches() {
return cacheMap.values();
}
@Override
public Cache getCache(String name) {
return cacheMap.get(name);
}
public void setAdmin(GeneralCacheAdministrator admin) {
this.admin = admin;
}
public void setCacheConfigs(List cacheConfigs) {
this.cacheConfigs = cacheConfigs;
}
}