因为项目中需要做的人性化,将用户的历史搜索记录给保存下来, 以下是我设计的开发方案,只是简单的根据用户搜索的先后顺序,去重然后留存十个记录。 用到的是redis中list数据结构,通过push,pop操作保证集合中数据是最新的。 代码实现如下:
- 保存,我使用了springboot自带的线程池来异步保存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
private final int SEARCH_HISTORY_LENGTH = 10;
@Async(value = "taskExecutor")
@Override
public ServiceResult saveSearchHistory(SearchHistory searchHistory) {
ServiceResult serviceResult = ServiceResult.serviceResultInstance();
try {
String key = concatSearchHistoryKey(searchHistory.getUserId(), searchHistory.getSearchType());
String searchValue = searchHistory.getSearchValue();
List<String> searchHistoryValues = redisUtils.lRange(key, 0, SEARCH_HISTORY_LENGTH);
//如果历史记录中已有,将该记录挪出,然后放到集合首位
if (searchHistoryValues.contains(searchValue)) {
//从list中移除,然后添加
redisUtils.lRem(key, 0, searchValue);
}
Long length = redisUtils.lLen(key);
if (length >= SEARCH_HISTORY_LENGTH) {
//保存redis中只会保存10个搜索记录
redisUtils.rpop(key);
}
redisUtils.lLeftPush(key, searchValue);
searchHistoryMapper.insert(searchHistory);
} catch (Exception e) {
log.error("saveSearchHistory fail ", e);
ServiceResult.renderFailResult(serviceResult, AppCode.SAVE_SEARCH_HISTORY_FAIL, e);
}
return serviceResult;
}
private String concatSearchHistoryKey(String userId, String searchType) {
return RedisKeyConstant.SEARCH_HISTORY + userId + RedisKeyConstant.KEY_SEPERATOR + searchType;
}
- 查询列表,先从缓存中获取,不存在查询db,将结果保存至redis
1 2 3 4 5 6 7 8 9
String key = concatSearchHistoryKey(userId, searchType); List<String> searchHistoryValues = redisUtils.lRange(key, 0, SEARCH_HISTORY_LENGTH); if (searchHistoryValues.isEmpty()) { searchHistoryValues = searchHistoryService.querySearchHistory(userId, searchType); redisUtils.lLeftPushAll(key, searchHistoryValues); viewResult.setData(searchHistoryValues); return viewResult; } viewResult.setData(searchHistoryValues);