7 changed files with 195 additions and 7 deletions
@ -0,0 +1,25 @@ |
|||||
|
package com.bnyer.img.service; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface HotSearchKeywordService { |
||||
|
|
||||
|
/** |
||||
|
* 获取最近一个月搜索次数最多的前10条记录 |
||||
|
* @return - |
||||
|
*/ |
||||
|
List<String> getHotKeywordList(); |
||||
|
|
||||
|
/** |
||||
|
* 点击增长热搜词热度次数 |
||||
|
* @param keyword 热搜词 |
||||
|
* @return - |
||||
|
*/ |
||||
|
void incrementHotKeywordScore(String keyword); |
||||
|
|
||||
|
/** |
||||
|
* 新增热搜词 |
||||
|
* @param keyword 热搜词 |
||||
|
*/ |
||||
|
void insertHotKeyword(String keyword); |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
package com.bnyer.img.service.impl; |
||||
|
|
||||
|
import com.bnyer.common.core.constant.RedisKeyConstant; |
||||
|
import com.bnyer.common.redis.service.RedisService; |
||||
|
import com.bnyer.img.service.HotSearchKeywordService; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.data.redis.core.HashOperations; |
||||
|
import org.springframework.data.redis.core.ValueOperations; |
||||
|
import org.springframework.data.redis.core.ZSetOperations; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
@Service |
||||
|
@Slf4j |
||||
|
public class HotSearchKeywordServiceImpl implements HotSearchKeywordService { |
||||
|
|
||||
|
@Autowired |
||||
|
private RedisService redisService; |
||||
|
|
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public List<String> getHotKeywordList() { |
||||
|
Long now = System.currentTimeMillis(); |
||||
|
List<String> result = new ArrayList<>(); |
||||
|
ZSetOperations zSetOperations = redisService.redisTemplate.opsForZSet(); |
||||
|
HashOperations hashOperations = redisService.redisTemplate.opsForHash(); |
||||
|
Set<String> value = zSetOperations.reverseRangeByScore(RedisKeyConstant.HOT_KEY_WORD_KEY, 0, Double.MAX_VALUE); |
||||
|
//key不为空的时候 推荐相关的最热前十名
|
||||
|
for (String val : value) { |
||||
|
if (result.size() > 9) {//只返回最热的前十名
|
||||
|
break; |
||||
|
} |
||||
|
Long time = Long.valueOf((String) hashOperations.get(RedisKeyConstant.HOT_KEY_WORD_TIME_KEY, val)); |
||||
|
if ((now - time) < 2592000000L) {//返回最近一个月的数据
|
||||
|
result.add(val); |
||||
|
} else {//时间超过一个月没搜索就把这个词热度归0
|
||||
|
zSetOperations.add(RedisKeyConstant.HOT_KEY_WORD_KEY, val, 0); |
||||
|
} |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void incrementHotKeywordScore(String keyword) { |
||||
|
Long now = System.currentTimeMillis(); |
||||
|
ZSetOperations zSetOperations = redisService.redisTemplate.opsForZSet(); |
||||
|
HashOperations hashOperations = redisService.redisTemplate.opsForHash(); |
||||
|
zSetOperations.incrementScore(RedisKeyConstant.HOT_KEY_WORD_KEY, keyword, 1); |
||||
|
hashOperations.put(RedisKeyConstant.HOT_KEY_WORD_TIME_KEY,keyword,String.valueOf(now)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void insertHotKeyword(String keyword) { |
||||
|
Long now = System.currentTimeMillis(); |
||||
|
ZSetOperations zSetOperations = redisService.redisTemplate.opsForZSet(); |
||||
|
HashOperations hashOperations = redisService.redisTemplate.opsForHash(); |
||||
|
zSetOperations.incrementScore(RedisKeyConstant.HOT_KEY_WORD_KEY, keyword, 1); |
||||
|
hashOperations.put(RedisKeyConstant.HOT_KEY_WORD_TIME_KEY,keyword,String.valueOf(now)); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue