A key-value storage cache tool like UserDefaults
on iOS Platform.
Like UserDefaults:
// Save cache.
let intVal = 123
try UserCaches.standard.set(intVal, forKey: key)
// Get cache. Specify the type('Int') to decide generic type.
let cache: Int = try UserCaches.standard.value(forKey: key)
UserCaches.standard
is a global instance of UserCaches.
Could also create new instance:
let cacheHelper = try UserCaches(cachePath: URL(fileURLWithPath: "/tmp/usercache.db"))
However, I suggest using following usage. (Example of setting of Baidu App)
import UserCaches
struct User: Codable {
let id: Int64
let name: String
}
enum ComBaiduMobileUserSetting: String, UserCachesSettable {
/// 隐私设置 - 允许通过手机号搜索到我
case privacy_findMeByPhoneNumber
/// 隐私设置 - 可通过感兴趣的人找到我
case privacy_findMeByInteresting
/// 隐私设置 - 开启通讯录关联
case privacy_relateAddressList
/// 隐私设置 - 黑名单
case privacy_blacklist
/// 字体大小
case font_size
var identifierMode: CacheKeyMode { return .identifier }
}
// Save caches
ComBaiduMobileUserSetting.privacy_findMeByPhoneNumber.storage = true
ComBaiduMobileUserSetting.privacy_findMeByInteresting.storage = false
ComBaiduMobileUserSetting.privacy_relateAddressList.storage = false
ComBaiduMobileUserSetting.privacy_blacklist.storage = [CacheCodability(User(id: 100120054,
name: "abc"))]
ComBaiduMobileUserSetting.font_size.storage = 20
// Get caches
let isFindMeByPhoneNumber: Bool? = ComBaiduMobileUserSetting.privacy_findMeByPhoneNumber.value()
let isFindMeByInteresting: Bool? = ComBaiduMobileUserSetting.privacy_findMeByInteresting.value()
let isRelateAddressList: Bool? = ComBaiduMobileUserSetting.privacy_relateAddressList.value()
let blacklist: [CacheCodability<User>] = ComBaiduMobileUserSetting.privacy_blacklist.value()
let fontSize: Int = ComBaiduMobileUserSetting.font_size.value() ?? 16
If you set identifierMode
to CacheKeyMode.identifier
, UserCachesSettable
will translate upper-case letter to lower-case letter and insert "." to translated left side on enum-name and replace "_" with "." on enum-case.
As above:
"ComBaiduMobileUserSetting" => "com.baidu.mobile.user.setting"
"privacy_findMeByPhoneNumber" => "privacy.findMeByPhoneNumber"
The case privacy_findMeByPhoneNumber
is translated to com.baidu.mobile.user.setting.privacy.findMeByPhoneNumber
, as a key, associated with true
for this example.
UseCaches support by default:
Default Support Type |
---|
Bool |
Int, Int64, UInt, Uint64 |
Float, Double |
String, Data |
Date (Implement with TimeInterval) |
Array<CacheCodable> |
Dictionary<Key: CacheCodable, Value: CacheCodable> |
CacheCodability<Codable> |
Especially, if a struct (or class) defer to Codable
, use CacheCodability wrap the struct (or class), UserCaches also accept it. See above CacheCodability<User>
Note: UserCaches requires Swift 4.1 and Xcode 9.3+
CocoaPods is a dependency manager for Cocoa projects. To install UserCaches with CocoaPods:
-
Make sure CocoaPods is installed. (UserCaches requires version 1.0.0 or greater.)
# Using the default Ruby install will require you to use sudo when # installing and updating gems. [sudo] gem install cocoapods
-
Update your Podfile to include the following:
use_frameworks! target 'YourAppTargetName' do pod 'UserCaches', '~> 0.0.5' end
-
Run
pod install --repo-update
.
The Swift Package Manager is a tool for managing the distribution of Swift code.
- Add the following to your
Package.swift
file:
dependencies: [
.package(url: "https://github.com/CUITCHE/UserCaches.git", from: "0.0.3")
]
- Build your project:
$ swift build
clone
this repo.cd
the repo directory.- Execute order
swift test
.
UserCaches is available under the MIT license.