daily pastebin goal
81%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top