尚品汇-商品热度实现利用Redis更新ES字段(二十九)

目录:

(1)商品热度排名

(2)在service-list-client封装远程接口

(3)在service-item模块调用接口

(1)商品热度排名

搜索商品时,后面我们会根据热点排序,何时更新热点?我们在获取商品详情时调用更新

当我们进入商品详情页的时候,我们从Reids取缓存数据时,这个取时候就把热点+1,改ES中的hotScore字段,然后在搜索商品的时候就根据那个字段进行排序

在service-list模块添加: 

封装接口与实现类与控制器

SearchService
/**
 * 更新热点
 * @param skuId
 */
void incrHotScore(Long skuId); 
实现类

 

Redis:使用zincrby hotScore 1  skuId:21 

@Autowired
private RedisTemplate redisTemplate;

@Override
public void incrHotScore(Long skuId) {
    // 定义key
    String hotKey = "hotScore";
    // 保存数据
    Double hotScore = redisTemplate.opsForZSet().incrementScore(hotKey, "skuId:" + skuId, 1);
    if (hotScore%10==0){
        // 更新es
        Optional<Goods> optional = goodsRepository.findById(skuId);
        Goods goods = optional.get();
        goods.setHotScore(Math.round(hotScore));
        goodsRepository.save(goods);
    }
}

控制器ListApiController:

/**
 * 更新商品incrHotScore
 *
 * @param skuId
 * @return
 */
@GetMapping("inner/incrHotScore/{skuId}")
public Result incrHotScore(@PathVariable("skuId") Long skuId) {
    // 调用服务层
    searchService.incrHotScore(skuId);
    return Result.ok();
}

(2)在service-list-client封装远程接口

搭建service-list-client

搭建方式如service-item-client

修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.atguigu.gmall</groupId>
        <artifactId>service-client</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>service-list-client</artifactId>
    <version>1.0</version>

    <packaging>jar</packaging>
    <name>service-list-client</name>
    <description>service-list-client</description>

</project>

添加接口

package com.atguigu.gmall.list.client;

@FeignClient(value = "service-list", fallback = ListDegradeFeignClient.class)
public interface ListFeignClient {

   /**
 * 更新商品incrHotScore
 * @param skuId
 * @return
 */
@GetMapping("/api/list/inner/incrHotScore/{skuId}")
Result incrHotScore(@PathVariable("skuId") Long skuId);

}

 降级返回:

package com.atguigu.gmall.list.client.impl;

@Component
public class ListDegradeFeignClient implements ListFeignClient {

    @Override
public Result incrHotScore(Long skuId) {
    return null;
}
}

(3)在service-item模块调用接口

引入依赖

<dependency>

   <groupId>com.atguigu.gmall</groupId>

   <artifactId>service-list-client</artifactId>

   <version>1.0</version>

</dependency>

接口调用

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ListFeignClient listFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public Map<String, Object> getBySkuId(Long skuId) {


        Map<String, Object> result = new HashMap<>();

       ...


        //获取分类信息
        CompletableFuture<Void> categoryViewCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());

            //分类信息
            result.put("categoryView", categoryView);
        }, threadPoolExecutor);

        //更新商品incrHotScore
        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        CompletableFuture.allOf(skuCompletableFuture, spuSaleAttrCompletableFuture, skuValueIdsMapCompletableFuture,skuPriceCompletableFuture, categoryViewCompletableFuture, incrHotScoreCompletableFuture).join();
        return result;
    }
}

 继续添加下面代码

访问页面到19次,他是不会增加10的,每访问10才进行增加 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值