ScheduleRedisService.java
3.4 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
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.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;
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().groupList(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);
}
@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);
}
}