Redis缓存展现首页信息(类目以及轮廓图)

配置好相关的主配置文件以及nginx反向代理

前提是服务端已做好给客户端调用:(redis与httpclient配置文件在另片博客中有些)

类目:

    @Autowired
    private HttpClientService httpClientService;

    @Value("${MANAGE_SHANGJI_URL}")
    private String MANAGE_SHANGJI_URL;

    @Autowired
    private JedisService jedisService;

    private static final ObjectMapper MAPPER = new ObjectMapper();
     //查询商品基本信息
    public Item queryItemById(Long itemId) {
        // 从缓存中命中
        String key = REDIS_ITEM_KEY.SHANGJI_SHOP_ITEM_.toString() + itemId;
        try {
            String cacheData = this.jedisService.get(key);
            if (null!=cacheData) {
                return MAPPER.readValue(cacheData, Item.class);
            }
        } catch (Exception e1) {
            // TODO
            e1.printStackTrace();
        }
        try {
            String url = MANAGE_SHANGJI_URL + "/rest/item/" + itemId;
            String jsonData = this.httpClientService.doGet(url);
            Item item = MAPPER.readValue(jsonData, Item.class);

            try {
                Long timeOut =  (long) (24*7);
                // 将结果集写入到缓存中
                this.jedisService.set(key, jsonData,timeOut);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return item;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

轮廓图:

    @Autowired
    private HttpClientService httpClientService;

    @Value("${MANAGE_SHANGJI_URL}")
    private String MANAGE_SHANGJI_URL;

    @Autowired
    private JedisService jedisService;

    private static final ObjectMapper MAPPER = new ObjectMapper();
     //查询商品基本信息
    public Item queryItemById(Long itemId) {
        // 从缓存中命中
        String key = REDIS_ITEM_KEY.SHANGJI_SHOP_ITEM_.toString() + itemId;
        try {
            String cacheData = this.jedisService.get(key);
            if (null!=cacheData) {
                return MAPPER.readValue(cacheData, Item.class);
            }
        } catch (Exception e1) {
            // TODO
            e1.printStackTrace();
        }
        try {
            String url = MANAGE_SHANGJI_URL + "/rest/item/" + itemId;
            String jsonData = this.httpClientService.doGet(url);
            Item item = MAPPER.readValue(jsonData, Item.class);

            try {
                Long timeOut =  (long) (24*7);
                // 将结果集写入到缓存中
                this.jedisService.set(key, jsonData,timeOut);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return item;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

凭证:

public class SysConstant {
    public static final String SHANGJI_SHOP_BIG_KEY="89";
    
    public enum SHANGJI_KEY_ID{
        SHANGJI_SHOP_ITEMCAT_ALL,
        REDIS_CACHE_BIG_AD_KEY
    }
    
    public enum REDIS_ITEM_KEY{
        SHANGJI_SHOP_ITEM_,
    }
}


工具类:

public class HttpClientService {

    @Autowired
    private CloseableHttpClient httpClient;
    
    @Autowired
    private RequestConfig requestConfig;

    /**
     * 执行GET请求,如果请求状态不为200返回null
     * 
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String doGet(String url) throws ClientProtocolException, IOException {
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpClient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return null;
    }

    /**
     * 带参数的GET请求
     * 
     * @param url
     * @param params
     * @return
     * @throws URISyntaxException
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String doGet(String url, Map<String, Object> params) throws URISyntaxException,
            ClientProtocolException, IOException {
        // 拼接URL
        URIBuilder uriBuilder = new URIBuilder(url);//?fdsf=fdsf
        if (null != params) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }
        String newUrl = uriBuilder.build().toString();
        return doGet(newUrl);
    }

    /**
     * 执行doPOST请求,请求响应状态不为200返回null
     * 
     * @param url
     * @param params
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String doPost(String url, Map<String, Object> params) throws ClientProtocolException, IOException {
        // 创建http POST请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        // 设置参数
        if (null != params) {
            // 参数列表
            List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            // 构造一个form表单式的实体
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
            // 将请求实体设置到httpPost对象中
            httpPost.setEntity(formEntity);
        }

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpClient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return null;
    }
    
    /**
     * 执行POST请求
     * 
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String doPost(String url) throws ClientProtocolException, IOException {
        return doPost(url, null);
    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值