提交 13e31a98 authored 作者: zgy's avatar zgy

为shopify的所有商品添加到缓存中

上级 1c3e0710
...@@ -28,6 +28,12 @@ public class KeyConstant { ...@@ -28,6 +28,12 @@ public class KeyConstant {
public final static String CAPTCHA_PHONE = "captcha_phone"; public final static String CAPTCHA_PHONE = "captcha_phone";
//自定义id头部 //自定义id头部
public final static String CUSTOMIZE_ID = "customizeId_"; public final static String CUSTOMIZE_ID = "customizeId_";
//shopify商品 //首页商品
public final static String SHOPIFY_ITEMS = "shopify_items";
//分类商品
public final static String SHOPIFY_TYPE_ITEMS = "shopify_type_items";
//商品详情
public final static String SHOPIFY_ITEM = "shopify_item"; public final static String SHOPIFY_ITEM = "shopify_item";
//商品类型
public final static String SHOPIFY_TYPE = "shopify_item";
} }
package com.diaoyun.zion.chinafrica.service.impl; package com.diaoyun.zion.chinafrica.service.impl;
import com.diaoyun.zion.chinafrica.constant.KeyConstant;
import com.diaoyun.zion.chinafrica.service.ShopifyService; import com.diaoyun.zion.chinafrica.service.ShopifyService;
import com.diaoyun.zion.chinafrica.vo.ShopifyConstant; import com.diaoyun.zion.chinafrica.vo.ShopifyConstant;
import com.diaoyun.zion.master.base.Result; import com.diaoyun.zion.master.base.Result;
...@@ -34,7 +35,7 @@ import java.util.Map; ...@@ -34,7 +35,7 @@ import java.util.Map;
public class ShopifyServiceImpl implements ShopifyService { public class ShopifyServiceImpl implements ShopifyService {
private static Logger logger = LoggerFactory.getLogger(ShopifyServiceImpl.class); private static Logger logger = LoggerFactory.getLogger(ShopifyServiceImpl.class);
@Resource @Resource
private RedisCache<Object> orderRedisCache; private RedisCache<Object> redisCache;
/** /**
* 查询商品 * 查询商品
...@@ -47,12 +48,22 @@ public class ShopifyServiceImpl implements ShopifyService { ...@@ -47,12 +48,22 @@ public class ShopifyServiceImpl implements ShopifyService {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
List<String> list = new ArrayList(); List<String> list = new ArrayList();
try { try {
List type = getProductType(); List products = (List) redisCache.get(KeyConstant.SHOPIFY_ITEMS);
if (products != null) {
JSONArray jsonArray = JSONArray.fromObject(products);
result.setData(jsonArray);
return result;
}
List type = (List) redisCache.get(KeyConstant.SHOPIFY_TYPE);
if (type == null) {
type = getProductType();
}
for (int i = 0; i < type.size(); i++) { for (int i = 0; i < type.size(); i++) {
params.put("product_type", type.get(i)); params.put("product_type", type.get(i));
params.put("limit", ShopifyConstant.SHOPIFY_LIMIT); params.put("limit", ShopifyConstant.SHOPIFY_LIMIT);
String data = HttpClientUtil.createConnection(ShopifyConstant.productTypeUrl(), params, "UTF-8"); String data = HttpClientUtil.createConnection(ShopifyConstant.productTypeUrl(), params, "UTF-8");
list.add(data); list.add(data);
redisCache.set(KeyConstant.SHOPIFY_ITEMS, list);
} }
JSONArray jsonArr = JSONArray.fromObject(list); JSONArray jsonArr = JSONArray.fromObject(list);
result.setData(jsonArr).setMessage("success"); result.setData(jsonArr).setMessage("success");
...@@ -73,6 +84,12 @@ public class ShopifyServiceImpl implements ShopifyService { ...@@ -73,6 +84,12 @@ public class ShopifyServiceImpl implements ShopifyService {
@Override @Override
public Result queryProductsByType(String product_type, String product_id) { public Result queryProductsByType(String product_type, String product_id) {
Result result = new Result(); Result result = new Result();
String typeProducts = (String) redisCache.get(KeyConstant.SHOPIFY_TYPE_ITEMS + product_id);
if (typeProducts != null) {
JSONObject object = JSONObject.fromObject(typeProducts);
result.setData(object);
return result;
}
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("product_type", product_type); params.put("product_type", product_type);
params.put("limit", ShopifyConstant.SHOPIFY_PAGE_SIZE); params.put("limit", ShopifyConstant.SHOPIFY_PAGE_SIZE);
...@@ -80,8 +97,8 @@ public class ShopifyServiceImpl implements ShopifyService { ...@@ -80,8 +97,8 @@ public class ShopifyServiceImpl implements ShopifyService {
try { try {
String data = HttpClientUtil.createConnection(ShopifyConstant.productTypeUrl(), params, "UTF-8"); String data = HttpClientUtil.createConnection(ShopifyConstant.productTypeUrl(), params, "UTF-8");
JSONObject jsonObject = JSONObject.fromObject(data); JSONObject jsonObject = JSONObject.fromObject(data);
System.out.println(jsonObject);
result.setData(jsonObject); result.setData(jsonObject);
redisCache.set(KeyConstant.SHOPIFY_TYPE_ITEMS + product_id, data);
} catch (IOException e) { } catch (IOException e) {
result.setCode(ResultCodeEnum.QUERY_ERROR.getCode()).setMessage(e.getMessage()); result.setCode(ResultCodeEnum.QUERY_ERROR.getCode()).setMessage(e.getMessage());
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
...@@ -99,13 +116,19 @@ public class ShopifyServiceImpl implements ShopifyService { ...@@ -99,13 +116,19 @@ public class ShopifyServiceImpl implements ShopifyService {
@Override @Override
public Result queryProductsDetails(String product_id) { public Result queryProductsDetails(String product_id) {
Result result = new Result(); Result result = new Result();
Map<String, Object> params = new HashMap<>(); JSONObject jsonProduct = null;
params.put("ids", product_id);
try { try {
String data = HttpClientUtil.createConnection(ShopifyConstant.productTypeUrl(), params, "UTF-8"); String product = (String) redisCache.get(KeyConstant.SHOPIFY_ITEM + product_id);
JSONObject jsonObject = JSONObject.fromObject(data); if (product != null) {
System.out.println(jsonObject); jsonProduct = JSONObject.fromObject(product);
result.setData(jsonObject); } else {
Map<String, Object> params = new HashMap<>();
params.put("ids", product_id);
String data = HttpClientUtil.createConnection(ShopifyConstant.productTypeUrl(), params, "UTF-8");
jsonProduct = JSONObject.fromObject(data);
redisCache.set(KeyConstant.SHOPIFY_ITEM + product_id, data);
}
result.setData(jsonProduct);
} catch (IOException e) { } catch (IOException e) {
result.setCode(ResultCodeEnum.QUERY_ERROR.getCode()).setMessage(e.getMessage()); result.setCode(ResultCodeEnum.QUERY_ERROR.getCode()).setMessage(e.getMessage());
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
...@@ -127,13 +150,13 @@ public class ShopifyServiceImpl implements ShopifyService { ...@@ -127,13 +150,13 @@ public class ShopifyServiceImpl implements ShopifyService {
List list = new ArrayList(); List list = new ArrayList();
JSONArray products = JSONObject.fromObject(connection).getJSONArray("products"); JSONArray products = JSONObject.fromObject(connection).getJSONArray("products");
for (int i = 0; i < products.size(); i++) { for (int i = 0; i < products.size(); i++) {
String string = products.getString(i); String product_type = products.getJSONObject(i).getString("product_type");
JSONObject jsonObject = JSONObject.fromObject(string);
String product_type = jsonObject.getString("product_type");
if (!"".equals(product_type) && !list.contains(product_type)) { if (!"".equals(product_type) && !list.contains(product_type)) {
list.add(product_type); list.add(product_type);
} }
} }
//将商品类型放到缓存中
redisCache.set(KeyConstant.SHOPIFY_TYPE, list);
return list; return list;
} }
} }
package com.diaoyun.zion.chinafrica.vo; package com.diaoyun.zion.chinafrica.vo;
import io.swagger.models.auth.In;
/** /**
* @Auther: wudepeng * @Auther: wudepeng
...@@ -20,6 +19,7 @@ public class ShopifyConstant { ...@@ -20,6 +19,7 @@ public class ShopifyConstant {
public static String SHOPIFY_PASSWORD = "1c97a1222c5b40e0597c816c6dfecac0"; public static String SHOPIFY_PASSWORD = "1c97a1222c5b40e0597c816c6dfecac0";
//api //api
public static String SHOPIFY_PRODUCTS_API = "/admin/api/2019-10/products.json"; public static String SHOPIFY_PRODUCTS_API = "/admin/api/2019-10/products.json";
//https://7f0e048ac4016b9357bb1eb2217e4201:1c97a1222c5b40e0597c816c6dfecac0@mollykitty.myshopify.com/admin/api/2019-10/products.json?fields=product_type; //https://7f0e048ac4016b9357bb1eb2217e4201:1c97a1222c5b40e0597c816c6dfecac0@mollykitty.myshopify.com/admin/api/2019-10/products.json?fields=product_type;
public static String productTypeUrl() { public static String productTypeUrl() {
......
...@@ -7,6 +7,7 @@ import com.diaoyun.zion.master.enums.ResultCodeEnum; ...@@ -7,6 +7,7 @@ import com.diaoyun.zion.master.enums.ResultCodeEnum;
import com.diaoyun.zion.master.util.HttpClientUtil; import com.diaoyun.zion.master.util.HttpClientUtil;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import io.swagger.models.auth.In;
import net.sf.json.JSONArray; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.junit.Test; import org.junit.Test;
...@@ -81,4 +82,13 @@ public class PayTest { ...@@ -81,4 +82,13 @@ public class PayTest {
} }
} }
@Test
public void test3(){
Integer count=0;
while (count<4){
System.out.println("helloworld!");
count++;
}
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论