java 更新缓存_Java 缓存机制

这个博客介绍了Java中实现的一个配置缓存类`ConfigCache`,它使用`ConcurrentHashMap`作为缓存容器,并通过`@Component`注解进行Spring管理。类中包含了缓存的更新逻辑,当超过预设的超时时间`CACHE_TIMEOUT`时,会同步刷新缓存,确保配置的时效性。此外,还实现了检查超时和更新门闩机制,保证了并发环境下的安全性。
摘要由CSDN通过智能技术生成

importjava.util.Collection;importjava.util.Collections;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.concurrent.ConcurrentHashMap;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importnet.blogjava.frankiegao123.log.slf4j.Log;importnet.blogjava.frankiegao123.log.slf4j.LogFactory;/***

System.Config 配置缓存

*

*@authorfrankiegao123

* 2010-6-10 下午02:48:35*/@Component("configCache")public class ConfigCache implementsConfigService {private final static Log log = LogFactory.getLog(ConfigCache.class);/*** 更新缓存时记录的时间*/

private volatile long time = 0L;/*** 正在更新缓存时的门闩,为 true 时表示当前没有更新缓存,为 true 时表示当前正在更新缓存*/

private volatile boolean updateGate = true;/*** 缓存容器*/

private Map cache = new ConcurrentHashMap();privateCommonDao commonDao;

@AutowiredpublicConfigCache(CommonDao commonDao) {this.commonDao =commonDao;

log.info("initializing cache...");

refreshCache();

time=System.currentTimeMillis();

log.info("initialized cache finished, cache size: {}, set cache time to current: {}, cache timeout: {}ms", cache.size(), time, ConfigConstant.CACHE_TIMEOUT);

}/***

根据配置的键名获取配置值

*

*@paramconfigKey

*@return*@authorfrankiegao123

* 2010-6-10 上午11:18:33*/

publicSysConfig getSysConfig(String configKey) {long current =System.currentTimeMillis();if(updateGate &&isTimeout(current)) {synchronized (this) {if(updateGate) {

timeoutSynRefresh(current);

}

}

}returncache.get(configKey);

}/***

超时时更新缓存。该方法需要在同步环境中调用

*@paramcurrent

*@authorfrankiegao123

* 2010-6-10 上午11:16:30*/

private void timeoutSynRefresh(longcurrent) {

updateGate= false;

log.info("refresh cache start..., time out: {}, size: {}, set updateGate to false", (current - time) / 1000.0, cache.size());try{

refreshCache();

time=current;

log.info("refresh cache finished, size after update: {}, set cache time to current: {}", cache.size(), String.valueOf(time));

}catch(Exception e) {

log.error("refresh cache failed", e);

}finally{

updateGate= true;

log.info("refresh cache finished, set updateGate to true");

}

}/***

更新缓存数据

*

*@authorfrankiegao123

* 2010-6-10 上午11:15:55*/

private voidrefreshCache() {

List configs =commonDao.getSysConfigs();for(Iterator i =configs.iterator(); i.hasNext(); ) {

SysConfig config=i.next();

cache.put(config.getKey(), config);

}

commonDao.clear();

SysConfig config=cache.get(SysConfig.TEST_KEY);if(config == null) {

log.error("refresh cache, cannot find TEST_KEY");

}else{

log.info("refresh cache, find TEST_KEY = [{}]", config.getValue());

}

}/***

缓存是否超时

*

*@paramcurrent

*@return*@authorfrankiegao123

* 2010-6-10 上午11:16:12*/

private boolean isTimeout(longcurrent) {return (current - time >=ConfigConstant.CACHE_TIMEOUT);

}

CollectiongetSysConfigs() {returnCollections.unmodifiableCollection(cache.values());

}intgetSize() {returncache.size();

}longgetTime() {returntime;

}booleanisUpdateGate() {returnupdateGate;

}voidrefresh() {

time= 0L;

log.info("refresh: reset cache time to 0");

getSysConfig("none");

log.info("refresh: refresh cache finished, cache: {0}", String.valueOf(time));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值