价格走势,每个维度都多增加前一个节点给APP
Showing
6 changed files
with
122 additions
and
4 deletions
1 | +package com.yohoufo.product.cache; | ||
2 | + | ||
3 | +import java.util.concurrent.TimeUnit; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by li.ma on 2019/3/6. | ||
7 | + */ | ||
8 | +public enum UfoProductCacheKeyEnum { | ||
9 | + SELLER_LIMIT_KEY("ufo:product:limitInfo:", "出售限制", 5, TimeUnit.MINUTES); | ||
10 | + | ||
11 | + private String cacheKey; | ||
12 | + | ||
13 | + private String desc; | ||
14 | + | ||
15 | + private int defaultExpireTime; | ||
16 | + | ||
17 | + private TimeUnit timeUnit; // 缓存时间单位 | ||
18 | + | ||
19 | + UfoProductCacheKeyEnum(String cacheKey, String desc, int defaultExpireTime, TimeUnit timeUnit) { | ||
20 | + this.cacheKey = cacheKey; | ||
21 | + this.desc = desc; | ||
22 | + this.defaultExpireTime = defaultExpireTime; | ||
23 | + this.timeUnit = timeUnit; | ||
24 | + } | ||
25 | + | ||
26 | + public int getDefaultExpireTime() { | ||
27 | + return defaultExpireTime; | ||
28 | + } | ||
29 | + | ||
30 | + public String getCacheKey() { | ||
31 | + return cacheKey; | ||
32 | + } | ||
33 | + | ||
34 | + public TimeUnit getTimeUnit() { | ||
35 | + return timeUnit; | ||
36 | + } | ||
37 | + | ||
38 | + public long getExpireSecondTime() { | ||
39 | + return this.timeUnit.toSeconds(defaultExpireTime); | ||
40 | + } | ||
41 | +} |
1 | +package com.yohoufo.product.cache; | ||
2 | + | ||
3 | +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | ||
4 | +import com.yohoufo.common.cache.CacheClient; | ||
5 | +import org.springframework.beans.factory.annotation.Autowired; | ||
6 | +import org.springframework.stereotype.Service; | ||
7 | + | ||
8 | +import java.util.Map; | ||
9 | + | ||
10 | +/** | ||
11 | + * Created by li.ma on 2019/3/6. | ||
12 | + */ | ||
13 | +@Service | ||
14 | +public class UfoProductCacheService { | ||
15 | + @Autowired | ||
16 | + private CacheClient cacheClient; | ||
17 | + | ||
18 | + /** | ||
19 | + * 单个设置字符串缓存 | ||
20 | + * @param cacheKeyEnum 缓存的枚举类 | ||
21 | + * @param value 缓存的值 | ||
22 | + * @param keyParams 缓存key可变的参数列表 可空 | ||
23 | + */ | ||
24 | + public void setCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) { | ||
25 | + RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams); | ||
26 | + cacheClient.setEx(redisKeyBuilder, value, cacheKeyEnum.getExpireSecondTime()); | ||
27 | + } | ||
28 | + | ||
29 | + | ||
30 | + /** | ||
31 | + * 批量设置字符串缓存 仅适合同一场场景的缓存批量设置 | ||
32 | + * @param cacheKeyEnum 缓存的枚举类 | ||
33 | + * @param value 缓存的值 | ||
34 | + * @param keyParams 缓存key可变的参数列表 可空 | ||
35 | + */ | ||
36 | + public void msetCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) { | ||
37 | + RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams); | ||
38 | + Map<RedisKeyBuilder, Object> map = null; | ||
39 | + cacheClient.mset(map, cacheKeyEnum.getExpireSecondTime()); | ||
40 | + } | ||
41 | + | ||
42 | + /** | ||
43 | + * 获取缓存 | ||
44 | + * @param cacheKeyEnum 缓存的枚举类 | ||
45 | + * @param clazz 缓存的值对象的类型 | ||
46 | + * @param keyParams 缓存key可变的参数列表 可空 | ||
47 | + * @param <T> | ||
48 | + * @return | ||
49 | + */ | ||
50 | + public <T> T getCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Class<T> clazz, String... keyParams) { | ||
51 | + RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams); | ||
52 | + return cacheClient.get(redisKeyBuilder, clazz); | ||
53 | + } | ||
54 | + | ||
55 | + | ||
56 | + /** | ||
57 | + * 删除缓存 | ||
58 | + * @param cacheKeyEnum 缓存的枚举类 | ||
59 | + * @param keyParams 缓存key可变的参数列表 可空 | ||
60 | + */ | ||
61 | + public void deleteCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, String... keyParams) { | ||
62 | + RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams); | ||
63 | + cacheClient.delete(redisKeyBuilder.getKey()); | ||
64 | + } | ||
65 | + | ||
66 | + | ||
67 | + private RedisKeyBuilder getRedisKeyBuilder(UfoProductCacheKeyEnum cacheKeyEnum, String[] keyParams) { | ||
68 | + RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance(); | ||
69 | + redisKeyBuilder.appendFixed(cacheKeyEnum.getCacheKey()).appendVarWithMH(keyParams); | ||
70 | + return redisKeyBuilder; | ||
71 | + } | ||
72 | +} |
@@ -22,13 +22,14 @@ public class PriceTrendDayService implements PriceTrendServiceInf{ | @@ -22,13 +22,14 @@ public class PriceTrendDayService implements PriceTrendServiceInf{ | ||
22 | @Autowired | 22 | @Autowired |
23 | private PriceTrendDayMapper priceTrendDayMapper; | 23 | private PriceTrendDayMapper priceTrendDayMapper; |
24 | 24 | ||
25 | + // 两个小时颗粒度 | ||
25 | public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { | 26 | public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { |
26 | if (null != productId) { | 27 | if (null != productId) { |
27 | int currentSecond = DateUtil.getCurrentTimeSecond(); | 28 | int currentSecond = DateUtil.getCurrentTimeSecond(); |
28 | 29 | ||
29 | LOG.info("method priceTrendDayMapper.selectByProductId in, productId:{},sizeId:{},currentSecond:{}", productId, sizeId, currentSecond); | 30 | LOG.info("method priceTrendDayMapper.selectByProductId in, productId:{},sizeId:{},currentSecond:{}", productId, sizeId, currentSecond); |
30 | 31 | ||
31 | - List<PriceTrendModel> priceTrendModels = priceTrendDayMapper.selectByProductId(productId, sizeId, currentSecond - 86400, currentSecond); | 32 | + List<PriceTrendModel> priceTrendModels = priceTrendDayMapper.selectByProductId(productId, sizeId, currentSecond - 86400 - 7200, currentSecond); |
32 | 33 | ||
33 | priceTrendModels.stream().forEach(item -> { | 34 | priceTrendModels.stream().forEach(item -> { |
34 | item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime()); | 35 | item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime()); |
@@ -30,7 +30,7 @@ public class PriceTrendHalfYearService implements PriceTrendServiceInf{ | @@ -30,7 +30,7 @@ public class PriceTrendHalfYearService implements PriceTrendServiceInf{ | ||
30 | // 180天(最小颗粒度:3天); | 30 | // 180天(最小颗粒度:3天); |
31 | public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { | 31 | public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { |
32 | if (null != productId) { | 32 | if (null != productId) { |
33 | - int timeSecondOfHalfYear = DateUtil.getTimeSecondOfDay(-179); | 33 | + int timeSecondOfHalfYear = DateUtil.getTimeSecondOfDay(-182); // 多放一个节点给APP,APP首节点好展示 |
34 | List<PriceTrendModel> priceTrendModels = priceTrendHalfYearMapper.selectByProductId(productId, sizeId, timeSecondOfHalfYear, DateUtil.getCurrentTimeSecond()); | 34 | List<PriceTrendModel> priceTrendModels = priceTrendHalfYearMapper.selectByProductId(productId, sizeId, timeSecondOfHalfYear, DateUtil.getCurrentTimeSecond()); |
35 | 35 | ||
36 | //List<PriceTrendModel> completeResult = compeleteZeroPrice(priceTrendModels, timeSecondOfHalfYear, productId, sizeId); | 36 | //List<PriceTrendModel> completeResult = compeleteZeroPrice(priceTrendModels, timeSecondOfHalfYear, productId, sizeId); |
@@ -19,9 +19,11 @@ public class PriceTrendMonthService implements PriceTrendServiceInf{ | @@ -19,9 +19,11 @@ public class PriceTrendMonthService implements PriceTrendServiceInf{ | ||
19 | @Autowired | 19 | @Autowired |
20 | private PriceTrendMonthMapper priceTrendMonthMapper; | 20 | private PriceTrendMonthMapper priceTrendMonthMapper; |
21 | 21 | ||
22 | + // 12小时一个颗粒 | ||
22 | public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { | 23 | public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { |
23 | if (null != productId) { | 24 | if (null != productId) { |
24 | - List<PriceTrendModel> priceTrendModels = priceTrendMonthMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-29), DateUtil.getCurrentTimeSecond()); | 25 | + // 多一个节点给APP,好展示 |
26 | + List<PriceTrendModel> priceTrendModels = priceTrendMonthMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-29) - 43200, DateUtil.getCurrentTimeSecond()); | ||
25 | 27 | ||
26 | priceTrendModels.stream().forEach(item -> { | 28 | priceTrendModels.stream().forEach(item -> { |
27 | item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime()); | 29 | item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime()); |
@@ -17,9 +17,11 @@ public class PriceTrendSixtyDayService implements PriceTrendServiceInf{ | @@ -17,9 +17,11 @@ public class PriceTrendSixtyDayService implements PriceTrendServiceInf{ | ||
17 | @Autowired | 17 | @Autowired |
18 | private PriceTrendSixtyDayMapper priceTrendSixtyDayMapper; | 18 | private PriceTrendSixtyDayMapper priceTrendSixtyDayMapper; |
19 | 19 | ||
20 | + // 颗粒度一天一个节点 | ||
20 | public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { | 21 | public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { |
21 | if (null != productId) { | 22 | if (null != productId) { |
22 | - List<PriceTrendModel> priceTrendModels = priceTrendSixtyDayMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-59), DateUtil.getCurrentTimeSecond()); | 23 | + // 多放一个节点给APP,APP首节点好展示 |
24 | + List<PriceTrendModel> priceTrendModels = priceTrendSixtyDayMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-60), DateUtil.getCurrentTimeSecond()); | ||
23 | priceTrendModels.stream().forEach(item -> { | 25 | priceTrendModels.stream().forEach(item -> { |
24 | item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime()); | 26 | item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime()); |
25 | item.setStartTime(null == item.getCreateTime() ? null : item.getCreateTime() - 86400); | 27 | item.setStartTime(null == item.getCreateTime() ? null : item.getCreateTime() - 86400); |
-
Please register or login to post a comment