From db3e861bbe90440e9e33f9e43ee2a27656f655ff Mon Sep 17 00:00:00 2001 From: Craig Radnovich Date: Sun, 29 Nov 2015 15:31:03 +0800 Subject: [PATCH 1/2] Fix #38: Add more CacheExpiry options https://github.com/aschuch/AwesomeCache/issues/38 --- AwesomeCache/Cache.swift | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) 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 } } From ec1e83d9228dc623a8337f499b2d03832b3e4f5a Mon Sep 17 00:00:00 2001 From: Craig Radnovich Date: Sun, 29 Nov 2015 15:31:25 +0800 Subject: [PATCH 2/2] Use tabs instead of spaces like most of the preexisting code --- Example.xcodeproj/project.pbxproj | 1 + 1 file changed, 1 insertion(+) 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;