热搜和最近搜索

controller层


/**
 * 热搜及最近搜索
 * 2022-10-26
 * jwb
 */
@RequestMapping("/app/serach")
@RestController
public class HotSerachController {

	@Resource
	private HotSearchService hotSearchService;



	/**
	 * 搜索
	 *
	 */
	@GetMapping("/search")
	public List listProduct(String hhxsUserId, String searchKeywords) {
		return hotSearchService.search(hhxsUserId,searchKeywords);
	}

	/**
	 * 删除搜索记录
	 *
	 */
	@GetMapping("/deleteSearch")
	public boolean deleteSearch(String hhxsUserId) {
		return hotSearchService.deleteSearch(hhxsUserId);
	}

}

service层


import java.util.List;


public interface HotSearchService {

	List search(String hhxsUserId, String searchKeywords);


	boolean deleteSearch(String hhxsUserId);
}

serviceImpl层


import com.mysql.cj.util.StringUtils;
import org.jmis.riskassess.service.HotSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.time.Instant;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class HotSearchServiceImpl implements HotSearchService {


	/**
	 * 最近搜索的key
	 */
	public static final String RECENT_SEARCH = "latelySearch";



	public static final String SEARCH_TITLE = "searchTitle";

	@Resource
	private RedisTemplate redisTemplate;
	/**
	 * 搜索
	 */
	public List search(String hhxsUserId, String searchKeywords) {
		List strings=null;
		if (!StringUtils.isNullOrEmpty(searchKeywords)) {
			addRedisRecentSearch(hhxsUserId, searchKeywords);
		}
			 strings = searchRecentList(hhxsUserId);

		return strings;
	}
	//删除搜索记录
	@Override
	public boolean deleteSearch(String hhxsUserId) {
		return  redisTemplate.delete(RECENT_SEARCH + hhxsUserId);
	}

	public void addRedisRecentSearch(String hhxsUserId,String searchKeywords) {
		ZSetOperations zSet = redisTemplate.opsForZSet();
		//由于zset 的集合特性当插入已经存在的 v 值 (搜索记录) 时只会更新score 值,
		zSet.add(RECENT_SEARCH+hhxsUserId, searchKeywords, Instant.now().getEpochSecond());
//		//获取到全部用户的最近搜索记录,用reverseRangeWithScores方法,可以获取到根据score排序之后的集合
//		Set> typedTuples = zSet.reverseRangeWithScores(RECENT_SEARCH, 0, -1);
	}

	//最近搜索添加权重
	public void addSearchCount(String searchKeywords) {
		String key = SEARCH_TITLE + searchKeywords;
		Boolean b = redisTemplate.hasKey(key);
		if(b){
			String count = (String)redisTemplate.opsForValue().get(key);
			redisTemplate.opsForValue().set(key,Long.valueOf(count)+1L);
		}else{
			redisTemplate.opsForValue().set(key, 1L);
		}
	}
	//热搜
	public List hotSearch(String searchKeywords){
		addSearchCount(searchKeywords);
		String key = SEARCH_TITLE + searchKeywords;
		ZSetOperations zSetOps = redisTemplate.opsForZSet();
		Set strings = zSetOps.reverseRange(key, 0, 9);
		List collect = strings.stream().collect(Collectors.toList());
		return collect;
	}

	public List searchRecentList(String userId) {
		String key=RECENT_SEARCH+userId;
		ZSetOperations zSetOps = redisTemplate.opsForZSet();
		Set strings = zSetOps.reverseRange(key, 0, 9);
		List collect = strings.stream().collect(Collectors.toList());
		return collect;
	}

}

你可能感兴趣的:(java,开发语言)