Skip to content

Commit

Permalink
feat: page view count
Browse files Browse the repository at this point in the history
  • Loading branch information
novohit committed Nov 11, 2023
1 parent db10fe5 commit da241f8
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cloudshare-frontend/src/views/list-page/share/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@
min-width="100"
align="center">
</el-table-column>
<el-table-column
<!-- <el-table-column
prop="download"
sortable
label="下载量"
min-width="100"
align="center">
</el-table-column>
</el-table-column> -->
<el-table-column
prop="shareStatus"
sortable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
public final class BizConstant {

private BizConstant() {

}

public static final String LINUX_SEPARATOR = "/";

public static final String DOT = ".";
Expand All @@ -29,4 +33,6 @@ public final class BizConstant {
public static final String SEARCH_HISTORY_PREFIX = "cloudshare:search_history:";

public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

public static final String LINK_PV_PREFIX = "cloudshare:pv:";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.cloudshare.server.common.pvcount;

import com.cloudshare.server.common.constant.BizConstant;
import com.cloudshare.server.link.service.ShortLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

/**
* @author novo
* @since 2023/11/7
*/
@Component
@Slf4j
public class PVCountTask {

private final ShortLinkService shortLinkService;

private final StringRedisTemplate stringRedisTemplate;

public PVCountTask(ShortLinkService shortLinkService, StringRedisTemplate stringRedisTemplate) {
this.shortLinkService = shortLinkService;
this.stringRedisTemplate = stringRedisTemplate;
}


@Scheduled(cron = "0 */5 * * * ?")
public void dataSync() {
Set<String> keys = stringRedisTemplate.keys(BizConstant.LINK_PV_PREFIX + "*");
if (!CollectionUtils.isEmpty(keys)) {
List<String> strings = keys.stream().toList();
List<String> 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)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}


Expand All @@ -39,18 +45,43 @@ 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算法
*
* @param value
* @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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public record ShareVO(
String code,
LocalDateTime expiredAt,
String fileName,
Integer pv,
Integer download,
Long pv,
LocalDateTime createdAt
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShareVO> DOList2VOList(List<Share> shareList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public ShareCreateRespVO create(ShareCreateReqDTO reqDTO) {
public List<ShareVO> list() {
Long userId = UserContextThreadHolder.getUserId();
List<Share> shareList = shareRepository.findByUserId(userId);
List<ShareVO> resp = shareConverter.DOList2VOList(shareList);
List<ShareVO> resp = new ArrayList<>();
for (Share share : shareList) {
resp.add(shareConverter.DO2VO(share, shortLinkService.getPV(share.getShortLink().getCode())));
}
// List<ShareVO> resp = shareConverter.DOList2VOList(shareList);
return resp;
}

Expand Down

0 comments on commit da241f8

Please sign in to comment.