|
|
@@ -3,11 +3,21 @@ package com.uas.platform.b2c.advertise.news.service.impl;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.platform.b2c.advertise.news.service.NewsService;
|
|
|
import com.uas.platform.b2c.core.config.MicroServicesConf;
|
|
|
+import com.uas.platform.b2c.core.utils.FastjsonUtils;
|
|
|
import com.uas.platform.b2c.core.utils.JacksonUtils;
|
|
|
+import javafx.beans.binding.ObjectBinding;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.dao.DataAccessException;
|
|
|
+import org.springframework.data.redis.connection.RedisConnection;
|
|
|
+import org.springframework.data.redis.core.RedisCallback;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import java.io.Serializable;
|
|
|
+
|
|
|
/**
|
|
|
* 新闻操作业务实现类
|
|
|
*
|
|
|
@@ -17,30 +27,37 @@ import org.springframework.web.client.RestTemplate;
|
|
|
@Service
|
|
|
public class NewsServiceImpl implements NewsService {
|
|
|
|
|
|
+ private final RedisTemplate<Serializable,Serializable> redisTemplate;
|
|
|
+
|
|
|
private final MicroServicesConf conf;
|
|
|
|
|
|
private final RestTemplate restTemplate;
|
|
|
|
|
|
+ /**
|
|
|
+ * 缓存刷新时间
|
|
|
+ */
|
|
|
+ @Value("#{sys.newsRedisRefresh ?: 600}")
|
|
|
+ private long newsRedisRefresh;
|
|
|
+
|
|
|
@Autowired
|
|
|
- public NewsServiceImpl(MicroServicesConf conf, RestTemplate restTemplate) {
|
|
|
+ public NewsServiceImpl(MicroServicesConf conf, RestTemplate restTemplate, RedisTemplate redisTemplate) {
|
|
|
this.conf = conf;
|
|
|
this.restTemplate = restTemplate;
|
|
|
+ this.redisTemplate = redisTemplate;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public JSONObject getNewsListByPage(int page, int pageSize) {
|
|
|
if (page <= 0) page = 1;
|
|
|
String url = conf.getRequestUrlForNews( "/news/created/v2?pagenumber=" + page +"&pagesize=" + pageSize + "&taxonomySlug=B2C");
|
|
|
- String result = restTemplate.getForEntity(url, String.class).getBody();
|
|
|
- return JacksonUtils.fromJson(result, JSONObject.class);
|
|
|
+ return getDataFromRedisByUrl(url);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public JSONObject getNewsListOrderByViewCount(int page, int pageSize) {
|
|
|
if (page <= 0) page = 1;
|
|
|
String url = conf.getRequestUrlForNews("/news/viewcount/v2?pagenumber=" + page +"&pagesize=" + pageSize + "&taxonomySlug=B2C");
|
|
|
- String result = restTemplate.getForEntity(url, String.class).getBody();
|
|
|
- return JacksonUtils.fromJson(result, JSONObject.class);
|
|
|
+ return getDataFromRedisByUrl(url);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -50,4 +67,23 @@ public class NewsServiceImpl implements NewsService {
|
|
|
String result = restTemplate.getForEntity(url, String.class).getBody();
|
|
|
return JacksonUtils.fromJson(result, JSONObject.class);
|
|
|
}
|
|
|
+
|
|
|
+ private JSONObject getDataFromRedisByUrl(final String url){
|
|
|
+ Object result = redisTemplate.execute(new RedisCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ byte[] key = redisTemplate.getStringSerializer().serialize(url);
|
|
|
+ if (connection.exists(key)) {
|
|
|
+ String value = redisTemplate.getStringSerializer().deserialize(connection.get(key));
|
|
|
+ return value;
|
|
|
+ }else {
|
|
|
+ byte[] value = redisTemplate.getStringSerializer().serialize(restTemplate.getForEntity(url, String.class).getBody());
|
|
|
+ connection.set(key, value);
|
|
|
+ connection.expire(key, newsRedisRefresh);
|
|
|
+ return redisTemplate.getStringSerializer().deserialize(connection.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return JacksonUtils.fromJson(result.toString(), JSONObject.class);
|
|
|
+ }
|
|
|
}
|