- Features.
- Prerequisites.
- Installation.
- How to Use.
- Example.
- API.
- Default values.
- get.
- put.
- has.
- remove.
- clear.
- cleanExpirated.
- isExpirated.
- Personalize.
- License.
- Easy to use
- Personalize cache protocol
- Default cache protocol use user defaults
- iOS 8+
- Xcode 7+
- Swift 3.0
SimpleCache is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SimpleCache"
Check out the demo project for a concrete 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()
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()
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
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
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
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
Return remove all keys in cache.
SimpleCache.clear()
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
}
// ...
}
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
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
}
// ...
}
SimpleCache is available under the MIT license. See the LICENSE file for more info.