OilRedisService.java
3.89 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
package com.bsth.redis;
import com.bsth.entity.OilInfo;
import com.bsth.repository.OilInfoRepository;
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.*;
/**
* 油量数据Redis缓存
* Created by panzhao on 2017/3/19.
*/
@Service
@Order(3)
public class OilRedisService implements CommandLineRunner {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
OilInfoRepository oilInfoRepository;
Logger logger = LoggerFactory.getLogger(this.getClass());
private final static String REDIS_KEY_PREFIX = "oil:";
/**
* 将油量数据写入redis
*
* @param list
*/
public void wirte(List<OilInfo> list) {
ArrayListMultimap<String, OilInfo> multimap;
try {
if (list.size() == 0)
return;
//按日期和线路分组数据
Class clazz = OilInfo.class;
multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq"));
//写入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);
}
}
/**
* 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key
*
* @param nbbmArray
* @param rq
* @return
*/
public Map<String, OilInfo> findByNbbmGroup(Iterable<String> nbbmArray, String rq) {
Map<String, OilInfo> rs = new HashMap<>();
rq = rq.replaceAll("-", "");
try {
List<OilInfo> list = new ArrayList<>();
for (String nbbm : nbbmArray) {
nbbm = nbbm.split("_")[1];
list.addAll(read(nbbm, rq));
}
Class clazz = OilInfo.class;
rs = new ConvertUtil().groupList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy"));
} catch (Exception e) {
logger.error("", e);
}
return rs;
}
public List<OilInfo> read(String nbbm, String rq) {
return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1);
}
/**
* 将 list 与redis里的数据合并
*
* @param key
* @param list
*/
public void mergeData(String key, List<OilInfo> list) {
key = REDIS_KEY_PREFIX + key;
ListOperations<String, OilInfo> ops = redisTemplate.opsForList();
List<OilInfo> cacheList = ops.range(key, 0, -1);
for (OilInfo oil : cacheList) {
if (!list.contains(oil))
list.add(oil);
}
//删除redis数据
redisTemplate.delete(key);
//重新写入
ops.leftPushAll(key, list);
}
@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.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays);
List<OilInfo> list = oilInfoRepository.findByDateLT(dt.toDate());
//写入redis
wirte(list);
}
}