添加首页缓存

dev
lisai17@sina.com 2021-09-24 14:27:27 +08:00
parent 80e19b32ad
commit 74b0e14b13
5 changed files with 57 additions and 8 deletions

View File

@ -0,0 +1,43 @@
package com.cowr.service.ssjygl.jobs;
import com.cowr.service.ssjygl.overall.OverallCacheService;
import com.cowr.ssjygl.overall.OverallService;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Redis;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.List;
public class LoadStatCacheJob implements Job {
private static Log log = Log.getLog(LoadStatCacheJob.class);
public void load() {
try {
String statkey = OverallCacheService.me.overallmapkey("stat");
Record statcachedata = Redis.use().get(statkey);
if (statcachedata == null) {
statcachedata = OverallCacheService.me.stat(null);
Redis.use().setex(statkey, OverallCacheService.statExpireAt, statcachedata);
}
String lastOrderkey = OverallCacheService.me.overallmapkey("lastOrder");
List<Record> lastOrdercachedata = Redis.use().get(lastOrderkey);
if (lastOrdercachedata == null) {
lastOrdercachedata = OverallService.me.lastOrder(null);
Redis.use().setex(lastOrderkey, OverallCacheService.statExpireAt, lastOrdercachedata);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
load();
}
}

View File

@ -22,6 +22,7 @@ import com.cowr.service.ssjygl.driver.DriverController;
import com.cowr.service.ssjygl.invoice.invalidverify.InvoiceInvalidVerifyController;
import com.cowr.service.ssjygl.invoice.log.InvoiceLogController;
import com.cowr.service.ssjygl.invoice.receive.InvoiceReceiveController;
import com.cowr.service.ssjygl.jobs.LoadStatCacheJob;
import com.cowr.service.ssjygl.netty.NettyServer;
import com.cowr.service.ssjygl.order.invalidverify.OrderInvalidVerifyController;
import com.cowr.service.ssjygl.order.ordercluster.OrderclusterController;
@ -318,6 +319,8 @@ public class Config extends JFinalConfig {
}
SyncTaskService.me.start();
new LoadStatCacheJob().load();
} catch (Exception e) {
log.error(e.getMessage(), e);
}

View File

@ -4,6 +4,7 @@ import com.cowr.common.Const;
import com.cowr.service.ssjygl.main.Config;
public class OverallCacheService extends com.cowr.ssjygl.overall.OverallService {
public static final int statExpireAt = 1800; // 超时时间,单位秒
public static OverallCacheService me = new OverallCacheService();
public static final String tablename = "overall";

View File

@ -10,7 +10,6 @@ import com.jfinal.plugin.redis.Redis;
import java.util.List;
public class OverallController extends Controller {
private static final int statExpireAt = 600; // 超时时间,单位秒
public void stat() {
String key = OverallCacheService.me.overallmapkey("stat");
@ -18,7 +17,7 @@ public class OverallController extends Controller {
if (cachedata == null) {
cachedata = OverallCacheService.me.stat(null);
Redis.use().setex(key, statExpireAt, cachedata);
Redis.use().setex(key, OverallCacheService.statExpireAt, cachedata);
}
renderJson(Result.success(cachedata));
@ -45,14 +44,12 @@ public class OverallController extends Controller {
}
public void lastOrder() {
Integer supermarket_id = getInt("supermarket_id");
String key = OverallCacheService.me.overallmapkey("lastOrder_" + supermarket_id);
String key = OverallCacheService.me.overallmapkey("lastOrder");
List<Record> cachedata = Redis.use().get(key);
if (cachedata == null) {
cachedata = OverallService.me.lastOrder(supermarket_id);
cachedata = OverallService.me.lastOrder(null);
Redis.use().setex(key, statExpireAt, cachedata);
Redis.use().setex(key, OverallCacheService.statExpireAt, cachedata);
}
renderJson(Result.success(cachedata));

View File

@ -27,4 +27,9 @@ statsms.enable=true
# 检查异常数据
checkexceptiondata.job=com.cowr.service.ssjygl.jobs.CheckExceptionDataJob
checkexceptiondata.cron= 0 3 * * * ?
checkexceptiondata.enable=true
checkexceptiondata.enable=true
# 加载首页缓存数据
loadstat.job=com.cowr.service.ssjygl.jobs.LoadStatCacheJob
loadstat.cron= 0 */30 * * * ?
loadstat.enable=true