Guest User

Untitled

a guest
Jul 11th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import Foundation
  2. import Disk
  3.  
  4. public protocol Cachable: Codable{
  5. associatedtype CacheObject
  6.  
  7. var cacheName: String {get}
  8.  
  9. static func cacheId(_ id: String?) -> String
  10. static func loadCache(cacheName: String) -> CacheObject?
  11. static func removeCache(cacheName: String)
  12.  
  13. func saveCache() -> Bool
  14. }
  15.  
  16. extension Cachable where CacheObject: Cachable{
  17.  
  18. static func loadCache(cacheName: String) -> CacheObject?{
  19. do {
  20. let cache = try Disk.retrieve(
  21. cacheName,
  22. from: .caches,
  23. as: CacheObject.self
  24. )
  25. return cache
  26. } catch {
  27. return nil
  28. }
  29. }
  30.  
  31. static func removeCache(cacheName: String) {
  32. do {
  33. try Disk.remove(
  34. cacheName,
  35. from: .caches
  36. )
  37. } catch {
  38. print("Remove cache is failure")
  39. }
  40. }
  41.  
  42. @discardableResult func saveCache() -> Bool{
  43. do {
  44. try Disk.save(
  45. self,
  46. to: .caches,
  47. as: self.cacheName
  48. )
  49. return true
  50. } catch {
  51. return false
  52. }
  53. }
  54. }
Add Comment
Please, Sign In to add comment