From b8981a47fa6cc853e729cbf5c8c09690d0a3c86c Mon Sep 17 00:00:00 2001 From: Anton Stremovskyy Date: Thu, 18 May 2023 12:35:25 +0300 Subject: [PATCH] fixed non public methods --- README.md | 1 + Sources/SwiftCacher/SwiftCacher.swift | 42 +++++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d09ed79..5db2517 100644 --- a/README.md +++ b/README.md @@ -93,3 +93,4 @@ SwiftCacher is inspired by the need for a simple and efficient caching mechanism + diff --git a/Sources/SwiftCacher/SwiftCacher.swift b/Sources/SwiftCacher/SwiftCacher.swift index 54d1560..efded9a 100644 --- a/Sources/SwiftCacher/SwiftCacher.swift +++ b/Sources/SwiftCacher/SwiftCacher.swift @@ -1,12 +1,17 @@ import Foundation +/// A cache for storing objects on disk. public struct SwiftCacher { private let cacheDirectory: URL + /// Creates a new cache instance. + /// - Parameter cacheDirectoryName: The name of the cache directory. + /// - Parameter fileManager: The file manager to use for creating the cache directory. Defaults to `FileManager.default`. public init() { // Create a directory for the cache let cacheDirectoryName = "CacheDirectory" let fileManager = FileManager.default + guard let cacheDirectoryURL = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first else { fatalError("Failed to retrieve caches directory URL.") } @@ -22,7 +27,13 @@ public struct SwiftCacher { } } - func setObject(_ object: T, forKey key: String) { + /// Caches an object. + /// - Parameters: + /// - object: The object to cache. + /// - key: The key to associate with the object. + /// - Throws: An error if the object cannot be cached. + /// - Note: The object must be a subclass of `NSObject` and conform to `NSCoding` and `NSSecureCoding`. + public func setObject(_ object: T, forKey key: String) { let fileURL = cacheDirectory.appendingPathComponent(key) // Serialize the object @@ -36,32 +47,42 @@ public struct SwiftCacher { } } - func getObject(forKey key: String) -> T? { + /// Retrieves a cached object. + /// - Parameters: + /// - Parameter key: The key associated with the object. + /// - Returns: The cached object, or `nil` if no object is cached for the given key. + /// - Throws: An error if the cached object cannot be retrieved. + /// - Note: The object must be a subclass of `NSObject` and conform to `NSCoding` and `NSSecureCoding`. + public func getObject(forKey key: String) -> T? { let fileURL = cacheDirectory.appendingPathComponent(key) - + // Read the serialized object from disk guard let data = try? Data(contentsOf: fileURL) else { return nil } - + // Deserialize the object do { let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data) unarchiver.requiresSecureCoding = true - + guard let object = unarchiver.decodeObject(of: T.self, forKey: NSKeyedArchiveRootObjectKey) else { return nil } - + unarchiver.finishDecoding() - + return object } catch { fatalError("Failed to unarchive object from cache: \(error)") } } - func removeObject(forKey key: String) { + /// Removes a cached object. + /// - Parameters: + /// - Parameter key: The key associated with the object. + /// - Throws: An error if the object cannot be removed. + public func removeObject(forKey key: String) { let fileURL = cacheDirectory.appendingPathComponent(key) // Remove the object from disk @@ -72,7 +93,10 @@ public struct SwiftCacher { } } - func removeAllObjects() { + /// Removes all objects from the cache. + /// - Throws: An error if the objects cannot be removed. + /// - Note: This method is not atomic. + public func removeAllObjects() { // Remove all objects from the cache directory do { let fileURLs = try FileManager.default.contentsOfDirectory(at: cacheDirectory, includingPropertiesForKeys: nil, options: [])