Skip to content

Commit

Permalink
add expiry enum
Browse files Browse the repository at this point in the history
* includes date closure to verify method for testing
  • Loading branch information
Andreas Grauel committed Jan 31, 2020
1 parent f6ec076 commit 20102cd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Sources/Storage+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public extension Storage {
})
}

func load<T: Codable>(forKey key: String, as: T.Type, maxAge: TimeInterval = .greatestFiniteMagnitude) throws -> T {
func load<T: Codable>(forKey key: String, as: T.Type, withExpiry expiry: Expiry = .never) throws -> T {
func loadFromDisk<T: Codable>(forKey key: String, as: T.Type) throws -> T {
let data = try Data(contentsOf: fileUrl(forKey: key))
let decoder = options.decoder
Expand All @@ -35,7 +35,7 @@ public extension Storage {
}
}

return try commonLoad(forKey: key, withMaxAge: maxAge, fromData: { data in
return try commonLoad(forKey: key, withExpiry: expiry, fromData: { data in
return try loadFromDisk(forKey: key, as: T.self)
})
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Storage+Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public extension Storage {
try commonSave(object: object as AnyObject, forKey: key, toData: { object })
}

func load(forKey key: String, withMaxAge maxAge: TimeInterval = .greatestFiniteMagnitude) throws -> Data {
return try commonLoad(forKey: key, withMaxAge: maxAge, fromData: { $0 })
func load(forKey key: String, withExpiry expiry: Expiry = .never) throws -> Data {
return try commonLoad(forKey: key, withExpiry: expiry, fromData: { $0 })
}
}
4 changes: 2 additions & 2 deletions Sources/Storage+Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public extension Storage {
})
}

func load(forKey key: String, withMaxAge maxAge: TimeInterval = .greatestFiniteMagnitude) throws -> Image {
return try commonLoad(forKey: key, withMaxAge: maxAge, fromData: { data in
func load(forKey key: String, withExpiry expiry: Expiry = .never) throws -> Image {
return try commonLoad(forKey: key, withExpiry: expiry, fromData: { data in
return try unwrapOrThrow(Utils.image(data: data), StorageError.decodeData)
})
}
Expand Down
31 changes: 24 additions & 7 deletions Sources/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ extension Storage {
func fileUrl(forKey key: String) -> URL {
return folderUrl.appendingPathComponent(key, isDirectory: false)
}

func verify(maxAge: TimeInterval,
forKey key: String,
fromDate date: @escaping (() -> Date) = { Date() }) throws -> Bool {
date().timeIntervalSince(try modificationDate(forKey: key)) <= maxAge
}
}

extension Storage {
Expand All @@ -93,13 +99,17 @@ extension Storage {
.trueOrThrow(StorageError.createFile)
}

func commonLoad<T>(forKey key: String, withMaxAge maxAge: TimeInterval, fromData: (Data) throws -> T) throws -> T {
func verify(maxAge: TimeInterval, forKey key: String) throws -> Bool {
Date().timeIntervalSince(try modificationDate(forKey: key)) <= maxAge
}

guard try verify(maxAge: maxAge, forKey: key) else {
throw StorageError.expired(maxAge: maxAge)
func commonLoad<T>(forKey key: String,
withExpiry expiry: Expiry,
fromDate date: @escaping (() -> Date) = { Date() },
fromData: (Data) throws -> T) throws -> T {
switch expiry {
case .never:
break
case .maxAge(let maxAge):
guard try verify(maxAge: maxAge, forKey: key, fromDate: date) else {
throw StorageError.expired(maxAge: maxAge)
}
}

if let object = cache.object(forKey: key as NSString) as? T {
Expand All @@ -112,3 +122,10 @@ extension Storage {
}
}
}

extension Storage {
public enum Expiry {
case never
case maxAge(maxAge: TimeInterval)
}
}

0 comments on commit 20102cd

Please sign in to comment.