From 73f5bc30506161036ba2b8a10caa25cdfe79bef1 Mon Sep 17 00:00:00 2001 From: Michael Redig Date: Fri, 5 Aug 2022 18:57:41 -0500 Subject: [PATCH 1/3] (refactor) refactored MXLRUCache to swift --- MatrixSDK.xcodeproj/project.pbxproj | 18 +-- MatrixSDK/MatrixSDK.h | 2 - MatrixSDK/Utils/MXLRUCache.h | 50 -------- MatrixSDK/Utils/MXLRUCache.m | 168 ------------------------- MatrixSDK/Utils/MXLRUCache.swift | 83 ++++++++++++ MatrixSDK/Utils/Media/MXMediaManager.m | 3 +- 6 files changed, 91 insertions(+), 233 deletions(-) delete mode 100644 MatrixSDK/Utils/MXLRUCache.h delete mode 100644 MatrixSDK/Utils/MXLRUCache.m create mode 100644 MatrixSDK/Utils/MXLRUCache.swift diff --git a/MatrixSDK.xcodeproj/project.pbxproj b/MatrixSDK.xcodeproj/project.pbxproj index 88b2d6d746..dc9dd2144c 100644 --- a/MatrixSDK.xcodeproj/project.pbxproj +++ b/MatrixSDK.xcodeproj/project.pbxproj @@ -651,6 +651,8 @@ 32FE41371D0AB7070060835E /* MXEnumConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 32FE41351D0AB7070060835E /* MXEnumConstants.m */; }; 32FFB4F0217E146A00C96002 /* MXRecoveryKey.h in Headers */ = {isa = PBXBuildFile; fileRef = 32FFB4EE217E146A00C96002 /* MXRecoveryKey.h */; }; 32FFB4F1217E146A00C96002 /* MXRecoveryKey.m in Sources */ = {isa = PBXBuildFile; fileRef = 32FFB4EF217E146A00C96002 /* MXRecoveryKey.m */; }; + 33C2FCD7289DD3EA00C83E31 /* MXLRUCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33C2FCD6289DD3EA00C83E31 /* MXLRUCache.swift */; }; + 33C2FCD8289DD3EA00C83E31 /* MXLRUCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33C2FCD6289DD3EA00C83E31 /* MXLRUCache.swift */; }; 3489B40A85709695DC4EB17C /* Pods_MatrixSDK_MatrixSDK_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F669A7EDB909643AE4F39F80 /* Pods_MatrixSDK_MatrixSDK_iOS.framework */; }; 3A0AF06B2705A11400679D1A /* MXSpaceGraphData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A0AF06A2705A11400679D1A /* MXSpaceGraphData.swift */; }; 3A0AF06C2705A11400679D1A /* MXSpaceGraphData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A0AF06A2705A11400679D1A /* MXSpaceGraphData.swift */; }; @@ -976,7 +978,6 @@ B14EF24C2397E90400758AF0 /* MXCredentials.m in Sources */ = {isa = PBXBuildFile; fileRef = 323547DB2226FC5700F15F94 /* MXCredentials.m */; }; B14EF24D2397E90400758AF0 /* MXOutgoingRoomKeyRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 32FA10C91FA1C9F700E54233 /* MXOutgoingRoomKeyRequest.m */; }; B14EF24E2397E90400758AF0 /* MXAllowedCertificates.m in Sources */ = {isa = PBXBuildFile; fileRef = 32322A4A1E575F65005DD155 /* MXAllowedCertificates.m */; }; - B14EF24F2397E90400758AF0 /* MXLRUCache.m in Sources */ = {isa = PBXBuildFile; fileRef = F03EF5031DF01596009DF592 /* MXLRUCache.m */; }; B14EF2502397E90400758AF0 /* MXIncomingRoomKeyRequestManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A30B171FB4813400C8309E /* MXIncomingRoomKeyRequestManager.m */; }; B14EF2512397E90400758AF0 /* MXRoomEventFilter.m in Sources */ = {isa = PBXBuildFile; fileRef = 323F3F9120D3F0C700D26D6A /* MXRoomEventFilter.m */; }; B14EF2522397E90400758AF0 /* MXLoginPolicyData.m in Sources */ = {isa = PBXBuildFile; fileRef = 3275FD9721A6B53300B9C13D /* MXLoginPolicyData.m */; }; @@ -1116,7 +1117,6 @@ B14EF2E22397E90400758AF0 /* MX3PidAddManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 32D2CC0023422462002BD8CA /* MX3PidAddManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; B14EF2E42397E90400758AF0 /* MXStore.h in Headers */ = {isa = PBXBuildFile; fileRef = 32114A841A262CE000FF2EC4 /* MXStore.h */; settings = {ATTRIBUTES = (Public, ); }; }; B14EF2E62397E90400758AF0 /* MXMegolmEncryption.h in Headers */ = {isa = PBXBuildFile; fileRef = 32A151371DAD292400400192 /* MXMegolmEncryption.h */; }; - B14EF2E72397E90400758AF0 /* MXLRUCache.h in Headers */ = {isa = PBXBuildFile; fileRef = F03EF5021DF01596009DF592 /* MXLRUCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; B14EF2E92397E90400758AF0 /* MXTools.h in Headers */ = {isa = PBXBuildFile; fileRef = 329FB1771A0A74B100A5E88E /* MXTools.h */; settings = {ATTRIBUTES = (Public, ); }; }; B14EF2EA2397E90400758AF0 /* MXDeviceListOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 322691301E5EF77D00966A6E /* MXDeviceListOperation.h */; }; B14EF2EB2397E90400758AF0 /* MXRoomAccountData.h in Headers */ = {isa = PBXBuildFile; fileRef = 32481A821C03572900782AD3 /* MXRoomAccountData.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -1849,8 +1849,6 @@ F03EF4FF1DF014D9009DF592 /* MXMediaLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = F03EF4FB1DF014D9009DF592 /* MXMediaLoader.m */; }; F03EF5001DF014D9009DF592 /* MXMediaManager.h in Headers */ = {isa = PBXBuildFile; fileRef = F03EF4FC1DF014D9009DF592 /* MXMediaManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; F03EF5011DF014D9009DF592 /* MXMediaManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F03EF4FD1DF014D9009DF592 /* MXMediaManager.m */; }; - F03EF5041DF01596009DF592 /* MXLRUCache.h in Headers */ = {isa = PBXBuildFile; fileRef = F03EF5021DF01596009DF592 /* MXLRUCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; - F03EF5051DF01596009DF592 /* MXLRUCache.m in Sources */ = {isa = PBXBuildFile; fileRef = F03EF5031DF01596009DF592 /* MXLRUCache.m */; }; F03EF5081DF071D5009DF592 /* MXEncryptedAttachments.h in Headers */ = {isa = PBXBuildFile; fileRef = F03EF5061DF071D5009DF592 /* MXEncryptedAttachments.h */; settings = {ATTRIBUTES = (Public, ); }; }; F03EF5091DF071D5009DF592 /* MXEncryptedAttachments.m in Sources */ = {isa = PBXBuildFile; fileRef = F03EF5071DF071D5009DF592 /* MXEncryptedAttachments.m */; }; F082946D1DB66C3D00CEAB63 /* MXInvite3PID.h in Headers */ = {isa = PBXBuildFile; fileRef = F082946B1DB66C3D00CEAB63 /* MXInvite3PID.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -2412,6 +2410,7 @@ 32FFB4ED217DC0E900C96002 /* MXKeyBackup_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXKeyBackup_Private.h; sourceTree = ""; }; 32FFB4EE217E146A00C96002 /* MXRecoveryKey.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXRecoveryKey.h; sourceTree = ""; }; 32FFB4EF217E146A00C96002 /* MXRecoveryKey.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXRecoveryKey.m; sourceTree = ""; }; + 33C2FCD6289DD3EA00C83E31 /* MXLRUCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXLRUCache.swift; sourceTree = ""; }; 3A0AF06A2705A11400679D1A /* MXSpaceGraphData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXSpaceGraphData.swift; sourceTree = ""; }; 3A108A392580E9C2005EEBE9 /* MXKeyProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXKeyProvider.h; sourceTree = ""; }; 3A108A7C25810C96005EEBE9 /* MXKeyData.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXKeyData.h; sourceTree = ""; }; @@ -2866,8 +2865,6 @@ F03EF4FB1DF014D9009DF592 /* MXMediaLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXMediaLoader.m; sourceTree = ""; }; F03EF4FC1DF014D9009DF592 /* MXMediaManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXMediaManager.h; sourceTree = ""; }; F03EF4FD1DF014D9009DF592 /* MXMediaManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXMediaManager.m; sourceTree = ""; }; - F03EF5021DF01596009DF592 /* MXLRUCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXLRUCache.h; sourceTree = ""; }; - F03EF5031DF01596009DF592 /* MXLRUCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXLRUCache.m; sourceTree = ""; }; F03EF5061DF071D5009DF592 /* MXEncryptedAttachments.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXEncryptedAttachments.h; sourceTree = ""; }; F03EF5071DF071D5009DF592 /* MXEncryptedAttachments.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXEncryptedAttachments.m; sourceTree = ""; }; F082946B1DB66C3D00CEAB63 /* MXInvite3PID.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXInvite3PID.h; sourceTree = ""; }; @@ -3106,8 +3103,7 @@ 189CC080265E361500BE1FD8 /* MXLog.swift */, 181FD5D22660C791008EC084 /* MXLogObjcWrapper.h */, 181FD5D32660C791008EC084 /* MXLogObjcWrapper.m */, - F03EF5021DF01596009DF592 /* MXLRUCache.h */, - F03EF5031DF01596009DF592 /* MXLRUCache.m */, + 33C2FCD6289DD3EA00C83E31 /* MXLRUCache.swift */, 324676EB25C15F4600EA855B /* MXMemory.swift */, ECE3DF9C270C660900FB4C96 /* MXMulticastDelegate.swift */, 32A9770221626E5C00919CC0 /* MXServerNotices.h */, @@ -5261,7 +5257,6 @@ 3AC13802264482A100EE1E74 /* MXExportedOlmDevice.h in Headers */, 32A151391DAD292400400192 /* MXMegolmEncryption.h in Headers */, EC8A538B25B1BC77004E0802 /* MXCallCandidate.h in Headers */, - F03EF5041DF01596009DF592 /* MXLRUCache.h in Headers */, EC8A539D25B1BC77004E0802 /* MXCallCandidatesEventContent.h in Headers */, ED88999127F2065D00718486 /* MXRoomAliasResolution.h in Headers */, 18B22A7027707CDD00482170 /* MXEventContentLocation.h in Headers */, @@ -5643,7 +5638,6 @@ 32AF928B240EA3880008A0FD /* MXSecretShareSend.h in Headers */, B14EF2E62397E90400758AF0 /* MXMegolmEncryption.h in Headers */, EC8A53E125B1BCC6004E0802 /* MXThirdPartyProtocolInstance.h in Headers */, - B14EF2E72397E90400758AF0 /* MXLRUCache.h in Headers */, B105CDD7261F54C8006EB204 /* MXSpaceChildContent.h in Headers */, 32AF929824115D8B0008A0FD /* MXPendingSecretShareRequest.h in Headers */, B14EF2E92397E90400758AF0 /* MXTools.h in Headers */, @@ -6435,7 +6429,6 @@ EC0B9431271D95CC00B4D440 /* MXFileRoomSummaryStore.m in Sources */, 32322A4C1E575F65005DD155 /* MXAllowedCertificates.m in Sources */, 18C26C3D273C031900805154 /* PollAggregator.swift in Sources */, - F03EF5051DF01596009DF592 /* MXLRUCache.m in Sources */, EC8A53AF25B1BC77004E0802 /* MXCallAnswerEventContent.m in Sources */, B1432B51282AB29A00737CA6 /* MXBeaconInfoSummaryAllRoomListener.swift in Sources */, 32A30B191FB4813400C8309E /* MXIncomingRoomKeyRequestManager.m in Sources */, @@ -6507,6 +6500,7 @@ 32B090E2261F709B002924AA /* MXAsyncTaskQueue.swift in Sources */, 322691371E5EFF8700966A6E /* MXDeviceListOperationsPool.m in Sources */, C6F935801E5B3ACA00FC34BF /* MXResponse.swift in Sources */, + 33C2FCD7289DD3EA00C83E31 /* MXLRUCache.swift in Sources */, ECDA764827BA939E000C48CF /* MXRoomVersionsCapability.m in Sources */, 32EEA8632604014A0041425B /* MXSummable.swift in Sources */, 3A108AB725812995005EEBE9 /* MXKeyProvider.m in Sources */, @@ -6973,7 +6967,6 @@ EC116590270F3ABF0089FA56 /* RLMRealm+MatrixSDK.m in Sources */, B14EF24D2397E90400758AF0 /* MXOutgoingRoomKeyRequest.m in Sources */, B14EF24E2397E90400758AF0 /* MXAllowedCertificates.m in Sources */, - B14EF24F2397E90400758AF0 /* MXLRUCache.m in Sources */, B14EF2502397E90400758AF0 /* MXIncomingRoomKeyRequestManager.m in Sources */, ED2DD119286C450600F06731 /* MXCryptoRequests.swift in Sources */, B14EF2512397E90400758AF0 /* MXRoomEventFilter.m in Sources */, @@ -7076,6 +7069,7 @@ B14EF27C2397E90400758AF0 /* MXRestClient.swift in Sources */, 32AF929A24115D8B0008A0FD /* MXPendingSecretShareRequest.m in Sources */, B14EF27D2397E90400758AF0 /* MXKeyBackup.m in Sources */, + 33C2FCD8289DD3EA00C83E31 /* MXLRUCache.swift in Sources */, EC1165BD27107E330089FA56 /* MXRoomListDataFetchOptions.swift in Sources */, ECDA764927BA939E000C48CF /* MXRoomVersionsCapability.m in Sources */, B14EF27E2397E90400758AF0 /* MXCrypto.m in Sources */, diff --git a/MatrixSDK/MatrixSDK.h b/MatrixSDK/MatrixSDK.h index d9a130bb09..25d820550a 100644 --- a/MatrixSDK/MatrixSDK.h +++ b/MatrixSDK/MatrixSDK.h @@ -56,8 +56,6 @@ FOUNDATION_EXPORT NSString *MatrixSDKVersion; #import "MXMediaManager.h" -#import "MXLRUCache.h" - #import "MXCallStack.h" #import "MXCrypto.h" diff --git a/MatrixSDK/Utils/MXLRUCache.h b/MatrixSDK/Utils/MXLRUCache.h deleted file mode 100644 index 6b9acc9472..0000000000 --- a/MatrixSDK/Utils/MXLRUCache.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - Copyright 2016 OpenMarket Ltd - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - */ - -#import - -/** - `MXLRUCache` is an LRU cache - */ -@interface MXLRUCache : NSObject - -/** - Create a LRU cache with a max number of cached object - @param capacity the maximum number of cached items - */ - -- (id)initWithCapacity:(NSUInteger)capacity; - -/** - Retrieve an object from its key. - @param key the object key - @return the cached object if it is found else nil - */ -- (NSObject*)get:(NSString*)key; - -/** - Put an object from its key. - @param key the object key - @param object the object to store - */ -- (void)put:(NSString*)key object:(NSObject*)object; - -/** - Clear the LRU cache. - */ -- (void)clear; - -@end diff --git a/MatrixSDK/Utils/MXLRUCache.m b/MatrixSDK/Utils/MXLRUCache.m deleted file mode 100644 index 116478c53c..0000000000 --- a/MatrixSDK/Utils/MXLRUCache.m +++ /dev/null @@ -1,168 +0,0 @@ -/* - Copyright 2016 OpenMarket Ltd - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - */ - -#import "MXLRUCache.h" - -@interface MXLRUCacheItem : NSObject - -/** - The item counter - */ -@property NSUInteger refCount; - -/** - The cached object - */ -@property NSObject* object; - -/** - The object key - */ -@property NSString* key; - -@end - -@implementation MXLRUCacheItem -@end - - -@interface MXLRUCache () -{ - // the cached objects list - // sorted by regCount - NSMutableArray *cachedObjects; - - // the cached objects keys - NSMutableArray *cachedKeys; - - // - NSUInteger capacity; -} -@end - -@implementation MXLRUCache - -- (id)initWithCapacity:(NSUInteger)aCapacity -{ - self = [super init]; - if (self) - { - capacity = aCapacity; - cachedObjects = [[NSMutableArray alloc] initWithCapacity:capacity]; - cachedKeys = [[NSMutableArray alloc] initWithCapacity:capacity]; - } - return self; -} - -- (void)sortCachedItems -{ - cachedObjects = [[cachedObjects sortedArrayUsingComparator:^NSComparisonResult(id a, id b) - { - MXLRUCacheItem* item1 = (MXLRUCacheItem*)a; - MXLRUCacheItem* item2 = (MXLRUCacheItem*)b; - - return item2.refCount - item1.refCount; - }] mutableCopy]; - - [cachedKeys removeAllObjects]; - - for(MXLRUCacheItem* item in cachedObjects) - { - [cachedKeys addObject:item.key]; - } -} - - -/** - Retrieve an object from its key. - @param key the object key - @return the cached object if it is found else nil - */ -- (NSObject*)get:(NSString*)key -{ - NSObject* object = nil; - - if (key) - { - @synchronized(cachedObjects) - { - NSUInteger pos = [cachedKeys indexOfObject:key]; - - if (pos != NSNotFound) - { - MXLRUCacheItem* item = [cachedObjects objectAtIndex:pos]; - - object = item.object; - - // update the count order - item.refCount++; - [self sortCachedItems]; - } - } - } - - return object; -} - -/** - Put an object from its key. - @param key the object key - @param object the object to store - */ -- (void)put:(NSString*)key object:(NSObject*)object -{ - if (key) - { - @synchronized(cachedObjects) - { - NSUInteger pos = [cachedKeys indexOfObject:key]; - - if (pos == NSNotFound) - { - MXLRUCacheItem* item = [[MXLRUCacheItem alloc] init]; - - item.object = object; - item.refCount = 1; - item.key = key; - - // remove the less used object - if (cachedKeys.count >= capacity) - { - [cachedObjects removeLastObject]; - [cachedKeys removeLastObject]; - - } - - [cachedObjects addObject:item]; - [cachedKeys addObject:key]; - } - } - } -} - -/** - Clear the LRU cache. - */ -- (void)clear -{ - @synchronized(cachedObjects) - { - [cachedObjects removeAllObjects]; - [cachedKeys removeAllObjects]; - } -} - -@end diff --git a/MatrixSDK/Utils/MXLRUCache.swift b/MatrixSDK/Utils/MXLRUCache.swift new file mode 100644 index 0000000000..f6ae7328b3 --- /dev/null +++ b/MatrixSDK/Utils/MXLRUCache.swift @@ -0,0 +1,83 @@ +// +// Copyright 2022 The Matrix.org Foundation C.I.C +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation + +@objcMembers +public class MXLRUCache: NSObject { + + private var cachedObjects: [LRUCacheItem] = [] + private let capacity: UInt + + private static let queue = DispatchQueue(label: "LRUCache") + + + public init(capacity: UInt) { + self.capacity = capacity + super.init() + } + + public func get(_ key: String) -> AnyObject? { + Self.queue.sync { + guard + let item = cachedObjects.first(where: { $0.key == key }) + else { return nil } + item.refCount += 1 + sortCachedItems() + return item + } + } + + public func put(_ key: String, object: AnyObject?) { + Self.queue.sync { + guard + cachedObjects.firstIndex(where: { $0.key == key }) == nil + else { return } + + let newItem = LRUCacheItem(object: object, key: key) + + if cachedObjects.count > capacity { + cachedObjects.removeLast() + } + + cachedObjects.append(newItem) + } + } + + public func clear() { + Self.queue.sync { + cachedObjects.removeAll() + } + } + + private func sortCachedItems() { + cachedObjects.sort { + $0.refCount > $1.refCount + } + } + + private class LRUCacheItem: NSObject { + var refCount: UInt = 1 + let object: AnyObject? + let key: String + + init(refCount: UInt = 1, object: AnyObject?, key: String) { + self.refCount = refCount + self.object = object + self.key = key + } + } +} diff --git a/MatrixSDK/Utils/Media/MXMediaManager.m b/MatrixSDK/Utils/Media/MXMediaManager.m index ef73ade893..52e5f1e1cc 100644 --- a/MatrixSDK/Utils/Media/MXMediaManager.m +++ b/MatrixSDK/Utils/Media/MXMediaManager.m @@ -28,7 +28,8 @@ #import "MXSDKOptions.h" -#import "MXLRUCache.h" +#import "MatrixSDKSwiftHeader.h" + #import "MXTools.h" NSUInteger const kMXMediaCacheSDKVersion = 3; From 01203f320968943da6e58d5243e9279fa5041e8c Mon Sep 17 00:00:00 2001 From: Michael Redig Date: Fri, 5 Aug 2022 19:01:12 -0500 Subject: [PATCH 2/3] (nit) tabs -> spaces cuz that's seemingly what the project uses --- MatrixSDK/Utils/MXLRUCache.swift | 98 ++++++++++++++++---------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/MatrixSDK/Utils/MXLRUCache.swift b/MatrixSDK/Utils/MXLRUCache.swift index f6ae7328b3..746222f0c7 100644 --- a/MatrixSDK/Utils/MXLRUCache.swift +++ b/MatrixSDK/Utils/MXLRUCache.swift @@ -19,65 +19,65 @@ import Foundation @objcMembers public class MXLRUCache: NSObject { - private var cachedObjects: [LRUCacheItem] = [] - private let capacity: UInt + private var cachedObjects: [LRUCacheItem] = [] + private let capacity: UInt - private static let queue = DispatchQueue(label: "LRUCache") + private static let queue = DispatchQueue(label: "LRUCache") - public init(capacity: UInt) { - self.capacity = capacity - super.init() - } + public init(capacity: UInt) { + self.capacity = capacity + super.init() + } - public func get(_ key: String) -> AnyObject? { - Self.queue.sync { - guard - let item = cachedObjects.first(where: { $0.key == key }) - else { return nil } - item.refCount += 1 - sortCachedItems() - return item - } - } + public func get(_ key: String) -> AnyObject? { + Self.queue.sync { + guard + let item = cachedObjects.first(where: { $0.key == key }) + else { return nil } + item.refCount += 1 + sortCachedItems() + return item + } + } - public func put(_ key: String, object: AnyObject?) { - Self.queue.sync { - guard - cachedObjects.firstIndex(where: { $0.key == key }) == nil - else { return } + public func put(_ key: String, object: AnyObject?) { + Self.queue.sync { + guard + cachedObjects.firstIndex(where: { $0.key == key }) == nil + else { return } - let newItem = LRUCacheItem(object: object, key: key) + let newItem = LRUCacheItem(object: object, key: key) - if cachedObjects.count > capacity { - cachedObjects.removeLast() - } + if cachedObjects.count > capacity { + cachedObjects.removeLast() + } - cachedObjects.append(newItem) - } - } + cachedObjects.append(newItem) + } + } - public func clear() { - Self.queue.sync { - cachedObjects.removeAll() - } - } + public func clear() { + Self.queue.sync { + cachedObjects.removeAll() + } + } - private func sortCachedItems() { - cachedObjects.sort { - $0.refCount > $1.refCount - } - } + private func sortCachedItems() { + cachedObjects.sort { + $0.refCount > $1.refCount + } + } - private class LRUCacheItem: NSObject { - var refCount: UInt = 1 - let object: AnyObject? - let key: String + private class LRUCacheItem: NSObject { + var refCount: UInt = 1 + let object: AnyObject? + let key: String - init(refCount: UInt = 1, object: AnyObject?, key: String) { - self.refCount = refCount - self.object = object - self.key = key - } - } + init(refCount: UInt = 1, object: AnyObject?, key: String) { + self.refCount = refCount + self.object = object + self.key = key + } + } } From e097cecb3b5bf6dc3880587b71859217c31edc0b Mon Sep 17 00:00:00 2001 From: Michael Redig Date: Fri, 5 Aug 2022 19:03:40 -0500 Subject: [PATCH 3/3] (nit) contribution stuff --- AUTHORS.rst | 4 +++- MatrixSDK.xcodeproj/project.pbxproj | 2 +- changelog.d/pr-1544.change | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 changelog.d/pr-1544.change diff --git a/AUTHORS.rst b/AUTHORS.rst index 53843f77fb..2816db5d8d 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -45,4 +45,6 @@ Pierre Dubois John Flanagan * PR #463 Fix compatibility with Swift 4.0 (issue #393) * PR #465 SwiftMatrixSDK: Enum cleanup - \ No newline at end of file + +Michael Redig + * PR #1544 refactored MXLRUCache to swift diff --git a/MatrixSDK.xcodeproj/project.pbxproj b/MatrixSDK.xcodeproj/project.pbxproj index dc9dd2144c..dfbe798aa1 100644 --- a/MatrixSDK.xcodeproj/project.pbxproj +++ b/MatrixSDK.xcodeproj/project.pbxproj @@ -2410,7 +2410,7 @@ 32FFB4ED217DC0E900C96002 /* MXKeyBackup_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXKeyBackup_Private.h; sourceTree = ""; }; 32FFB4EE217E146A00C96002 /* MXRecoveryKey.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXRecoveryKey.h; sourceTree = ""; }; 32FFB4EF217E146A00C96002 /* MXRecoveryKey.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXRecoveryKey.m; sourceTree = ""; }; - 33C2FCD6289DD3EA00C83E31 /* MXLRUCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXLRUCache.swift; sourceTree = ""; }; + 33C2FCD6289DD3EA00C83E31 /* MXLRUCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXLRUCache.swift; sourceTree = ""; usesTabs = 0; }; 3A0AF06A2705A11400679D1A /* MXSpaceGraphData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXSpaceGraphData.swift; sourceTree = ""; }; 3A108A392580E9C2005EEBE9 /* MXKeyProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXKeyProvider.h; sourceTree = ""; }; 3A108A7C25810C96005EEBE9 /* MXKeyData.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXKeyData.h; sourceTree = ""; }; diff --git a/changelog.d/pr-1544.change b/changelog.d/pr-1544.change new file mode 100644 index 0000000000..8d72c1b7c6 --- /dev/null +++ b/changelog.d/pr-1544.change @@ -0,0 +1 @@ +Swift: Refactored MXLRUCache to Swift \ No newline at end of file