- Notifications
You must be signed in to change notification settings - Fork1
A caching library for each type in iOS
License
dev-labs-bg/SKCache
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Description
- Key features
- Usage
- Storage
- Configuration
- Expiry date
- Adding/Fetching objects
- Enable disk storage
- Installation
- Author
- Contributing
- License
SKCache doesn't claim to be unique in this area, but it's not another monsterlibrary that gives you a god's power. It does nothing but caching, but it does it well.
- Work with Swift 4.2, Swift 4 and Swift 3.2.
- Disk storage is optional.
- Support
expiry
and clean up of expired objects. - Extensive unit test coverage
- iOS, tvOS support.
SKCache
is built based onNSCache
and supports all valid types in Swift. It has memory storage and can support optionaly disk storage. Memory storage should be less time and memory consuming, while disk storage is used for content that outlives the application life-cycle, see it more like a convenient way to store user information that should persist across application launches.
SKCache
supports any objects that conform toCodable protocol. You canmake your own things conform to Codable so that can be saved and loaded fromSKCache
.
The supported types are
- Primitives like
Int
,Float
,String
,Bool
, ... - Array of primitives like
[Int]
,[Float]
,[Double]
, ... - Set of primitives like
Set<String>
,Set<Int>
, ... - Simply dictionary like
[String: Int]
,[String: String]
, ... Date
URL
Data
Error handling is done viatry catch
.SKCache
throws errors in terms ofOperations
.
/// Enum to hold possible errors during execution of methods over SKCache////// - fetchFail: Failed to fetch an object/// - deletaFail: Failed to delete an object/// - saveFail: Failed to save an object/// - loadFail: Failed to load the SKCachepublicenumOperations:Swift.Error{case fetchFailcase deletaFailcase saveFailcase loadFail}
There can be errors because of disk problem or type mismatch when saving/loading into/from device storage, so if want to handle errors, you need to dotry catch
do{trySKCache.save()}catch{print(error)}
do{trySKCache.load()}catch{print(error)}
Here is how you can setup some configuration options
SKCache.elementsCount=1000 // setup total count of elements saved into the cacheSKCache.elementsCostLimit=1024*1024 // setup the cost limit of the cacheSKCache.shared.expiration=.everyDay // setup expiration date of each object in the cache
By default, all saved objects have the same expiry as the expiry you specify inSKCache.shared.expiration
. You can overwrite this for a specific object by specifyingexpiry
in the constructor ofSKObject
// Default expiry date from configuration will be applied to the itemletobject=SKObject(value:"This is a string", key:"string")// A given expiry date will be applied to the itemletobject=SKObject(value:"This is a string", key:"string", expirationDate:ExpiryDate.everyDay.expiryDate())
If you want to add or fetch an object you just follow thise simple steps:
//1. Create a SKObjectletobject=SKObject(value:"This is a string", key:"string")//2. Add it to the cacheSKCache.shared.add(object: object)//3. Fetch an object from the cacheletstring:String?=SKCache.shared.get(forKey:"string")
As of version 1.3.0 disk storage is enabled by default. There is no need to call aditional method to load the cache with objects.A new property called isOnlyInMemory was introduced to indicate wether the cached objects will be saved on the disk space or will remain in the memory.
SKCache is available throughCocoaPods. To installit, simply add the following line to your Podfile:
pod'SKCache'
SKCache
was created and is maintaned by Dev Labs. You can find us@devlabsbg ordevlabs.bg
We would love you to contribute toSKCache, so:
- if you found a bug, open an issue
- if you have a feature request, open an issue
- if you want to contribute, submit a pull request
SKCache is available under the MIT license. See theLICENSE
About
A caching library for each type in iOS