SimpleCache 0.3.1

SimpleCache 0.3.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2016年11月
SwiftSwift 版本3.0
SPM支持 SPM

Nicolas Molina 维护。



  • 作者:
  • Nicolas Molina

SimpleCache

Índice

特性

  • 易于使用
  • 个性化缓存协议
  • 默认缓存协议使用用户默认设置

先决条件

  • iOS 8+
  • Xcode 7+
  • Swift 3.0

如何使用

查看示例项目以获取具体示例。

示例

// 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

默认值

seconds 中过期时间。默认是 60

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

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

缓存协议。默认使用用户默认设置

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)

返回缓存中 key 的值。如果没有在缓存中找到 key,返回 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)

在缓存中将 keyvalue。在 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)

如果缓存中存在 key 的值,则返回 true,否则返回 false

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

如果存在,则在缓存中返回 key 的值,并从缓存中删除此键。

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

返回删除缓存中的所有键。

SimpleCache.clear()
cleanExpirated
SimpleCache.cleanExpirated()

返回从缓存中删除已过期的键。

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)

如果缓存中的 key 已过期,则返回 true,否则返回 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 受 MIT 许可协议保护。更多信息请查看 LICENSE 文件。