From aa56fc6d1e6ed18fe4d6e791f6b602899a5227fc Mon Sep 17 00:00:00 2001 From: Paulo Andrade Date: Tue, 11 Aug 2020 13:10:09 +0100 Subject: [PATCH 1/2] Fixed warnings --- Haneke/CryptoSwiftMD5.swift | 2 +- Haneke/String+Haneke.swift | 11 +++++------ HanekeTests/NSData+Test.swift | 7 ++----- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Haneke/CryptoSwiftMD5.swift b/Haneke/CryptoSwiftMD5.swift index 85355c4d..33f0e8e5 100755 --- a/Haneke/CryptoSwiftMD5.swift +++ b/Haneke/CryptoSwiftMD5.swift @@ -167,7 +167,7 @@ class MD5 : HashBase { let chunkSizeBytes = 512 / 8 // 64 for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) { // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 - var M = sliceToUInt32Array(chunk) + let M = sliceToUInt32Array(chunk) assert(M.count == 16, "Invalid array") // Initialize hash value for this chunk: diff --git a/Haneke/String+Haneke.swift b/Haneke/String+Haneke.swift index b3188bef..23c3eb91 100644 --- a/Haneke/String+Haneke.swift +++ b/Haneke/String+Haneke.swift @@ -24,13 +24,12 @@ extension String { let MD5Calculator = MD5(Array(data)) let MD5Data = MD5Calculator.calculate() - let resultBytes = UnsafeMutablePointer(mutating: MD5Data) - let resultEnumerator = UnsafeBufferPointer(start: resultBytes, count: MD5Data.count) - let MD5String = NSMutableString() - for c in resultEnumerator { - MD5String.appendFormat("%02x", c) + let MD5String = MD5Data.withUnsafeBufferPointer { (ptr) -> String in + return ptr.reduce("") { (str, c) -> String in + str.appendingFormat("%02x", c) + } } - return MD5String as String + return MD5String } func MD5Filename() -> String { diff --git a/HanekeTests/NSData+Test.swift b/HanekeTests/NSData+Test.swift index 8f8a7417..485a323c 100644 --- a/HanekeTests/NSData+Test.swift +++ b/HanekeTests/NSData+Test.swift @@ -11,11 +11,8 @@ import Foundation extension Data { static func dataWithLength(_ length : Int) -> Data { - let buffer: [UInt8] = [UInt8](repeating: 0, count: length) -// return Data(bytes: UnsafePointer(&buffer), count: length) - let pointer = UnsafeRawPointer(buffer) - - return NSData(bytes: pointer, length: length) as Data + var buffer: [UInt8] = [UInt8](repeating: 0, count: length) + return NSData(bytes: &buffer, length: length) as Data } } From 0391da31fb9d1f409573d1c1ae0c3d8554b75547 Mon Sep 17 00:00:00 2001 From: Paulo Andrade Date: Tue, 11 Aug 2020 13:22:18 +0100 Subject: [PATCH 2/2] Allowing optional memory cache --- Haneke/Cache.swift | 22 +++++++++++++--------- Haneke/Format.swift | 5 ++++- HanekeTests/CacheTests.swift | 8 ++++++++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Haneke/Cache.swift b/Haneke/Cache.swift index 50a718f4..17544dd4 100644 --- a/Haneke/Cache.swift +++ b/Haneke/Cache.swift @@ -65,7 +65,7 @@ open class Cache where T.Result == T, T : DataRepresentable if let (format, memoryCache, diskCache) = self.formats[formatName] { self.format(value: value, format: format) { formattedValue in let wrapper = ObjectWrapper(value: formattedValue) - memoryCache.setObject(wrapper, forKey: key as AnyObject) + memoryCache?.setObject(wrapper, forKey: key as AnyObject) // Value data is sent as @autoclosure to be executed in the disk cache queue. diskCache.setData(self.dataFromValue(formattedValue, format: format), key: key) succeed?(formattedValue) @@ -78,7 +78,7 @@ open class Cache where T.Result == T, T : DataRepresentable @discardableResult open func fetch(key: String, formatName: String = HanekeGlobals.Cache.OriginalFormatName, failure fail : Fetch.Failer? = nil, success succeed : Fetch.Succeeder? = nil) -> Fetch { let fetch = Cache.buildFetch(failure: fail, success: succeed) if let (format, memoryCache, diskCache) = self.formats[formatName] { - if let wrapper = memoryCache.object(forKey: key as AnyObject) as? ObjectWrapper, let result = wrapper.hnk_value as? T { + if let wrapper = memoryCache?.object(forKey: key as AnyObject) as? ObjectWrapper, let result = wrapper.hnk_value as? T { fetch.succeed(result) diskCache.updateAccessDate(self.dataFromValue(result, format: format), key: key) return fetch @@ -124,7 +124,7 @@ open class Cache where T.Result == T, T : DataRepresentable open func remove(key: String, formatName: String = HanekeGlobals.Cache.OriginalFormatName) { if let (_, memoryCache, diskCache) = self.formats[formatName] { - memoryCache.removeObject(forKey: key as AnyObject) + memoryCache?.removeObject(forKey: key as AnyObject) diskCache.removeData(with: key) } } @@ -132,7 +132,7 @@ open class Cache where T.Result == T, T : DataRepresentable open func removeAll(_ completion: (() -> ())? = nil) { let group = DispatchGroup() for (_, (_, memoryCache, diskCache)) in self.formats { - memoryCache.removeAllObjects() + memoryCache?.removeAllObjects() group.enter() diskCache.removeAllData { group.leave() @@ -171,18 +171,22 @@ open class Cache where T.Result == T, T : DataRepresentable func onMemoryWarning() { for (_, (_, memoryCache, _)) in self.formats { - memoryCache.removeAllObjects() + memoryCache?.removeAllObjects() } } // MARK: Formats - public var formats : [String : (Format, NSCache, DiskCache)] = [:] + public var formats : [String : (Format, NSCache?, DiskCache)] = [:] open func addFormat(_ format : Format) { let name = format.name let formatPath = self.formatPath(withFormatName: name) - let memoryCache = NSCache() + var memoryCache: NSCache? = nil + if let memCapacity = format.memoryCapacity { + memoryCache = NSCache() + memoryCache!.totalCostLimit = memCapacity + } let diskCache = DiskCache(path: formatPath, capacity : format.diskCapacity) self.formats[name] = (format, memoryCache, diskCache) } @@ -214,7 +218,7 @@ open class Cache where T.Result == T, T : DataRepresentable return value.asData() } - fileprivate func fetchFromDiskCache(_ diskCache : DiskCache, key: String, memoryCache : NSCache, failure fail : ((Error?) -> ())?, success succeed : @escaping (T) -> ()) { + fileprivate func fetchFromDiskCache(_ diskCache : DiskCache, key: String, memoryCache : NSCache?, failure fail : ((Error?) -> ())?, success succeed : @escaping (T) -> ()) { diskCache.fetchData(key: key, failure: { error in if let block = fail { if (error as NSError?)?.code == NSFileReadNoSuchFileError { @@ -234,7 +238,7 @@ open class Cache where T.Result == T, T : DataRepresentable DispatchQueue.main.async(execute: { succeed(descompressedValue) let wrapper = ObjectWrapper(value: descompressedValue) - memoryCache.setObject(wrapper, forKey: key as AnyObject) + memoryCache?.setObject(wrapper, forKey: key as AnyObject) }) } }) diff --git a/Haneke/Format.swift b/Haneke/Format.swift index 7350b44d..0234a6be 100644 --- a/Haneke/Format.swift +++ b/Haneke/Format.swift @@ -14,13 +14,16 @@ public struct Format { public let diskCapacity : UInt64 + public let memoryCapacity : Int? + public var transform : ((T) -> (T))? public var convertToData : ((T) -> Data)? - public init(name: String, diskCapacity : UInt64 = UINT64_MAX, transform: ((T) -> (T))? = nil) { + public init(name: String, diskCapacity : UInt64 = UINT64_MAX, memoryCapacity: Int? = 0, transform: ((T) -> (T))? = nil) { self.name = name self.diskCapacity = diskCapacity + self.memoryCapacity = memoryCapacity self.transform = transform } diff --git a/HanekeTests/CacheTests.swift b/HanekeTests/CacheTests.swift index 66f8d17a..2a131a23 100644 --- a/HanekeTests/CacheTests.swift +++ b/HanekeTests/CacheTests.swift @@ -617,6 +617,14 @@ class CacheTests: XCTestCase { waitForExpectations(timeout: 0, handler: nil) } + func testNilMemoryCache() { + let sut = Format(name: self.name, memoryCapacity: nil) + + let cache = Cache(name: "Test") + cache.addFormat(sut) + XCTAssertNil(cache.formats[sut.name]?.1) + } + // MARK: Helpers func clearMemoryCache() {