Skip to content

Commit

Permalink
fixed non public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stremovskyy committed May 18, 2023
1 parent 7d30573 commit b8981a4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ SwiftCacher is inspired by the need for a simple and efficient caching mechanism




42 changes: 33 additions & 9 deletions Sources/SwiftCacher/SwiftCacher.swift
Original file line number Diff line number Diff line change
@@ -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.")
}
Expand All @@ -22,7 +27,13 @@ public struct SwiftCacher {
}
}

func setObject<T: NSCoding>(_ 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<T: NSCoding>(_ object: T, forKey key: String) {
let fileURL = cacheDirectory.appendingPathComponent(key)

// Serialize the object
Expand All @@ -36,32 +47,42 @@ public struct SwiftCacher {
}
}

func getObject<T: NSObject & NSCoding>(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<T: NSObject & NSCoding>(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
Expand All @@ -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: [])
Expand Down

0 comments on commit b8981a4

Please sign in to comment.