Merge branch 'jrose/nse-group-data-caches'

This commit is contained in:
Jordan Rose 2022-01-20 17:30:26 -08:00
commit a61bbdf7f3
2 changed files with 11 additions and 14 deletions

View File

@ -91,7 +91,12 @@ public extension GroupV2Params {
return try uuid(forUuidCiphertext: uuidCiphertext)
}
private static let decryptedUuidCache = LRUCache<Data, UUID>(maxSize: 256)
private static var maxGroupSize: Int {
return Int(RemoteConfig.groupsV2MaxGroupSizeHardLimit)
}
private static let decryptedUuidCache = LRUCache<Data, UUID>(maxSize: Self.maxGroupSize,
nseMaxSize: Self.maxGroupSize)
func uuid(forUuidCiphertext uuidCiphertext: UuidCiphertext) throws -> UUID {
let cacheKey = (uuidCiphertext.serialize().asData + groupSecretParamsData)
@ -117,7 +122,8 @@ public extension GroupV2Params {
return userId
}
private static let decryptedProfileKeyCache = LRUCache<Data, Data>(maxSize: 256)
private static let decryptedProfileKeyCache = LRUCache<Data, Data>(maxSize: Self.maxGroupSize,
nseMaxSize: Self.maxGroupSize)
func profileKey(forProfileKeyCiphertext profileKeyCiphertext: ProfileKeyCiphertext,
uuid: UUID) throws -> Data {

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2021 Open Whisper Systems. All rights reserved.
// Copyright (c) 2022 Open Whisper Systems. All rights reserved.
//
// An @objc wrapper around LRUCache.
@ -64,7 +64,6 @@ public class AnyLRUCache: NSObject {
public class LRUCache<KeyType: Hashable & Equatable, ValueType> {
private let cache = NSCache<AnyObject, AnyObject>()
private let maxSize: Int
private let _resetCount = AtomicUInt(0)
public var resetCount: UInt {
_resetCount.get()
@ -73,15 +72,7 @@ public class LRUCache<KeyType: Hashable & Equatable, ValueType> {
public init(maxSize: Int,
nseMaxSize: Int = 0,
shouldEvacuateInBackground: Bool = false) {
self.maxSize = {
if CurrentAppContext().isNSE {
let disableCacheInNSE = true
return disableCacheInNSE ? 0 : nseMaxSize
} else {
return maxSize
}
}()
self.cache.countLimit = maxSize
self.cache.countLimit = CurrentAppContext().isNSE ? nseMaxSize : maxSize
if CurrentAppContext().isMainApp,
shouldEvacuateInBackground {
@ -116,7 +107,7 @@ public class LRUCache<KeyType: Hashable & Equatable, ValueType> {
remove(key: key)
return
}
guard maxSize > 0 else {
guard cache.countLimit > 0 else {
Logger.verbose("Using disabled cache.")
return
}