|
|
@@ -43,6 +43,11 @@ class LNProfileManager {
|
|
|
|
|
|
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 var captchaRemain = 0
|
|
|
private var captchaTimer: Timer?
|
|
|
@@ -126,7 +131,16 @@ extension LNProfileManager {
|
|
|
guard let self else { return }
|
|
|
if let res, err == nil {
|
|
|
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)
|
|
|
}
|
|
|
}
|
|
|
@@ -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 {
|
|
|
func getBindPhoneCaptcha(code: String, phone: String,
|
|
|
queue: DispatchQueue = .main,
|