diff --git a/AwesomeCache/Cache.swift b/AwesomeCache/Cache.swift index 5531235..5f82b29 100644 --- a/AwesomeCache/Cache.swift +++ b/AwesomeCache/Cache.swift @@ -3,8 +3,13 @@ import Foundation /// Represents the expiry of a cached object public enum CacheExpiry { case Never - case Seconds(NSTimeInterval) case Date(NSDate) + case Seconds(NSTimeInterval) + case Minutes(Int) + case Hours(Int) + case Days(Int) + case Months(Int) + case Years(Int) } /// A generic cache that persists objects to disk and is backed by a NSCache. @@ -250,14 +255,37 @@ public class Cache { } private func expiryDateForCacheExpiry(expiry: CacheExpiry) -> NSDate { + let now = NSDate() + switch expiry { case .Never: return NSDate.distantFuture() case .Seconds(let seconds): - return NSDate().dateByAddingTimeInterval(seconds) + return now.dateByAddingTimeInterval(seconds) case .Date(let date): return date + default: + break } + + let offsetComponents = NSDateComponents() + + switch expiry { + case .Hours(let hours): + offsetComponents.hour = hours + case .Days(let days): + offsetComponents.day = days + case .Months(let months): + offsetComponents.month = months + case .Years(let years): + offsetComponents.year = years + default: + offsetComponents.hour = 2 + } + + let cal = NSCalendar.currentCalendar() + + return cal.dateByAddingComponents(offsetComponents, toDate: now, options: NSCalendarOptions(rawValue: 0)) ?? now } } diff --git a/Example.xcodeproj/project.pbxproj b/Example.xcodeproj/project.pbxproj index 6c38dbe..6abd39a 100644 --- a/Example.xcodeproj/project.pbxproj +++ b/Example.xcodeproj/project.pbxproj @@ -113,6 +113,7 @@ 4D44D2881971627700EC5FDB /* Products */, ); sourceTree = ""; + usesTabs = 1; }; 4D44D2881971627700EC5FDB /* Products */ = { isa = PBXGroup;