countserviceimpl.java
来自「社区文章采用的是平板、树形自由选择的两种展示方式」· Java 代码 · 共 247 行
JAVA
247 行
/*
* Created on 2007-5-13
* Last modified on 2008-1-1
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.counter;
import java.util.List;
import org.apache.log4j.Logger;
import com.yeqiangwei.cache.Cache;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.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.model.Counter;
import com.yeqiangwei.club.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.util.CounterService;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
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 static Cache<Counters> CACHE = CacheFactory.<Counters>creator(CacheRegion.COUNT);
private static Cache<Long> CACHE_COUNT = CacheFactory.<Long>creator(CacheRegion.LONG);
private static final int CACHE_TIMEOUT = 600; //缓存超时後更新到数据库(秒数)
public static final Object object = new Object();
public void doCount(Counters model) throws ClubException {
int ymd = FormatDateTime.formatDateTimeToInt("yyyyMMdd");
CounterParameter param = new CounterParameter();
param.setForumId(model.getForumId());
param.setYmd(ymd);
Counters 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_COUNT.get(cacheTimeKey(param.getForumId())))){
/*
* 设置缓存有效期至
*/
CACHE_COUNT.put(cacheTimeKey(param.getForumId()),new Long(l));
}
Long cacheTime = CACHE_COUNT.get(cacheTimeKey(param.getForumId()));
Long nowTime = new Long(FormatDateTime.now());
logger.debug("forumId="+(param.getForumId()));
logger.debug("nowTime<cacheTime="+(cacheTime<nowTime));
if(cacheTime<nowTime){
logger.debug("nowTime="+nowTime.longValue());
logger.debug(cacheTimeKey(param.getForumId())+",cacheTime="+cacheTime.longValue());
this.createOrUpdate(m); //更新版面统计
this.doCounter(model); //更新全局统计
CACHE_COUNT.put(cacheTimeKey(m.getForumId()),new Long(l));
}
}
/**
* 更新全局统计
* @param model
* @throws ClubException
*/
private void doCounter(Counters model) throws ClubException{
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 Counters findById(int id) {
return null;
}
public void createOrUpdate(Counters model) throws ClubException {
logger.debug("update cache of "+cacheKey(model.getForumId())+" to database");
if(model.getCountersId()>0){
this.update(model);
}else{
this.create(model);
}
}
public void create(Counters model) throws ClubException {
synchronized(object){
if(model.getYmd()==0){
throw new ClubException(MessageUtils.getMessage("error_empty"));
}
CounterParameter param = new CounterParameter();
param.setYmd(model.getYmd());
param.setForumId(model.getForumId());
com.yeqiangwei.club.dao.hibernate.ConnectionManager.beginTransaction();
Counters counters = this.getCountersDAO().findOnlyByParameter(param);
logger.debug("findOnlyByParameter:forumId="+model.getForumId()+",ymd="+model.getYmd());
if(counters!=null){
logger.debug("Counters not null");
BeanUtils.copyProperties(model, counters);
throw new ClubException(MessageUtils.getMessage("error_duplicate"));
}else{
logger.debug("Counters is null");
this.getCountersDAO().create(model);
CACHE.put(cacheKey(model.getForumId()),model);
if(model.getCountersId()==0){
throw new ClubException("Counters create error");
}
}
CACHE_COUNT.clear();
com.yeqiangwei.club.dao.hibernate.ConnectionManager.commitTransaction();
}
}
public void update(Counters model) throws ClubException {
if(model.getCountersId()==0){
throw new ClubException(MessageUtils.getMessage("error_update_noid"));
}
else if(model.getForumId()==0||model.getYmd()==0){
throw new ClubException(MessageUtils.getMessage("error_empty"));
}
else {
Counters counters = this.getCountersDAO().findById(model.getCountersId());
if(counters!=null){
BeanUtils.copyProperties(counters, model);
this.getCountersDAO().update(counters);
CACHE.put(cacheKey(model.getForumId()),counters);
}
}
}
public Counters findOnlyPublic() {
Counters counters = CACHE.get(cacheKey(0));
if(Validator.isEmpty(counters)){
Counter counter = this.getCounterService().findOnly();
counters = new Counters();
BeanUtils.copyProperties(counters,counter);
CACHE.put(cacheKey(0),counters);
}
return counters;
}
public int delete(Counters model) throws ClubException {
return this.getCountersDAO().delete(model);
}
public List<Counters> findByParameter(CounterParameter param) {
return this.getCountersDAO().findByParameter(param);
}
public long countByParameter(CounterParameter param) {
return this.getCountersDAO().countByParameter(param);
}
public synchronized Counters findOnlyByParameter(CounterParameter param) {
Counters model = CACHE.get(cacheKey(param.getForumId()));
if(Validator.isEmpty(model)||model.getCountersId()==0){
model = this.getCountersDAO().findOnlyByParameter(param);
if(Validator.isEmpty(model)){
model = new Counters();
model.setForumId(param.getForumId());
model.setYmd(param.getYmd());
try {
this.create(model);
} catch (ClubException e) {
logger.error(e.toString());
}
return model;
}else{
CACHE.put(cacheKey(model.getForumId()),model);
CACHE_COUNT.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 Counters();
model.setForumId(param.getForumId());
model.setYmd(param.getYmd());
try {
this.create(model);
} catch (ClubException e) {
logger.error(e.toString());
}
}
return model;
}
}
private String cacheKey(int forumId){
return "Counters:forumId="+forumId;
}
private String cacheTimeKey(int forumId){
return "CacheTime:forumId="+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 + =
减小字号Ctrl + -
显示快捷键?