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

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

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