ScheduleRedisService.java
5.72 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
package com.bsth.redis;
import com.bsth.entity.ScheduleRealInfo;
import com.bsth.repository.ScheduleRealInfoRepository;
import com.bsth.util.ConfigUtil;
import com.bsth.util.ConvertUtil;
import com.bsth.ws_proxy.WebServiceProxy;
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.Service;
import java.util.Iterator;
import java.util.List;
/**
* 班次 redis 缓存管理
* Created by panzhao on 2017/3/13.
*/
@Service
@Order(2)
public class ScheduleRedisService implements CommandLineRunner {
@Autowired
ScheduleRealInfoRepository scheduleRealInfoRepository;
private final static String REDIS_KEY_PREFIX = "schedule:";
@Autowired
private RedisTemplate redisTemplate;
@Autowired
WebServiceProxy webServiceProxy;
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 将一批班次写入redis
*
* @param list
*/
public void wirte(List<ScheduleRealInfo> list) {
ArrayListMultimap<String, ScheduleRealInfo> multimap;
try {
if (list.size() == 0)
return;
//按日期和线路分组数据
Class clazz = ScheduleRealInfo.class;
multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("xlBm"), clazz.getDeclaredField("scheduleDateStr"));
//写入redis
Iterator<String> iterator = multimap.keySet().iterator();
String key;
while (iterator.hasNext()) {
key = iterator.next();
mergeData(key, multimap.get(key));
}
} catch (Exception e) {
logger.error("", e);
}
}
/**
* 将 list 与redis里的数据合并
*
* @param key
* @param list
*/
public void mergeData(String key, List<ScheduleRealInfo> list) {
key = REDIS_KEY_PREFIX + key.replaceAll("-", "");
ListOperations<String, ScheduleRealInfo> ops = redisTemplate.opsForList();
List<ScheduleRealInfo> cacheList = ops.range(key, 0, -1);
for (ScheduleRealInfo sch : cacheList) {
if (!list.contains(sch))
list.add(sch);
}
//删除redis数据
redisTemplate.delete(key);
//重新写入
ops.leftPushAll(key, list);
}
/**
* 根据日期和线路编码从redis获取班次
*
* @param dateStr
* @param lineCode
* @return
*/
public List<ScheduleRealInfo> read(String dateStr, String lineCode) {
return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1);
}
/**
* 返回指定日期,公司的实际排班,并按线路_车辆分组
* (新老系统并行时调用这个函数)
* @param rq
* @param companyId
* @return
*/
public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm_bingxing(String rq, String companyId){
List<String> lineArray = webServiceProxy.findLinesByCompany_new(companyId);
ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
rq = rq.replaceAll("-", "");
List<ScheduleRealInfo> list;
for(String lineCode : lineArray){
list = read(rq, lineCode);
for(ScheduleRealInfo sch : list){
rs.put(sch.getXlBm() + "_" + sch.getClZbh(), sch);
}
}
return rs;
}
/**
* 返回指定日期,公司的实际排班,并按线路_车辆分组
* @param rq
* @param companyId
* @return
*/
public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm(String rq, String companyId){
List<String> lineArray = webServiceProxy.findLinesByCompany(companyId);
ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
rq = rq.replaceAll("-", "");
List<ScheduleRealInfo> list;
for(String lineCode : lineArray){
list = read(rq, lineCode);
for(ScheduleRealInfo sch : list){
rs.put(sch.getXlBm() + "_" + sch.getClZbh(), sch);
}
}
return rs;
}
/**
* 返回指定日期,公司的实际排班,并按线路分组
* @param rq
* @param companyId
* @return
*/
public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByLine(String rq, String companyId){
List<String> lineArray = webServiceProxy.findLinesByCompany(companyId);
ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
rq = rq.replaceAll("-", "");
List<ScheduleRealInfo> list;
for(String lineCode : lineArray){
list = read(rq, lineCode);
for(ScheduleRealInfo sch : list){
rs.put(sch.getXlBm(), sch);
}
}
return rs;
}
@Override
public void run(String... strings) throws Exception {
int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
//设置key 序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
DateTime dt = new DateTime();
dt = dt.minusDays(cacheDays);
List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd"));
//写入redis
wirte(list);
}
}