Эх сурвалжийг харах

Revert "feat: 移除原用户缓存逻辑"

This reverts commit 42519fc5e53932916aedbccdf5df5f38ad41e95e.
陈文艺 1 өдөр өмнө
parent
commit
25c503f6df

+ 94 - 1
Lanu/Manager/Profile/LNProfileManager.swift

@@ -43,6 +43,11 @@ class LNProfileManager {
     
     
     private var randomProfile: LNRandomProfileResponse?
     private var randomProfile: LNRandomProfileResponse?
     
     
+    private static let profileCacheLimit = 300
+    private var profileCache: [String: LNProfileUserInfo] = [:]
+    private var profileCacheOrder: [String] = []
+    private let profileCacheLock = NSLock()
+    
     private let captchaCoolDown = 60
     private let captchaCoolDown = 60
     private var captchaRemain = 0
     private var captchaRemain = 0
     private var captchaTimer: Timer?
     private var captchaTimer: Timer?
@@ -126,7 +131,16 @@ extension LNProfileManager {
             guard let self else { return }
             guard let self else { return }
             if let res, err == nil {
             if let res, err == nil {
                 res.list.forEach {
                 res.list.forEach {
-                    if !$0.userNo.isMyUid || self.myUserInfo.update($0) {
+                    if $0.userNo.isMyUid {
+                        if self.myUserInfo.update($0) {
+                            self.notifyUserInfoChanged(newInfo: $0)
+                        }
+                    } else {
+                        self.updateProfileCache(
+                            uid: $0.userNo,
+                            name: $0.nickname,
+                            avatar: $0.avatar
+                        )
                         self.notifyUserInfoChanged(newInfo: $0)
                         self.notifyUserInfoChanged(newInfo: $0)
                     }
                     }
                 }
                 }
@@ -173,6 +187,85 @@ extension LNProfileManager {
     }
     }
 }
 }
 
 
+extension LNProfileManager {
+    func getCachedProfileUserInfo(uid: String) -> LNProfileUserInfo? {
+        profileCacheLock.lock()
+        defer { profileCacheLock.unlock() }
+        guard let info = profileCache[uid] else {
+            return nil
+        }
+        touchProfileCacheKey(uid)
+        return info
+    }
+    
+    func getCachedProfileUserInfo(uid: String,
+                                  fetchIfNeeded: Bool = true,
+                                  queue: DispatchQueue = .main,
+                                  handler: @escaping (LNProfileUserInfo?) -> Void) {
+        if let info = getCachedProfileUserInfo(uid: uid) {
+            queue.asyncIfNotGlobal {
+                handler(info)
+            }
+            return
+        }
+        
+        guard fetchIfNeeded else {
+            queue.asyncIfNotGlobal {
+                handler(nil)
+            }
+            return
+        }
+        
+        getUserProfileDetail(uid: uid, queue: queue) { [weak self] profile in
+            guard let self, let profile else {
+                handler(nil)
+                return
+            }
+            
+            self.updateProfileCache(
+                uid: profile.userNo,
+                name: profile.nickname,
+                avatar: profile.avatar
+            )
+            handler(self.getCachedProfileUserInfo(uid: uid))
+        }
+    }
+    
+    func updateProfileCache(uid: String,
+                            name: String,
+                            avatar: String) {
+        guard !uid.isEmpty else {
+            return
+        }
+        
+        profileCacheLock.lock()
+        defer { profileCacheLock.unlock() }
+        
+        let info = profileCache[uid] ?? LNProfileUserInfo()
+        info.uid = uid
+        info.name = name
+        info.avatar = avatar
+        
+        profileCache[uid] = info
+        touchProfileCacheKey(uid)
+        trimProfileCacheIfNeeded()
+    }
+    
+    private func touchProfileCacheKey(_ uid: String) {
+        if let index = profileCacheOrder.firstIndex(of: uid) {
+            profileCacheOrder.remove(at: index)
+        }
+        profileCacheOrder.append(uid)
+    }
+    
+    private func trimProfileCacheIfNeeded() {
+        while profileCacheOrder.count > Self.profileCacheLimit {
+            let expiredUid = profileCacheOrder.removeFirst()
+            profileCache.removeValue(forKey: expiredUid)
+        }
+    }
+}
+
 extension LNProfileManager {
 extension LNProfileManager {
     func getBindPhoneCaptcha(code: String, phone: String,
     func getBindPhoneCaptcha(code: String, phone: String,
                              queue: DispatchQueue = .main,
                              queue: DispatchQueue = .main,

+ 4 - 3
Lanu/Views/Room/Base/Views/Gift/LNRoomGiftHeaderView.swift

@@ -285,10 +285,11 @@ private class LNRoomGiftSpecifiedUserView: UIView {
             return
             return
         }
         }
         
         
-        LNProfileManager.shared.getUserProfileDetail(uid: uid) { [weak self] info in
+        LNProfileManager.shared.getCachedProfileUserInfo(uid: uid, fetchIfNeeded: true)
+        { [weak self] info in
             guard let self else { return }
             guard let self else { return }
-            guard let info, info.userNo == curUid else { return }
-            nameLabel.text = info.nickname
+            guard let info, info.uid == curUid else { return }
+            nameLabel.text = info.name
             avatar.sd_setImage(with: URL(string: info.avatar))
             avatar.sd_setImage(with: URL(string: info.avatar))
         }
         }
     }
     }

+ 10 - 2
Lanu/Views/Room/OrderRoom/Seats/LNOrderRoomUserSeatView.swift

@@ -226,8 +226,16 @@ extension LNOrderRoomUserSeatView: LNRoomViewModelNotify {
         } else {
         } else {
             userView.isHidden = false
             userView.isHidden = false
             
             
-            userAvatar.sd_setImage(with: URL(string: curSeat.avatar))
-            nameLabel.text = curSeat.nickname
+            if !curSeat.nickname.isEmpty {
+                userAvatar.sd_setImage(with: URL(string: curSeat.avatar))
+                nameLabel.text = curSeat.nickname
+            } else {
+                LNProfileManager.shared.getCachedProfileUserInfo(uid: curSeat.uid) { [weak self] info in
+                    guard let self, let info, info.uid == curSeat.uid else { return }
+                    userAvatar.sd_setImage(with: URL(string: info.avatar))
+                    nameLabel.text = info.name
+                }
+            }
         }
         }
         
         
         muteIc.isHidden = !curSeat.isMute
         muteIc.isHidden = !curSeat.isMute

+ 7 - 0
Lanu/Views/Room/OrderRoom/ViewModel/LNOrderRoomViewModel.swift

@@ -45,6 +45,9 @@ extension LNOrderRoomViewModel {
             runOnMain {
             runOnMain {
                 handler(res)
                 handler(res)
             }
             }
+            res?.list.forEach {
+                LNProfileManager.shared.updateProfileCache(uid: $0.user.userNo, name: $0.user.nickname, avatar: $0.user.avatar)
+            }
             if let err {
             if let err {
                 showToast(err.errorDesc)
                 showToast(err.errorDesc)
             }
             }
@@ -75,6 +78,10 @@ extension LNOrderRoomViewModel {
                 handler(res)
                 handler(res)
             }
             }
             
             
+            res?.list.forEach {
+                LNProfileManager.shared.updateProfileCache(uid: $0.user.userNo, name: $0.user.nickname, avatar: $0.user.avatar)
+            }
+            
             if let err {
             if let err {
                 showToast(err.errorDesc)
                 showToast(err.errorDesc)
             }
             }