⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 countserviceimpl.java

📁 这是一款最新的野蔷薇论坛源码,有需要的朋友可以尽情下载
💻 JAVA
字号:
/* 
 * Created on 2007-5-13
 * Last modified on 2007-6-12
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.service.counter;

import java.util.List;

import org.apache.log4j.Logger;

import com.yeqiangwei.club.cache.Cache;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.cache.singleton.CacheFactory;
import com.yeqiangwei.club.dao.CountersDAO;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.model.Counter;
import com.yeqiangwei.club.dao.model.Counters;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.CounterParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.model.CountersModel;
import com.yeqiangwei.club.service.util.CounterService;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.TypeChange;
import com.yeqiangwei.util.Validator;

public class CountServiceImpl implements CountService{
	
	private static final Logger logger = Logger.getLogger(CountServiceImpl.class);
	
	private Cache cache = CacheFactory.creator(CacheRegion.COUNT);
	
	private static final int CACHE_TIMEOUT = 300; //缓存超时後更新到数据库(秒数)
	
	public void doCount(CountersModel model) throws ClubException {
		int ymd = FormatDateTime.formatDateTimeToInt("yyyyMMdd");
		CounterParameter param = new CounterParameter();
		param.setForumId(model.getForumId());
		param.setYmd(ymd);
		
		CountersModel m = this.findOnlyByParameter(param);
		m.setYmd(ymd);
		m.setBoys(m.getBoys()+model.getBoys());
		m.setGirls(m.getGirls()+model.getGirls());
		m.setOthers(m.getOthers()+model.getOthers());
		m.setHits(m.getHits()+model.getHits());
		if(model.getMostOnline()>m.getMostOnline()){
			m.setMostOnline(model.getMostOnline());
		}
		m.setReplys(m.getReplys()+model.getReplys());
		m.setTopics(m.getTopics()+model.getTopics());
		
		cache.put(cacheKey(model.getForumId()),m); //更新缓存
		//logger.debug("update cache of "+cacheKey(model.getForumId()));
		long l = TypeChange.stringToLong(FormatDateTime.dateAdd(FormatDateTime.SECOND,CACHE_TIMEOUT,"yyyyMMddHHmmss"));
		if(Validator.isEmpty(cache.get(cacheTimeKey(param.getForumId())))){
			/*
			 * 设置缓存有效期至
			 */
			cache.put(cacheTimeKey(param.getForumId()),new Long(l));
		}
		Long cacheTime = (Long) cache.get(cacheTimeKey(param.getForumId()));
		Long nowTime = new Long(FormatDateTime.now());
		if(cacheTime<nowTime){
			logger.debug("nowTime:"+nowTime.longValue());
			logger.debug(cacheTimeKey(param.getForumId())+" cacheTime:"+cacheTime.longValue());
			m = this.createOrUpdate(m); //更新版面统计
			this.doCounter(model); //更新全局统计
			if(!Validator.isEmpty(m)){
				cache.put(cacheTimeKey(m.getForumId()),new Long(l));
			}else{
				cache.remove(cacheKey(m.getForumId()));
				cache.remove(cacheTimeKey(m.getForumId()));
				logger.debug("error update Counters to database");
			}
		}
	}
	
	/**
	 * 更新全局统计
	 * @param model
	 * @throws ClubException 
	 */
	private void doCounter(CountersModel model) throws ClubException{
		//logger.debug("update Counter to database");
		Counter item = this.getCounterService().findOnly();
		if(Validator.isEmpty(item)){
			item = new Counter();
		}
		item.setBoys(item.getBoys()+model.getBoys());
		item.setGirls(item.getGirls()+model.getGirls());
		item.setHits(item.getHits()+model.getHits());
		if(model.getMostOnline()>item.getMostOnline()){
			item.setMostOnline(model.getMostOnline());
		}
		item.setReplys(item.getReplys()+model.getReplys());
		item.setTopics(item.getTopics()+model.getTopics());
		this.getCounterService().createOrUpdate(item);
		if(!Validator.isEmpty(item)){
			BeanUtils.copyProperties(model,item);
			cache.put(cacheKey(0),model); //更新缓存
		}
	}

	public CountersModel findById(int id) {
		return null;
	}

	public CountersModel createOrUpdate(CountersModel model) {
		logger.debug("update cache of "+cacheKey(model.getForumId())+" to database");
		if(model.getCountersId()>0){
			model = this.update(model);
		}else{
			model = this.create(model);
		}
		return model;
	}

	public CountersModel create(CountersModel model) {
		Counters item = new Counters();
		BeanUtils.copyProperties(item,model);
		item =  this.getCountersDAO().create(item);
		if(!Validator.isEmpty(item)){
			model.setCountersId(item.getCountersId());
		}else{
			model = null;
		}
		return model;
	}

	public CountersModel update(CountersModel model) {
		Counters item = this.getCountersDAO().findById(model.getCountersId());
		BeanUtils.copyProperties(item,model);
		item =  this.getCountersDAO().update(item);
		if(!Validator.isEmpty(item)){
			model.setCountersId(item.getCountersId());
		}else{
			model = null;
		}
		return model;
	}
	
	

	public CountersModel findOnlyPublic() {
		CountersModel m = (CountersModel) cache.get(cacheKey(0));
		if(Validator.isEmpty(m)){
			Counter item = this.getCounterService().findOnly();
			m = new CountersModel();
			BeanUtils.copyProperties(m,item);
			cache.put(cacheKey(0),m);
		}
		return m;
	}


	public int delete(CountersModel model) {
		return 0;
	}

	public List<CountersModel> findByParameter(CounterParameter param) {
		//Counters item = this.getCountersDAO().findOnlyByParameter(param);
		return null;
	}

	public long countByParameter(CounterParameter param) {
		return 0;
	}

	public CountersModel findOnlyByParameter(CounterParameter param) {
		CountersModel model = (CountersModel) cache.get(cacheKey(param.getForumId()));
		if(Validator.isEmpty(model)){
			Counters item = this.getCountersDAO().findOnlyByParameter(param);
			if(Validator.isEmpty(item)){
				model = new CountersModel();
				model.setForumId(param.getForumId());
				cache.remove(cacheKey(param.getForumId()));
				cache.remove(cacheTimeKey(param.getForumId()));
				return model;
			}else{
				model = new CountersModel();
				BeanUtils.copyProperties(model,item);
				cache.put(cacheKey(model.getForumId()),model);
				cache.put(cacheTimeKey(param.getForumId()),new Long(FormatDateTime.now()));
				return model;
			}
		}else{
			if(model.getYmd()!=param.getYmd()){
				logger.debug("create new cache for new day");
				model = new CountersModel();
				model.setForumId(param.getForumId());
				cache.remove(cacheKey(param.getForumId()));
				cache.remove(cacheTimeKey(param.getForumId()));
			}
			return model;
		}
	}
	
	private String cacheKey(int forumId){
		return "CountersModel:"+forumId;
	}
	
	private String cacheTimeKey(int forumId){
		return "cacheime:"+forumId;
	}
	
	/*
	private CounterDAO getCounterDAO(){
		return DAOWrapper.<CounterDAO>getSingletonInstance(DAOLocator.COUNTER);
	}
	*/
	
	private CounterService getCounterService(){
		return ServiceWrapper.<CounterService>getSingletonInstance(ServiceLocator.COUNTER);
	}
	
	private CountersDAO getCountersDAO(){
		return DAOWrapper.<CountersDAO>getSingletonInstance(DAOLocator.COUNTERS);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -