PlanScheduleRedisService.java
6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.bsth.redis;
import com.bsth.Application;
import com.bsth.entity.SchedulePlanInfo;
import com.bsth.redis.util.DateUtils;
import com.bsth.redis.util.RedisUtils;
import com.bsth.repository.SchedulePlanInfoRepository;
import com.bsth.server_rs.base_info.line.Line;
import com.bsth.server_rs.base_info.line.buffer.LineBufferData;
import com.bsth.util.ConfigUtil;
import com.bsth.util.ConvertUtil;
import com.google.common.collect.ArrayListMultimap;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 计调的 计划排班redis缓存
* Created by panzhao on 2017/3/27.
*/
@Service
@Order(6)
public class PlanScheduleRedisService implements CommandLineRunner {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
SchedulePlanInfoRepository planInfoRepository;
@Autowired
RedisUtils redisUtils;
static Logger logger = LoggerFactory.getLogger(PlanScheduleRedisService.class);
private final static String REDIS_KEY_PREFIX = "plan:";
/**
* 将一批计划写入redis
*
* @param list
*/
public void wirte(List<SchedulePlanInfo> list) {
ArrayListMultimap<String, SchedulePlanInfo> multimap;
try {
if (list.size() == 0)
return;
//按日期和线路分组数据
Class clazz = SchedulePlanInfo.class;
multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("xlBm"), clazz.getDeclaredField("scheduleDate"));
//写入redis
Iterator<String> iterator = multimap.keySet().iterator();
String key;
while (iterator.hasNext()) {
key = iterator.next();
replace(key, multimap.get(key));
}
} catch (Exception e) {
logger.error("", e);
}
}
/**
* 将 list 与redis里的数据合并
*
* @param key
* @param list
*/
public void mergeData(String key, List<SchedulePlanInfo> list) {
key = REDIS_KEY_PREFIX + key.replaceAll("-", "");
ListOperations<String, SchedulePlanInfo> ops = redisTemplate.opsForList();
List<SchedulePlanInfo> cacheList = ops.range(key, 0, -1);
for (SchedulePlanInfo plan : cacheList) {
if (!list.contains(plan))
list.add(plan);
}
//更新
redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
}
/**
* 覆盖数据
*
* @param key
* @param list
*/
public void replace(String key, List<SchedulePlanInfo> list) {
key = REDIS_KEY_PREFIX + key.replaceAll("-", "");
redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
}
/**
* 根据日期和线路编码从redis获取计划
*
* @param dateStr
* @param lineCode
* @return
*/
public List<SchedulePlanInfo> read(String dateStr, String lineCode) {
return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1);
}
@Autowired
PlanClearThread planClearThread;
@Override
public void run(String... strings) throws Exception {
Application.mainServices.schedule(new Runnable() {
@Override
public void run() {
int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
//设置key 序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
DateTime dt = new DateTime();
dt = dt.minusDays(cacheDays);
dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays);
List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(dt.toDate(), new Date());
//写入redis
wirte(list);
}
}, 60 * 5, TimeUnit.SECONDS);
//定时 00:05 分清理计划,并加载当天的计划
long diff = (DateUtils.getTimestamp() + 1000 * 60 * 5) - System.currentTimeMillis();
if (diff < 0)
diff += (1000 * 60 * 60 * 24);
Application.mainServices.scheduleAtFixedRate(planClearThread, diff / 1000, 60 * 60 * 24, TimeUnit.SECONDS);
}
public List<SchedulePlanInfo> findByMultiLine(List<String> lineArray, String rq) {
rq = rq.replaceAll("-", "");
List<SchedulePlanInfo> rs = new ArrayList<>();
for (String lineCode : lineArray) {
rs.addAll(read(rq, lineCode));
}
return rs;
}
@Component
public static class PlanClearThread extends Thread {
@Autowired
PlanScheduleRedisService planRedisService;
@Autowired
SchedulePlanInfoRepository planInfoRepository;
@Override
public void run() {
try {
logger.info("redis -清理计划排班");
int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
DateTime dt = new DateTime();
dt = dt.minusDays(cacheDays);
String rq = dt.toString("yyyy-MM-dd");
List<Line> lines = LineBufferData.findAll();
for (Line line : lines) {
planRedisService.delete(line.getLineCode(), rq);
}
//加载当天的计划
Date d = new Date();
Date s = new Date(d.getTime() - 1000 * 60 * 60 * 24);
List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(s, d);
//写入redis
planRedisService.wirte(list);
} catch (Exception e) {
logger.error("", e);
}
}
}
private void delete(String lineCode, String rq) {
String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", "");
redisTemplate.delete(key);
}
}