From da241f8ef53ea0cbf103a0094cd6fedffe37090f Mon Sep 17 00:00:00 2001 From: novohit Date: Sat, 11 Nov 2023 13:37:24 +0800 Subject: [PATCH] feat: page view count --- .../src/views/list-page/share/index.vue | 4 +- .../server/common/constant/BizConstant.java | 6 +++ .../server/common/pvcount/PVCountTask.java | 45 +++++++++++++++++++ .../link/controller/ShortLinkController.java | 1 + .../server/link/service/ShortLinkService.java | 35 ++++++++++++++- .../share/controller/response/ShareVO.java | 3 +- .../server/share/convert/ShareConverter.java | 5 +-- .../share/service/impl/ShareServiceImpl.java | 6 ++- 8 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 server/src/main/java/com/cloudshare/server/common/pvcount/PVCountTask.java diff --git a/cloudshare-frontend/src/views/list-page/share/index.vue b/cloudshare-frontend/src/views/list-page/share/index.vue index 2e23b27..90da446 100644 --- a/cloudshare-frontend/src/views/list-page/share/index.vue +++ b/cloudshare-frontend/src/views/list-page/share/index.vue @@ -91,13 +91,13 @@ min-width="100" align="center"> - - + --> keys = stringRedisTemplate.keys(BizConstant.LINK_PV_PREFIX + "*"); + if (!CollectionUtils.isEmpty(keys)) { + List strings = keys.stream().toList(); + List values = stringRedisTemplate.opsForValue().multiGet(strings); + for (int i = 0; i < strings.size(); i++) { + String key = strings.get(i); + String code = key.replaceFirst(BizConstant.LINK_PV_PREFIX, ""); + shortLinkService.savePV(code, Long.parseLong(values.get(i))); + } + } + } +} diff --git a/server/src/main/java/com/cloudshare/server/link/controller/ShortLinkController.java b/server/src/main/java/com/cloudshare/server/link/controller/ShortLinkController.java index 185506c..a06c8a7 100644 --- a/server/src/main/java/com/cloudshare/server/link/controller/ShortLinkController.java +++ b/server/src/main/java/com/cloudshare/server/link/controller/ShortLinkController.java @@ -54,6 +54,7 @@ public void dispatch(@PathVariable("code") String code, HttpServletRequest reque if (CheckUtil.isLetterOrDigit(code)) { ShortLink shortLink = shortLinkService.findOneByCode(code); if (shortLink != null) { + shortLinkService.incrementPV(code); response.setHeader("Location", shortLink.getOriginalUrl()); // 302跳转 response.setStatus(HttpStatus.FOUND.value()); diff --git a/server/src/main/java/com/cloudshare/server/link/service/ShortLinkService.java b/server/src/main/java/com/cloudshare/server/link/service/ShortLinkService.java index d8d69ee..5dcd5ce 100644 --- a/server/src/main/java/com/cloudshare/server/link/service/ShortLinkService.java +++ b/server/src/main/java/com/cloudshare/server/link/service/ShortLinkService.java @@ -1,10 +1,13 @@ package com.cloudshare.server.link.service; +import com.cloudshare.server.common.constant.BizConstant; import com.cloudshare.server.link.model.ShortLink; import com.cloudshare.server.link.repository.ShortLinkRepository; import com.google.common.hash.Hashing; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; /** * @author novo @@ -15,13 +18,16 @@ public class ShortLinkService { private final ShortLinkRepository shortLinkRepository; + private final StringRedisTemplate stringRedisTemplate; + private static final String CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; @Value("${cloudshare.short-link.url}") private String domain; - public ShortLinkService(ShortLinkRepository shortLinkRepository) { + public ShortLinkService(ShortLinkRepository shortLinkRepository, StringRedisTemplate stringRedisTemplate) { this.shortLinkRepository = shortLinkRepository; + this.stringRedisTemplate = stringRedisTemplate; } @@ -39,10 +45,35 @@ public String create(Long shareId, String url) { shortLink.setShortUrl(domain + code62); shortLink.setOriginalUrl(url); shortLink.setShareId(shareId); + shortLink.setPv(0L); + shortLink.setUv(0L); shortLinkRepository.save(shortLink); return shortLink.getShortUrl(); } + + public void incrementPV(String code) { + stringRedisTemplate.opsForValue().increment(BizConstant.LINK_PV_PREFIX + code); + } + + public Long getPV(String code) { + String pv = stringRedisTemplate.opsForValue().get(BizConstant.LINK_PV_PREFIX + code); + if (!StringUtils.hasText(pv)) { + ShortLink shortLink = shortLinkRepository.findOneByCode(code); + return shortLink.getPv(); + } + return Long.parseLong(pv); + } + + + public void savePV(String code, Long pv) { + ShortLink shortLink = shortLinkRepository.findOneByCode(code); + shortLink.setPv(pv); + shortLinkRepository.save(shortLink); + } + + // ================================================================================================================= + /** * google murmurhash算法 * @@ -50,7 +81,7 @@ public String create(Long shareId, String url) { * @return */ @SuppressWarnings(value = {"all"}) - public static long murmurHash32(String value) { + private long murmurHash32(String value) { long murmurHash32 = Hashing.murmur3_32().hashUnencodedChars(value).padToLong(); return murmurHash32; } diff --git a/server/src/main/java/com/cloudshare/server/share/controller/response/ShareVO.java b/server/src/main/java/com/cloudshare/server/share/controller/response/ShareVO.java index 69e8cff..14f7c4d 100644 --- a/server/src/main/java/com/cloudshare/server/share/controller/response/ShareVO.java +++ b/server/src/main/java/com/cloudshare/server/share/controller/response/ShareVO.java @@ -18,8 +18,7 @@ public record ShareVO( String code, LocalDateTime expiredAt, String fileName, - Integer pv, - Integer download, + Long pv, LocalDateTime createdAt ) { } diff --git a/server/src/main/java/com/cloudshare/server/share/convert/ShareConverter.java b/server/src/main/java/com/cloudshare/server/share/convert/ShareConverter.java index 6eaa142..3ac9845 100644 --- a/server/src/main/java/com/cloudshare/server/share/convert/ShareConverter.java +++ b/server/src/main/java/com/cloudshare/server/share/convert/ShareConverter.java @@ -14,11 +14,10 @@ @Mapper(componentModel = "spring") public interface ShareConverter { - @Mapping(target = "pv", constant = "0") - @Mapping(target = "download", constant = "0") + @Mapping(target = "pv", expression = "java(pv)") @Mapping(target = "fileName", expression = "java(share.getFileDocument().getName())") @Mapping(target = "url", expression = "java(share.getShortLink().getShortUrl())") - ShareVO DO2VO(Share share); + ShareVO DO2VO(Share share, Long pv); List DOList2VOList(List shareList); diff --git a/server/src/main/java/com/cloudshare/server/share/service/impl/ShareServiceImpl.java b/server/src/main/java/com/cloudshare/server/share/service/impl/ShareServiceImpl.java index 49f6a36..87fc1ed 100644 --- a/server/src/main/java/com/cloudshare/server/share/service/impl/ShareServiceImpl.java +++ b/server/src/main/java/com/cloudshare/server/share/service/impl/ShareServiceImpl.java @@ -105,7 +105,11 @@ public ShareCreateRespVO create(ShareCreateReqDTO reqDTO) { public List list() { Long userId = UserContextThreadHolder.getUserId(); List shareList = shareRepository.findByUserId(userId); - List resp = shareConverter.DOList2VOList(shareList); + List resp = new ArrayList<>(); + for (Share share : shareList) { + resp.add(shareConverter.DO2VO(share, shortLinkService.getPV(share.getShortLink().getCode()))); + } +// List resp = shareConverter.DOList2VOList(shareList); return resp; }