Browse Source

为新闻服务添加redis缓存

wangdy 8 years ago
parent
commit
0fcc16f1f9

+ 41 - 5
src/main/java/com/uas/platform/b2c/advertise/news/service/impl/NewsServiceImpl.java

@@ -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);
+	}
 }

+ 1 - 1
src/main/resources/dev/redis.properties

@@ -1,2 +1,2 @@
-redis.host=192.168.253.6
+redis.host=10.10.100.200
 redis.port=6379

+ 1 - 0
src/main/resources/dev/sys.properties

@@ -24,6 +24,7 @@ reportUploadUrl=http://print.ubtob.com/report/fileUpload?userName=B2C
 # micro service IP(Swarm Manager IP)
 microServiceIp=10.10.101.23
 newsMicroServiceIp=news.usoftchina.com
+newsRedisRefresh=600
 endpointUri=10.10.101.23
 recommendPort=20102
 

+ 1 - 0
src/main/resources/prod/sys.properties

@@ -25,6 +25,7 @@ reportUploadUrl=http://print.ubtob.com/report/fileUpload?userName=B2C
 # micro service IP(Swarm Manager IP)
 microServiceIp=10.10.100.23
 newsMicroServiceIp=news.usoftchina.com
+newsRedisRefresh=600
 endpointUri=10.10.100.23
 recommendPort=20100
 

+ 2 - 1
src/main/resources/test/redis.properties

@@ -1,2 +1,3 @@
-redis.host=192.168.253.6
+#redis.host=192.168.253.6
+redis.host=10.10.100.200
 redis.port=6379

+ 1 - 0
src/main/resources/test/sys.properties

@@ -24,6 +24,7 @@ reportUploadUrl=http://print.ubtob.com/report/fileUpload?userName=B2C
 # micro service IP(Swarm Manager IP)
 microServiceIp=10.10.101.23
 newsMicroServiceIp=news.usoftchina.com
+newsRedisRefresh=600
 endpointUri=10.10.101.23
 recommendPort=20102