Commit fcff3a3ae1174bd9f66d8cf0e432ce3c6a75035d
1 parent
46ef4f6e
update...
Showing
1 changed file
with
30 additions
and
1 deletions
src/main/java/com/bsth/redis/OilRedisService.java
| 1 | 1 | package com.bsth.redis; |
| 2 | 2 | |
| 3 | +import com.bsth.Application; | |
| 3 | 4 | import com.bsth.entity.OilInfo; |
| 4 | 5 | import com.bsth.redis.util.RedisUtils; |
| 5 | 6 | import com.bsth.repository.OilInfoRepository; |
| ... | ... | @@ -15,9 +16,11 @@ import org.springframework.core.annotation.Order; |
| 15 | 16 | import org.springframework.data.redis.core.ListOperations; |
| 16 | 17 | import org.springframework.data.redis.core.RedisTemplate; |
| 17 | 18 | import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 19 | +import org.springframework.stereotype.Component; | |
| 18 | 20 | import org.springframework.stereotype.Service; |
| 19 | 21 | |
| 20 | 22 | import java.util.*; |
| 23 | +import java.util.concurrent.TimeUnit; | |
| 21 | 24 | |
| 22 | 25 | /** |
| 23 | 26 | * 油量数据Redis缓存 |
| ... | ... | @@ -36,7 +39,7 @@ public class OilRedisService implements CommandLineRunner { |
| 36 | 39 | @Autowired |
| 37 | 40 | RedisUtils redisUtils; |
| 38 | 41 | |
| 39 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 42 | + static Logger logger = LoggerFactory.getLogger(OilRedisService.class); | |
| 40 | 43 | |
| 41 | 44 | private final static String REDIS_KEY_PREFIX = "oil:"; |
| 42 | 45 | |
| ... | ... | @@ -116,8 +119,19 @@ public class OilRedisService implements CommandLineRunner { |
| 116 | 119 | redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); |
| 117 | 120 | } |
| 118 | 121 | |
| 122 | + @Autowired | |
| 123 | + OilRefreshThread oilRefreshThread; | |
| 124 | + | |
| 119 | 125 | @Override |
| 120 | 126 | public void run(String... strings) throws Exception { |
| 127 | + //定时刷新油耗信息 | |
| 128 | + Application.mainServices.scheduleWithFixedDelay(oilRefreshThread, 20, 60 * 30, TimeUnit.SECONDS); | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * 和数据库同步数据 | |
| 133 | + */ | |
| 134 | + public void synchData(){ | |
| 121 | 135 | int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); |
| 122 | 136 | //设置key 序列化器 |
| 123 | 137 | redisTemplate.setKeySerializer(new StringRedisSerializer()); |
| ... | ... | @@ -128,4 +142,19 @@ public class OilRedisService implements CommandLineRunner { |
| 128 | 142 | //写入redis |
| 129 | 143 | wirte(list); |
| 130 | 144 | } |
| 145 | + | |
| 146 | + @Component | |
| 147 | + public static class OilRefreshThread extends Thread { | |
| 148 | + | |
| 149 | + @Autowired | |
| 150 | + OilRedisService oilRedisService; | |
| 151 | + @Override | |
| 152 | + public void run() { | |
| 153 | + try { | |
| 154 | + oilRedisService.synchData(); | |
| 155 | + } catch (Exception e) { | |
| 156 | + logger.error("", e); | |
| 157 | + } | |
| 158 | + } | |
| 159 | + } | |
| 131 | 160 | } | ... | ... |