监听数据库中的缓存配置以更新内存中的缓存数据

缓存监听器接口,用于启动和停止监听器

package com.zhenyi.common.cache.thread;

 
public interface CacheMonitor {
	
	public void start();
	public void stop();

}
缓存监听器实现,实现中 List<ConfigCache> cacheManagers包括多个缓存管理器ConfigCache,此缓存监听器每隔一段时间调用各个ConfigCache的refresh()方法去执行ConfigCache对应的缓存数据刷新

package com.zhenyi.common.cache.thread;

import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhenyi.common.cache.manage.ConfigCache;


@Service("cacheMonitorImpl")
public class CacheMonitorImpl implements CacheMonitor {
	
	private static final Logger LOG = LoggerFactory.getLogger(CacheMonitorImpl.class);
	protected volatile boolean stop = false;
	protected int period = 100000;
	
	@Autowired
	private List<ConfigCache> cacheManagers;

	@PostConstruct
	public void start() {
		if(LOG.isDebugEnabled()){
			LOG.debug(":::缓存监控启动:::");
		}
		init();
		Thread monitor = new MonitorThread();
		monitor.start();
	}

	@Override
	public void stop() {
		stop = true;
		if(LOG.isDebugEnabled()){
			LOG.debug(":::缓存监控停止:::");
		}
	}
	
	public class MonitorThread extends Thread {
		@Override
		public void run() {
			while (!stop) {
				try {
					for(ConfigCache manager : cacheManagers){
						manager.refresh();
					}
				} catch (Exception e1) {
					LOG.error("缓存监控错误:", e1);
				}
				try {
					Thread.sleep(period);
				} catch (InterruptedException e) {
					LOG.error("休眠错误:", e);
				}
			}
		}
	}
	
	protected void init(){
		for(ConfigCache manager : cacheManagers){
			manager.init();
		}
	}

}
ConfigCache接口,其中init()方法用于缓存初始化缓存数据,refresh()方法用于刷新数据

package com.zhenyi.common.cache.manage;

public interface ConfigCache {
	
	public void refresh();
	public void init();

}

ConfigCache接口的一个实现,其中CACHE_DOMAIN为数据的缓存域,例如数据库所有CACHE_DOMAIN为static的缓存配置都属于此ConfigCache实现。此ConfigCache实现的init方法用于执行初始化操作,例如初始化缓存数据,初始化缓存配置(ConfigCache)。refresh方法用于刷新内存中的缓存数据。刷新机制为每次执行refresh方法时查询CACHE_DOMAIN为static的ConfigCache配置记录,得到ConfigCache的一个列表,然后循环此列表,对比conCacheMap中对应的ConfigCache,如果ConfigCache的时间戳(VERSION)发生变化(例如使用后台管理系统对此ConfigCache对应的数据库中的ConfigCache配置记录的VERSION进行更改),根据ConfigCache的类型(CACHE_CODE),执行ConfigCache对应的缓存数据刷新。最后更新所有的ConfigCache配置,以得到最新的时间戳。此ConfigCache的一个数据缓存存在于GAME_DETAIL_MAP中

ConfigCache接口的一个实现:

package com.zhenyi.common.cache.manage;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhenyi.model.ConCache;
import com.zhenyi.model.SceeGame;
import com.zhenyi.model.SceeGameDetail;
import com.zhenyi.model.enumType.ConCacheCodeEnum;
import com.zhenyi.model.persistence.ConCacheMapper;
import com.zhenyi.model.persistence.SceeGameDetailMapper;
import com.zhenyi.model.persistence.SceeGameMapper;

@Service("configCacheImpl")
public class ConfigCacheImpl implements ConfigCache {

	private static final Logger LOG = LoggerFactory.getLogger(ConfigCacheImpl.class);
	private static final String CACHE_DOMAIN = "static";
	
	private Map<SceeGame,List<SceeGameDetail>> GAME_DETAIL_MAP;
	
	@Autowired
	private SceeGameMapper sceeGameMapper;
	
	@Autowired
	private SceeGameDetailMapper sceeGameDetailMapper;
	
	@Autowired
	private ConCacheMapper conCacheMapper;
	
	private Map<String,ConCache> conCacheMap = new ConcurrentHashMap<String,ConCache>();
	
	@Override
	public void refresh() {

		try {

			if (LOG.isDebugEnabled()) {
				LOG.debug("refresh -> CACHE_DOMAIN:{}", CACHE_DOMAIN);
			}

			List<ConCache> conCacheList = conCacheMapper.selectByCacheDomain(CACHE_DOMAIN);

			if (LOG.isDebugEnabled()) {
				LOG.debug("refresh -> query -> conCacheList:{}", conCacheList);
			}

			for (ConCache lastConCache : conCacheList) {

				ConCache conCache = conCacheMap.get(lastConCache.getCacheCode());

				if (conCache == null || !conCache.getVersion().equals(lastConCache.getVersion())) {

					if (ConCacheCodeEnum.SCEE_GAME.getCode().equals(lastConCache.getCacheCode())) {

						//执行此CACHE_CODE的数据刷新
					}else if(){//执行其它CACHE_CODE的缓存数据刷新}
					
					conCacheMap.put(lastConCache.getCacheCode(), lastConCache);
					
				}
			}

		} catch (Exception e) {
			LOG.error("refresh", e);
		}
	}

	@Override
	public void init() {
		
		List<ConCache> conCacheList = conCacheMapper.selectByCacheDomain(CACHE_DOMAIN);
		for(ConCache conCache:conCacheList){
			conCacheMap.put(conCache.getCacheCode(), conCache);
		}
	}

	public Map<SceeGame, List<SceeGameDetail>> getGAME_DETAIL_MAP() {
		return GAME_DETAIL_MAP;
	}

	public void setGAME_DETAIL_MAP(Map<SceeGame, List<SceeGameDetail>> gAME_DETAIL_MAP) {
		GAME_DETAIL_MAP = gAME_DETAIL_MAP;
	}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值