SimpleCache 0.3.1

SimpleCache 0.3.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2016
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Nicolas Molina.



  • By
  • Nicolas Molina

SimpleCache

Índice

Features

  • Easy to use
  • Personalize cache protocol
  • Default cache protocol use user defaults

Prerequisites

  • iOS 8+
  • Xcode 7+
  • Swift 3.0

How to Use

Check out the demo project for a concrete example.

Example

// String
SimpleCache.put("string", value: "This is a string")
print("Key: string\nValue: \(SimpleCache.get("string"))")

// Number
SimpleCache.put("number", value: 20.3)
print("Key: number\nValue: \(SimpleCache.get("number"))")

// JSON
SimpleCache.put("json", value: ["key": "value"])
print("Key: json\nValue: \(SimpleCache.get("json"))")

// NSData
SimpleCache.put("data", value: NSData(bytes: [0xFF, 0xD9] as [UInt8], length: 2))
print("Key: data\nValue: \(SimpleCache.get("data"))")

print("------------------------------")

// Remove an object from the cache
SimpleCache.remove("string")
print("Remove Key: string\nValue: \(SimpleCache.get("string"))")

print("------------------------------")

// Set expired key in 2 seconds
SimpleCache.put("string", value: "This is a string key expired in 2 seconds", seconds: 2)
print("Expired Key: string\nValue: \(SimpleCache.get("string"))")
print("Sleep 3 seconds")
dispatch_after(
    dispatch_time(
        DISPATCH_TIME_NOW,
        Int64(3 * Double(NSEC_PER_SEC))
    ),
    dispatch_get_main_queue(),
    {
        self.print("Expired Key: string\nValue: \(SimpleCache.get("string"))")
    }
)

// Clean the cache
SimpleCache.clear()

// Clean expired keys from cache
SimpleCache.cleanExpirated()

API

Default values

Expired time in seconds. Default is 60

SimpleCache.DEFAULT_CACHE_SECONDS: Int = 60 // Default is 1 minute

// Configure other time
SimpleCache.DEFAULT_CACHE_SECONDS = 5 * 60 // 5 minute

Cache protocol. Default use user defaults

SimpleCache.CACHE_PROTOCOL: SimpleCacheProtocol = UserDefaultsCache.sharedInstance // Default cache use user defaults

// Configure other cache
SimpleCache.CACHE_PROTOCOL = MyCache()
get
SimpleCache.get(key: String, defaultValue: Any? = nil)

Return value for key in cache. If not has key in cache return defaultValue.

SimpleCache.get("exist.key")                        // return value for "exist.key"
SimpleCache.get("not.exist.key")                    // return nil, because "not.exist.key" not exist in cache
SimpleCache.get("not.exist.key2", defaultValue: 10) // return 10, because "not.exist.key" not exist in cache, but defaultValue is set
put
SimpleCache.put(key: String, value: Any?, seconds: Int = DEFAULT_CACHE_SECONDS)

Put value for key in cache. Expired in seconds.

SimpleCache.put("a.key", value: 10)                   // Expired in DEFAULT_CACHE_SECONDS
SimpleCache.put("a.key2", value: 10, seconds: 5 * 60) // Expired in 5 minutes
has
SimpleCache.has(key: String)

Return true if has value for key in cache, else false.

SimpleCache.has("exist.key")     // return true
SimpleCache.has("not.exist.key") // return false
remove
SimpleCache.remove(key: String)

Return value for key in cache if exist, and remove this key in cache.

SimpleCache.remove("exist.key")     // return value for "exist.key"
SimpleCache.remove("not.exist.key") // return nil, because "not.exist.key" not exist in cache
clear

Return remove all keys in cache.

SimpleCache.clear()
cleanExpirated
SimpleCache.cleanExpirated()

Return remove expirated keys in cache.

import SimpleCache

class AppDelegate: UIResponder, UIApplicationDelegate {

    // ...

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        // Clean cache expirated keys
        SimpleCache.cleanExpirated()

        return true
    }

    // ...

}
isExpirated
SimpleCache.isExpirated(key: String)

Return true if key in cache is expired, else false.

SimpleCache.put("a.key2", value: 10, seconds: 5 * 60) // Expired in 5 minutes

SimpleCache.isExpirated("a.key2") // return false

// Delay 6 minutes

SimpleCache.isExpirated("a.key2") // return true

Personalize

import SimpleCache

public class MyCache: SimpleCacheProtocol
{

    fileprivate var cache: [String : Any] = [:]

    open func dictionaryRepresentation() -> [String : Any] {
        return cache
    }

    open func get(_ key: String, defaultValue: Any? = nil) -> Any? {
        if let value = cache[key] {
            return value
        }
        return defaultValue
    }

    open func put(_ key: String, value: Any?) {
        cache[key] = value
    }

    open func has(_ key: String) -> Bool {
        return get(key) != nil
    }

    open func remove(_ key: String) -> Any? {
        return cache.removeValueForKey(key)
    }

}

// Configure in AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {

    // ...

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        // Configure cache
        SimpleCache.CACHE_PROTOCOL = MyCache()

        return true
    }

    // ...

}

License

SimpleCache is available under the MIT license. See the LICENSE file for more info.