UserCache 0.1.1

UserCache 0.1.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布上次发布2016年9月
SPM支持 SPM

Nicolas Molina 维护。



UserCache 0.1.1

  • 作者
  • Nicolas Molina

UserCache

索引

特性

  • 易于使用
  • 默认缓存协议使用用户默认值

先决条件

  • iOS 8+
  • Xcode 7+
  • Swift 2.0

如何使用

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

示例

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

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

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

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

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

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

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

// Set expired key in 2 seconds
UserCache.put("string", value: "This is a string key expired in 2 seconds", seconds: 2)
print("Expired Key: string\nValue: \(UserCache.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: \(UserCache.get("string"))")
    }
)

// Clean the cache
// UserCache.clear()

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

API

UserCache.DEFAULT_CACHE_SECONDS = 60
UserCache.get(key: String, defaultValue: AnyObject? = nil)

返回缓存中对 key 的值。如果没有缓存中存在 key,则返回 defaultValue

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

在缓存中对 key 放置 value。在 seconds 后过期。

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

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

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

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

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

返回删除缓存中所有键。

UserCache.clear()
UserCache.cleanExpirated()

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

UserCache.cleanExpirated()
UserCache.isExpirated()

如果缓存中的 key 已过期,则返回 true,否则返回 false

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

// Delay 6 minutes

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

版权信息

UserCache 权限协议遵循 MIT。有关更多信息,请参阅 LICENSE 文件。