Skip to content

Commit

Permalink
Update init and caches
Browse files Browse the repository at this point in the history
  • Loading branch information
yangKJ committed Jan 10, 2024
1 parent c0b6f15 commit 3557c54
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 35 deletions.
2 changes: 1 addition & 1 deletion CacheX.podspec
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions Podfile.lock
@@ -1,5 +1,5 @@
PODS:
- CacheX (1.0.1)
- CacheX (1.1.0)

DEPENDENCIES:
- CacheX (from `./`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "./"

SPEC CHECKSUMS:
CacheX: 916a589e95180513c095f04971dbf13260e41d21
CacheX: 8db0e9d17717600be0b5c34bde7bc7522dbd9a3a

PODFILE CHECKSUM: 2648cc89ba06c410cd6e50291b50f2cc53c2c4a1

COCOAPODS: 1.12.1
COCOAPODS: 1.14.3
4 changes: 2 additions & 2 deletions Pods/Local Podspecs/CacheX.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Pods/Target Support Files/CacheX/CacheX-Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Sources/OCStorage.swift
Expand Up @@ -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()
}

Expand All @@ -34,29 +34,29 @@ 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
}
}

/// The largest cache cost of memory cache. The total cost is pixel count of all cached images in memory.
/// 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
}
}

Expand Down Expand Up @@ -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)
}
}
11 changes: 11 additions & 0 deletions Sources/Setup/CachedOptions.swift
Expand Up @@ -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 {
Expand Down
57 changes: 44 additions & 13 deletions Sources/Storage.swift
Expand Up @@ -3,32 +3,34 @@
// CacheX
//
// Created by Condy on 2023/3/23.
//
//

import Foundation

/// Mixed storge transfer station.
public final class Storage<T: Codable> {

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<T>.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.
Expand Down Expand Up @@ -77,10 +79,39 @@ public final class Storage<T: Codable> {
/// 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()
}
}
}

0 comments on commit 3557c54

Please sign in to comment.