From 3557c54f4a05a314c4c4b6f7edd38bad1c5ed56c Mon Sep 17 00:00:00 2001 From: Condy Date: Wed, 10 Jan 2024 15:09:23 +0800 Subject: [PATCH] Update init and caches --- CacheX.podspec | 2 +- Podfile.lock | 6 +- Pods/Local Podspecs/CacheX.podspec.json | 4 +- Pods/Manifest.lock | 6 +- Pods/Pods.xcodeproj/project.pbxproj | 4 +- .../CacheX/CacheX-Info.plist | 2 +- .../Pods-CacheXDemo.debug.xcconfig | 2 +- .../Pods-CacheXDemo.release.xcconfig | 2 +- Sources/OCStorage.swift | 16 +++--- Sources/Setup/CachedOptions.swift | 11 ++++ Sources/Storage.swift | 57 ++++++++++++++----- 11 files changed, 77 insertions(+), 35 deletions(-) diff --git a/CacheX.podspec b/CacheX.podspec index 841ac9d..5c9c85a 100644 --- a/CacheX.podspec +++ b/CacheX.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'CacheX' - s.version = '1.0.1' + s.version = '1.1.0' s.summary = 'Mixed storage warehouse with disk and memory. Support OC and Swift.' # This description is used to generate tags and improve search results. diff --git a/Podfile.lock b/Podfile.lock index ddb0ca8..d555c6e 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - CacheX (1.0.1) + - CacheX (1.1.0) DEPENDENCIES: - CacheX (from `./`) @@ -9,8 +9,8 @@ EXTERNAL SOURCES: :path: "./" SPEC CHECKSUMS: - CacheX: 916a589e95180513c095f04971dbf13260e41d21 + CacheX: 8db0e9d17717600be0b5c34bde7bc7522dbd9a3a PODFILE CHECKSUM: 2648cc89ba06c410cd6e50291b50f2cc53c2c4a1 -COCOAPODS: 1.12.1 +COCOAPODS: 1.14.3 diff --git a/Pods/Local Podspecs/CacheX.podspec.json b/Pods/Local Podspecs/CacheX.podspec.json index 1ca690d..d28d6a6 100644 --- a/Pods/Local Podspecs/CacheX.podspec.json +++ b/Pods/Local Podspecs/CacheX.podspec.json @@ -1,6 +1,6 @@ { "name": "CacheX", - "version": "1.0.1", + "version": "1.1.0", "summary": "Mixed storage warehouse with disk and memory. Support OC and Swift.", "homepage": "https://github.com/yangKJ/CacheX", "license": { @@ -12,7 +12,7 @@ }, "source": { "git": "https://github.com/yangKJ/CacheX.git", - "tag": "1.0.1" + "tag": "1.1.0" }, "swift_versions": "5.0", "requires_arc": true, diff --git a/Pods/Manifest.lock b/Pods/Manifest.lock index ddb0ca8..d555c6e 100644 --- a/Pods/Manifest.lock +++ b/Pods/Manifest.lock @@ -1,5 +1,5 @@ PODS: - - CacheX (1.0.1) + - CacheX (1.1.0) DEPENDENCIES: - CacheX (from `./`) @@ -9,8 +9,8 @@ EXTERNAL SOURCES: :path: "./" SPEC CHECKSUMS: - CacheX: 916a589e95180513c095f04971dbf13260e41d21 + CacheX: 8db0e9d17717600be0b5c34bde7bc7522dbd9a3a PODFILE CHECKSUM: 2648cc89ba06c410cd6e50291b50f2cc53c2c4a1 -COCOAPODS: 1.12.1 +COCOAPODS: 1.14.3 diff --git a/Pods/Pods.xcodeproj/project.pbxproj b/Pods/Pods.xcodeproj/project.pbxproj index 967b4ad..b9183a7 100644 --- a/Pods/Pods.xcodeproj/project.pbxproj +++ b/Pods/Pods.xcodeproj/project.pbxproj @@ -286,8 +286,8 @@ BFDFE7DC352907FC980B868725387E98 /* Project object */ = { isa = PBXProject; attributes = { - LastSwiftUpdateCheck = 1300; - LastUpgradeCheck = 1300; + LastSwiftUpdateCheck = 1500; + LastUpgradeCheck = 1500; }; buildConfigurationList = 4821239608C13582E20E6DA73FD5F1F9 /* Build configuration list for PBXProject "Pods" */; compatibilityVersion = "Xcode 14.0"; diff --git a/Pods/Target Support Files/CacheX/CacheX-Info.plist b/Pods/Target Support Files/CacheX/CacheX-Info.plist index 7ffdf65..dc59427 100644 --- a/Pods/Target Support Files/CacheX/CacheX-Info.plist +++ b/Pods/Target Support Files/CacheX/CacheX-Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.0.1 + 1.1.0 CFBundleSignature ???? CFBundleVersion diff --git a/Pods/Target Support Files/Pods-CacheXDemo/Pods-CacheXDemo.debug.xcconfig b/Pods/Target Support Files/Pods-CacheXDemo/Pods-CacheXDemo.debug.xcconfig index c7021b3..a4ac005 100644 --- a/Pods/Target Support Files/Pods-CacheXDemo/Pods-CacheXDemo.debug.xcconfig +++ b/Pods/Target Support Files/Pods-CacheXDemo/Pods-CacheXDemo.debug.xcconfig @@ -3,7 +3,7 @@ CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = NO FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/CacheX" GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 HEADER_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/CacheX/CacheX.framework/Headers" -LIBRARY_SEARCH_PATHS = $(inherited) "${DT_TOOLCHAIN_DIR}/usr/lib/swift/${PLATFORM_NAME}" /usr/lib/swift +LIBRARY_SEARCH_PATHS = $(inherited) "${TOOLCHAIN_DIR}/usr/lib/swift/${PLATFORM_NAME}" /usr/lib/swift OTHER_LDFLAGS = $(inherited) -ObjC -framework "CacheX" OTHER_SWIFT_FLAGS = $(inherited) -D COCOAPODS PODS_BUILD_DIR = ${BUILD_DIR} diff --git a/Pods/Target Support Files/Pods-CacheXDemo/Pods-CacheXDemo.release.xcconfig b/Pods/Target Support Files/Pods-CacheXDemo/Pods-CacheXDemo.release.xcconfig index c7021b3..a4ac005 100644 --- a/Pods/Target Support Files/Pods-CacheXDemo/Pods-CacheXDemo.release.xcconfig +++ b/Pods/Target Support Files/Pods-CacheXDemo/Pods-CacheXDemo.release.xcconfig @@ -3,7 +3,7 @@ CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = NO FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/CacheX" GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 HEADER_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/CacheX/CacheX.framework/Headers" -LIBRARY_SEARCH_PATHS = $(inherited) "${DT_TOOLCHAIN_DIR}/usr/lib/swift/${PLATFORM_NAME}" /usr/lib/swift +LIBRARY_SEARCH_PATHS = $(inherited) "${TOOLCHAIN_DIR}/usr/lib/swift/${PLATFORM_NAME}" /usr/lib/swift OTHER_LDFLAGS = $(inherited) -ObjC -framework "CacheX" OTHER_SWIFT_FLAGS = $(inherited) -D COCOAPODS PODS_BUILD_DIR = ${BUILD_DIR} diff --git a/Sources/OCStorage.swift b/Sources/OCStorage.swift index de23cbc..619b74a 100644 --- a/Sources/OCStorage.swift +++ b/Sources/OCStorage.swift @@ -22,7 +22,7 @@ struct StorageModel: Codable { } /// - Parameter queue: The default thread is the background thread. @objc public init(queue: DispatchQueue) { self.backgroundQueue = queue - self.storage = Storage(queue: backgroundQueue) + self.storage = Storage(queue: backgroundQueue, options: .diskAndMemory) super.init() } @@ -34,21 +34,21 @@ struct StorageModel: Codable { } /// The name of disk storage, this will be used as folder name within directory. @objc public var named: String = "DiskCached" { didSet { - storage.disk.named = named + storage.disk?.named = named } } /// The longest time duration in second of the cache being stored in disk. default is an week. @objc public var maxAgeLimit: TimeInterval = 60 * 60 * 24 * 7 { didSet { - storage.disk.expiry = Expiry.seconds(maxAgeLimit) + storage.disk?.expiry = Expiry.seconds(maxAgeLimit) } } /// The maximum total cost that the cache can hold before it starts evicting objects. default 20kb. @objc public var maxCountLimit: Disk.Byte = 20 * 1024 { didSet { - storage.disk.maxCountLimit = maxCountLimit + storage.disk?.maxCountLimit = maxCountLimit } } @@ -56,7 +56,7 @@ struct StorageModel: Codable { } /// Memory cache will be purged automatically when a memory warning notification is received. @objc public var maxCostLimit: UInt = 00 { didSet { - storage.memory.maxCostLimit = maxCostLimit + storage.memory?.maxCostLimit = maxCostLimit } } @@ -117,18 +117,18 @@ extension OCStorage { /// Get the disk cache size. @objc public var totalCost: Disk.Byte { get { - return storage.disk.totalCost + return storage.disk?.totalCost ?? 0 } } /// It's the file expired? @objc public func isExpired(forKey key: String) -> Bool { - return storage.disk.isExpired(forKey: key) + storage.disk?.isExpired(forKey: key) ?? false } /// Remove expired files from disk. /// - Parameter completion: Removed file URLs callback. @objc public func removeExpiredURLsFromDisk(completion: ((_ expiredURLs: [URL]) -> Void)? = nil) { - storage.disk.removeExpiredURLsFromDisk(completion: completion) + storage.disk?.removeExpiredURLsFromDisk(completion: completion) } } diff --git a/Sources/Setup/CachedOptions.swift b/Sources/Setup/CachedOptions.swift index 2ef31ec..79e9740 100644 --- a/Sources/Setup/CachedOptions.swift +++ b/Sources/Setup/CachedOptions.swift @@ -37,6 +37,17 @@ extension CachedOptions { } return nameds } + + func caches() -> [String: Cacheable] { + var caches = [String: Cacheable]() + if contains(.memory) { + caches[Memory.named] = Memory() + } + if contains(.disk) { + caches[Disk.named] = Disk() + } + return caches + } } @objc public enum OCCachedOptions: Int { diff --git a/Sources/Storage.swift b/Sources/Storage.swift index eb57fe0..8b0f653 100644 --- a/Sources/Storage.swift +++ b/Sources/Storage.swift @@ -3,32 +3,34 @@ // CacheX // // Created by Condy on 2023/3/23. -// +// import Foundation /// Mixed storge transfer station. public final class Storage { - public lazy var disk: Disk = Disk() - public lazy var memory: Memory = Memory() - - public lazy var caches: [String: Cacheable] = [ - Memory.named: memory, - Disk.named: disk - ] - + public var caches: [String: Cacheable] public let backgroundQueue: DispatchQueue lazy var transformer = TransformerFactory.forCodable() /// Initialize the object. /// - Parameter queue: The default thread is the background thread. - public init(queue: DispatchQueue? = nil) { + public init(queue: DispatchQueue? = nil, caches: [String: Cacheable]) { self.backgroundQueue = queue ?? { /// Create a background thread. DispatchQueue(label: "com.condy.CacheX.cached.queue", attributes: [.concurrent]) }() + self.caches = caches + } + + public convenience init(queue: DispatchQueue? = nil) { + self.init(queue: queue, caches: CachedOptions.all.caches()) + } + + public convenience init(queue: DispatchQueue? = nil, options: CachedOptions) { + self.init(queue: queue, caches: options.caches()) } /// Caching object. @@ -77,10 +79,39 @@ public final class Storage { /// Remove disk cache and memory cache. public func removedDiskAndMemoryCached(completion: SuccessComplete? = nil) { backgroundQueue.async { - self.disk.removedCached { isSuccess in - DispatchQueue.main.async { completion?(isSuccess) } + if let disk = self.caches[Disk.named] { + disk.removedCached { isSuccess in + DispatchQueue.main.async { completion?(isSuccess) } + } + } + if let memory = self.caches[Memory.named] { + memory.removedAllCached() + } + } + } +} + +/// 暂时兼容以前版本,未来将被废弃⚠️ +extension Storage { + public var disk: Disk? { + get { + return caches[Disk.named] as? Disk + } + set { + if let val = newValue { + caches.updateValue(val, forKey: Disk.named) + } + } + } + + public var memory: Memory? { + get { + return caches[Memory.named] as? Memory + } + set { + if let val = newValue { + caches.updateValue(val, forKey: Memory.named) } - self.memory.removedAllCached() } } }