事情是这样的,一篇文章,需要统计每天的阅读量。当日第一次阅读时,save();之后就是update();很简单的逻辑,代码如下:
public void set(String blogId) {
Date now = new Date() ;
String date = new SimpleDateFormat("yyyy-MM-dd").format(now);
TodayReaders readTimes = this.get(blogId, date);
if (null != readTimes) {
readTimes.setReadTimes(readTimes.getReadTimes() + 1 );
readTimes.update();
}else{
readTimes = new TodayReaders() ;
readTimes.setId(getIds());
readTimes.setReadTimes(1) ;
readTimes.setBlogId(blogId);
readTimes.setDate(now);
readTimes.save();
}
}但是……正式环境里面,居然就有一篇文章被存储了两条数据。请问遇到这样的情况该如何使用jfinal完美的解决?
insert into table(f1, fn) VALUES(?,?) on duplicate key update count=count+1;
然后 Db.update(sql) 打完收工
通常这类功能,会做成只去操作缓存,然后再启动一个线程定时将缓存中的数据写入数据库,同时清空缓存,这样可以避免经常性写库操作,尤其对高并发有帮助