import
java.util.Collection;
import
java.util.Collections;
import
java.util.Iterator;
import
java.util.List;
import
java.util.Map;
import
java.util.concurrent.ConcurrentHashMap;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Component;
import
net.blogjava.frankiegao123.log.slf4j.Log;
import
net.blogjava.frankiegao123.log.slf4j.LogFactory;
/**
* <p>System.Config 配置缓存</p>
*
* @author frankiegao123
* 2010-6-10 下午02:48:35
*/
@Component
(
"configCache"
)
public
class
ConfigCache
implements
ConfigService {
private
final
static
Log log = LogFactory.getLog(ConfigCache.
class
);
/**
* 更新缓存时记录的时间
*/
private
volatile
long
time = 0L;
/**
* 正在更新缓存时的门闩,为 true 时表示当前没有更新缓存,为 true 时表示当前正在更新缓存
*/
private
volatile
boolean
updateGate =
true
;
/**
* 缓存容器
*/
private
Map<String, SysConfig> cache =
new
ConcurrentHashMap<String, SysConfig>();
private
CommonDao commonDao;
@Autowired
public
ConfigCache(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);
}
/**
* <p>根据配置的键名获取配置值</p>
*
* @param configKey
* @return
* @author frankiegao123
* 2010-6-10 上午11:18:33
*/
public
SysConfig getSysConfig(String configKey) {
long
current = System.currentTimeMillis();
if
(updateGate && isTimeout(current)) {
synchronized
(
this
) {
if
(updateGate) {
timeoutSynRefresh(current);
}
}
}
return
cache.get(configKey);
}
/**
* <p>超时时更新缓存。该方法需要在同步环境中调用</p>
* @param current
* @author frankiegao123
* 2010-6-10 上午11:16:30
*/
private
void
timeoutSynRefresh(
long
current) {
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"
);
}
}
/**
* <p>更新缓存数据</p>
*
* @author frankiegao123
* 2010-6-10 上午11:15:55
*/
private
void
refreshCache() {
List<SysConfig> configs = commonDao.getSysConfigs();
for
(Iterator<SysConfig> 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());
}
}
/**
* <p>缓存是否超时</p>
*
* @param current
* @return
* @author frankiegao123
* 2010-6-10 上午11:16:12
*/
private
boolean
isTimeout(
long
current) {
return
(current - time >= ConfigConstant.CACHE_TIMEOUT);
}
Collection<SysConfig> getSysConfigs() {
return
Collections.unmodifiableCollection(cache.values());
}
int
getSize() {
return
cache.size();
}
long
getTime() {
return
time;
}
boolean
isUpdateGate() {
return
updateGate;
}
void
refresh() {
time = 0L;
log.info(
"refresh: reset cache time to 0"
);
getSysConfig(
"none"
);
log.info(
"refresh: refresh cache finished, cache: {0}"
, String.valueOf(time));
}
}
相关推荐
java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例
自己实现的Java缓存技术的实现实例代码,部署可以直接运行。。。
java缓存插件ehcache的应用实例
EhCache EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 主要的特性有: 1. 快速. 2. 简单. 3. 多种缓存策略 4. 缓存数据有两级:内存和磁盘,因此无需担心容量...
Cache 缓存类存放缓存id 缓存更新时间 缓存资源(object)缓存是否可用 CacheManager 缓存管理类有缓存各种操作的方法 以上两个为主要类 下面为测试类 TestCache main方法测试缓存 TestBo 需要存入缓存的资源
java ehcache分布式缓存实例 RMI方式实现.
基于Java的实例源码-Java缓存工具 SimpleCache.zip
基于Java的实例开发源码-Java缓存工具 SimpleCache.zip
基于Java的实例源码-ehcache(Java缓存框架 EhCache).zip
基于Java的实例开发源码-ehcache(Java缓存框架 EhCache).zip
介绍了memcached缓存开源框架在windows下的安装,以及如何用java访问缓存server,并结合原理和代码讲解。代码测过可用。
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 下图是 Ehcache 在应用程序中的位置: 主要的特性有: 1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:...
redis缓存java类使用实例,针对map.list,set等使用详细的实例!
java缓存(cache),详细的文档,大量的实例
该压缩包中有一个WORD和一个PPT,WORD中介绍了通过单实例和简单LRU算法实现缓存。PPT中介绍了缓存体系,JVM内存模型,JCONSOLE监控工具的使用,Oscache缓存架构 Ehcache缓存架构 Memcached缓存架构 JiveCache缓存架构...
Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...
java缓存实现demo完整实例,很不错的资源,欢迎大家来下载学习。/** * 此函数接受一个对象列表,数目不定,opration:表是触发的事件 * eg:change;fnClear:表示初始化下拉框。var_args表示多个下拉框... */ ...
本篇文章主要介绍了Java实现简单的缓存方法实例,需要的朋友可以参考下
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。主要的特性有:1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题5. 缓存...
* 文件名:CacheManager.java 管理缓存 * 版本信息:V1.0 * 日期:2013-06-18 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com * */ @SuppressWarnings("all") public class ...