代碼部分
package com.ruoyi.system.service.impl;import com.ruoyi.system.domain.Book;
import com.ruoyi.system.domain.MyOrder;
import com.ruoyi.system.mapper.BookMapper;
import com.ruoyi.system.mapper.MyOrderMapper;
import com.ruoyi.system.service.IBookRecommendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;@Service
public class BookRecommendServiceImpl implements IBookRecommendService {private static final Logger log = LoggerFactory.getLogger(BookRecommendServiceImpl.class);@Autowiredprivate MyOrderMapper orderMapper;@Autowiredprivate BookMapper bookMapper;@Autowiredprivate RedisTemplate<String, Object> redisTemplate;private static final String USER_SIMILARITY_KEY = "recommend:user:similarity";private static final double SIMILARITY_THRESHOLD = 0.000001; // 相似度閾值/*** 應用啟動時初始化推薦數據*/@PostConstructpublic void init() {log.info("檢查推薦數據初始化狀態...");try {if(!hasRecommendationData()) {log.info("未檢測到推薦數據,開始初始化計算...");preComputeUserSimilarities();} else {log.info("推薦數據已存在,跳過初始化計算");}} catch (Exception e) {log.error("推薦數據初始化失敗", e);}}/*** 檢查是否存在推薦數據*/private boolean hasRecommendationData() {Set<String> keys = redisTemplate.keys(USER_SIMILARITY_KEY + ":*");return keys != null && !keys.isEmpty();}@Override@Transactional(readOnly = true)public List<Book> recommendBooksByUserCF(Long userId, int limit) {if (userId == null || limit <= 0) {return Collections.emptyList();}try {// 1. 從Redis獲取用戶相似度數據Map<Object, Object> similarityScoresObj = redisTemplate.opsForHash().entries(USER_SIMILARITY_KEY + ":" + userId);if (similarityScoresObj == null || similarityScoresObj.isEmpty()) {log.debug("用戶 {} 無相似用戶數據", userId);return Collections.emptyList();}// 2. 轉換數據類型Map<Long, Double> similarityScores = convertSimilarityMap(similarityScoresObj);// 3. 獲取最相似的N個用戶List<Long> similarUserIds = getTopSimilarUsers(similarityScores, 10);if (similarUserIds.isEmpty()) {return Collections.emptyList();}// 4. 獲取推薦圖書return generateRecommendations(userId, similarUserIds, limit);} catch (Exception e) {log.error("為用戶 {} 生成推薦時發生錯誤", userId, e);return Collections.emptyList();}}/*** 轉換相似度Map數據類型*/private Map<Long, Double> convertSimilarityMap(Map<Object, Object> rawMap) {return rawMap.entrySet().stream().collect(Collectors.toMap(e -> Long.parseLong(e.getKey().toString()),e -> Double.parseDouble(e.getValue().toString())));}/*** 獲取最相似的用戶ID列表*/private List<Long> getTopSimilarUsers(Map<Long, Double> similarityScores, int topN) {return similarityScores.entrySet().stream().filter(e -> e.getValue() >= SIMILARITY_THRESHOLD).sorted(Map.Entry.<Long, Double>comparingByValue().reversed()).limit(topN).map(Map.Entry::getKey).collect(Collectors.toList());}/*** 生成推薦圖書列表*/private List<Book> generateRecommendations(Long targetUserId, List<Long> similarUserIds, int limit) {// 1. 獲取相似用戶訂單List<MyOrder> similarUserOrders = orderMapper.selectCompletedOrdersByUserIds(similarUserIds);// 2. 獲取目標用戶已購圖書Set<Long> purchasedBooks = getPurchasedBooks(targetUserId);// 3. 計算圖書推薦分數Map<Long, Double> bookScores = calculateBookScores(similarUserOrders, purchasedBooks);// 4. 獲取推薦圖書return getTopRecommendedBooks(bookScores, limit);}/*** 獲取用戶已購圖書ID集合*/private Set<Long> getPurchasedBooks(Long userId) {List<MyOrder> orders = orderMapper.selectCompletedOrdersByUserId(userId);if (orders == null || orders.isEmpty()) {return Collections.emptySet();}return orders.stream().map(order -> order.getBookId()).collect(Collectors.toSet());}/*** 計算圖書推薦分數*/private Map<Long, Double> calculateBookScores(List<MyOrder> similarUserOrders, Set<Long> purchasedBooks) {Map<Long, Double> bookScores = new HashMap<>();for (MyOrder order : similarUserOrders) {Long bookId = order.getBookId();if (!purchasedBooks.contains(bookId)) {bookScores.merge(bookId, (double) order.getQuantity(), Double::sum);}}return bookScores;}/*** 獲取評分最高的推薦圖書*/private List<Book> getTopRecommendedBooks(Map<Long, Double> bookScores, int limit) {if (bookScores.isEmpty()) {return Collections.emptyList();}List<Long> recommendedBookIds = bookScores.entrySet().stream().sorted(Map.Entry.<Long, Double>comparingByValue().reversed()).limit(limit).map(Map.Entry::getKey).collect(Collectors.toList());return bookMapper.selectBookByIds(recommendedBookIds);}@Override@Transactionalpublic void preComputeUserSimilarities() {log.info("開始計算用戶相似度矩陣...");long startTime = System.currentTimeMillis();try {// 1. 清空舊數據clearExistingSimilarityData();// 2. 獲取所有用戶ID(有完成訂單的)List<Long> userIds = orderMapper.selectAllUserIdsWithCompletedOrders();log.info("找到{}個有訂單的用戶", userIds.size());if (userIds.isEmpty()) {log.warn("沒有找到任何用戶訂單數據!");return;}// 3. 構建用戶-圖書評分矩陣Map<Long, Map<Long, Integer>> ratingMatrix = buildRatingMatrix(userIds);// 4. 計算并存儲相似度computeAndStoreSimilarities(userIds, ratingMatrix);long duration = (System.currentTimeMillis() - startTime) / 1000;log.info("用戶相似度矩陣計算完成,耗時{}秒", duration);} catch (Exception e) {log.error("計算用戶相似度矩陣失敗", e);throw e;}}/*** 清空現有相似度數據*/private void clearExistingSimilarityData() {Set<String> keys = redisTemplate.keys(USER_SIMILARITY_KEY + ":*");if (keys != null && !keys.isEmpty()) {redisTemplate.delete(keys);log.info("已清除{}個舊的用戶相似度記錄", keys.size());}}/*** 構建用戶-圖書評分矩陣*/private Map<Long, Map<Long, Integer>> buildRatingMatrix(List<Long> userIds) {Map<Long, Map<Long, Integer>> ratingMatrix = new HashMap<>();for (Long userId : userIds) {List<MyOrder> orders = orderMapper.selectCompletedOrdersByUserId(userId);if (orders == null || orders.isEmpty()) {continue;}Map<Long, Integer> userRatings = new HashMap<>();for (MyOrder order : orders) {if (order == null || order.getBookId() == null) {continue;}Long bookId = order.getBookId();Integer quantity = Math.toIntExact(order.getQuantity() != null ? order.getQuantity() : 0);userRatings.merge(bookId, quantity, (oldVal, newVal) -> oldVal + newVal);}ratingMatrix.put(userId, userRatings);}return ratingMatrix;}/*** 計算并存儲用戶相似度*/private void computeAndStoreSimilarities(List<Long> userIds, Map<Long, Map<Long, Integer>> ratingMatrix) {int computedPairs = 0;for (int i = 0; i < userIds.size(); i++) {Long userId1 = userIds.get(i);Map<Long, Integer> ratings1 = ratingMatrix.get(userId1);Map<String, String> similarities = new HashMap<>();// 只計算后續用戶,避免重復計算for (int j = i + 1; j < userIds.size(); j++) {Long userId2 = userIds.get(j);Map<Long, Integer> ratings2 = ratingMatrix.get(userId2);double similarity = computeCosineSimilarity(ratings1, ratings2);if (similarity >= SIMILARITY_THRESHOLD) {similarities.put(userId2.toString(), String.valueOf(similarity));computedPairs++;}}if (!similarities.isEmpty()) {String key = USER_SIMILARITY_KEY + ":" + userId1;redisTemplate.opsForHash().putAll(key, similarities);redisTemplate.expire(key, 7, TimeUnit.DAYS);}// 定期打印進度if (i % 100 == 0 || i == userIds.size() - 1) {log.info("已處理 {}/{} 用戶", i + 1, userIds.size());}}log.info("共計算{}對用戶相似關系", computedPairs);}/*** 計算余弦相似度*/private double computeCosineSimilarity(Map<Long, Integer> ratings1, Map<Long, Integer> ratings2) {// 獲取共同評價的圖書Set<Long> commonBooks = new HashSet<>(ratings1.keySet());commonBooks.retainAll(ratings2.keySet());if (commonBooks.isEmpty()) {return 0.0;}// 計算點積和范數double dotProduct = 0.0;double norm1 = 0.0;double norm2 = 0.0;// 計算共同圖書的部分for (Long bookId : commonBooks) {int r1 = ratings1.get(bookId);int r2 = ratings2.get(bookId);dotProduct += r1 * r2;norm1 += r1 * r1;norm2 += r2 * r2;}// 計算各自獨有的部分norm1 += ratings1.keySet().stream().filter(bookId -> !commonBooks.contains(bookId)).mapToDouble(bookId -> Math.pow(ratings1.get(bookId), 2)).sum();norm2 += ratings2.keySet().stream().filter(bookId -> !commonBooks.contains(bookId)).mapToDouble(bookId -> Math.pow(ratings2.get(bookId), 2)).sum();if (norm1 == 0 || norm2 == 0) {return 0.0;}return dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2));}/*** 定時任務:每天凌晨3點更新相似度矩陣*/@Scheduled(cron = "0 0 3 * * ?")public void scheduledSimilarityUpdate() {log.info("定時任務:開始更新用戶相似度矩陣...");try {preComputeUserSimilarities();log.info("定時任務:用戶相似度矩陣更新完成");} catch (Exception e) {log.error("定時任務:更新用戶相似度矩陣失敗", e);}}
}
協同過濾解析
協同過濾推薦算法原理與實現分析
這個代碼實現了一個基于用戶的協同過濾(User-based Collaborative Filtering)推薦系統,下面我將結合代碼詳細解析協同過濾算法的原理和實現方式。
1. 協同過濾基本概念
協同過濾(CF)是推薦系統中最經典的算法之一,核心思想是:
“相似用戶喜歡的物品,你也可能喜歡”
1.1 兩種主要類型
- 基于用戶的協同過濾(User-CF):代碼中實現的就是這種
- 基于物品的協同過濾(Item-CF)
2. 代碼實現的User-CF流程
2.1 整體流程
-
離線計算階段:
- 構建用戶-物品評分矩陣
- 計算并存儲用戶相似度
-
在線推薦階段:
- 查找相似用戶
- 基于相似用戶的偏好生成推薦
2.2 核心代碼解析
(1) 離線計算階段 (preComputeUserSimilarities
)
public void preComputeUserSimilarities() {// 1. 清空舊數據clearExistingSimilarityData();// 2. 獲取所有用戶IDList<Long> userIds = orderMapper.selectAllUserIdsWithCompletedOrders();// 3. 構建評分矩陣Map<Long, Map<Long, Integer>> ratingMatrix = buildRatingMatrix(userIds);// 4. 計算并存儲相似度computeAndStoreSimilarities(userIds, ratingMatrix);
}
評分矩陣構建:
- 用戶為行,圖書為列
- 值為購買數量(作為評分)
相似度計算:
- 使用余弦相似度(Cosine Similarity)
- 只存儲相似度高于閾值(SIMILARITY_THRESHOLD)的關系
(2) 相似度計算 (computeCosineSimilarity
)
private double computeCosineSimilarity(Map<Long, Integer> ratings1, Map<Long, Integer> ratings2) {// 獲取共同評價的圖書Set<Long> commonBooks = new HashSet<>(ratings1.keySet());commonBooks.retainAll(ratings2.keySet());// 計算點積和范數double dotProduct = 0.0;double norm1 = 0.0;double norm2 = 0.0;// 計算余弦相似度return dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2));
}
余弦相似度公式:
similarity = (A·B) / (||A|| * ||B||)
(3) 在線推薦階段 (recommendBooksByUserCF
)
public List<Book> recommendBooksByUserCF(Long userId, int limit) {// 1. 獲取用戶相似度數據Map<Long, Double> similarityScores = getSimilarityScores(userId);// 2. 獲取最相似的N個用戶List<Long> similarUserIds = getTopSimilarUsers(similarityScores, 10);// 3. 生成推薦return generateRecommendations(userId, similarUserIds, limit);
}
3. 關鍵技術點
3.1 數據存儲優化
- Redis存儲:用戶相似度矩陣存儲在Redis中,鍵格式為
recommend:user:similarity:{userId}
- 定期更新:通過
@Scheduled
每天凌晨3點更新
3.2 性能優化
- 只計算上三角矩陣:避免重復計算(A-B和B-A的相似度相同)
- 閾值過濾:只存儲有意義的相似關系(SIMILARITY_THRESHOLD)
- 分批處理:每處理100個用戶打印進度
3.3 推薦生成邏輯
- 排除用戶已購買的圖書
- 根據相似用戶的購買記錄加權計算推薦分數
- 按分數排序返回TopN推薦
4. 協同過濾的優缺點
優點:
- 不需要物品內容信息:僅依賴用戶行為數據
- 能發現潛在興趣:可能推薦出意想不到的物品
- 解釋性強:可以告訴用戶"因為和你相似的用戶也喜歡"
缺點:
- 冷啟動問題:新用戶/新物品缺乏數據
- 稀疏性問題:用戶-物品矩陣通常非常稀疏
- 計算復雜度高:用戶增長時相似度計算成本呈平方級增長
5. 代碼中的工程化實踐
- 定時任務:
@Scheduled
定期更新相似度矩陣 - 啟動初始化:
@PostConstruct
檢查數據是否需要初始化 - 異常處理:完善的try-catch和日志記錄
- 性能監控:記錄關鍵步驟的執行時間
- 資源清理:計算前先清除舊數據
這個實現是一個典型的生產級協同過濾推薦系統,平衡了算法效果和工程實踐,適合中等規模的電商平臺使用。對于更大規模的系統,可能需要考慮更復雜的優化如分布式計算、近似算法等。