Skip to content

Instantly share code, notes, and snippets.

@karthikAdaptavant
Last active March 9, 2020 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthikAdaptavant/e9b98f62413fd7995e18164235a80574 to your computer and use it in GitHub Desktop.
Save karthikAdaptavant/e9b98f62413fd7995e18164235a80574 to your computer and use it in GitHub Desktop.
import UIKit
// MARK: Cache Service
struct FlagService {
let key: String
let bufferTime: Double
var canProcess: Bool {
let buffer: Double = bufferTime * 60 * 60
guard (Date().timeIntervalSince1970 - getFlag()) > buffer else {
logger.info("Not the time to Fetch for key: \(key)")
return false
}
return true
}
init(key: String, bufferTime: Int = 12) {
self.key = key
self.bufferTime = Double(bufferTime)
}
func setFlag(_ value: Double = Date().timeIntervalSince1970) {
Preference.set(value, forKey: key)
}
private func getFlag() -> Double {
return Preference.double(forKey: key)
}
}
@karthikAdaptavant
Copy link
Author

Usage.

let flagService = FlagService(key: "pass your key here", bufferTime: 12) // 12 hours
flagService.setFlag() // this will set current time as flag 
flagService.getFlag() 
flagService.canProcess // this will tell to fetch or not. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment