https://github.com/dev-labs-bg/skcache
A caching library for each type in iOS
https://github.com/dev-labs-bg/skcache
cache ios swift
Last synced: about 2 months ago
JSON representation
A caching library for each type in iOS
- Host: GitHub
- URL: https://github.com/dev-labs-bg/skcache
- Owner: dev-labs-bg
- License: mit
- Created: 2017-11-03T14:27:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-28T13:16:32.000Z (about 6 years ago)
- Last Synced: 2025-04-18T01:41:16.774Z (2 months ago)
- Topics: cache, ios, swift
- Language: Swift
- Size: 944 KB
- Stars: 9
- Watchers: 7
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[]() []() [](https://github.com/dev-labs-bg/SKCache/blob/master/LICENSE) [](http://twitter.com/devlabsbg) [](http://devlabs.bg)
## Table of Contents
* [Description](#description)
* [Key features](#key-features)
* [Usage](#usage)
* [Storage](#storage)
* [Configuration](#configuration)
* [Expiry date](#expiry-date)
* [Adding/Fetching objects](#add-fetch-object)
* [Enable disk storage](#disk-storage)
* [Installation](#installation)
* [Author](#author)
* [Contributing](#contributing)
* [License](#license)## Description
**SKCache** doesn't claim to be unique in this area, but it's not another monster
library that gives you a god's power. It does nothing but caching, but it does it well.## Key features
- [x] Work with Swift 4.2, Swift 4 and Swift 3.2.
- [x] Disk storage is optional.
- [x] Support `expiry` and clean up of expired objects.
- [x] Extensive unit test coverage
- [x] iOS, tvOS support.## Usage
### Storage
`SKCache` is built based on `NSCache` 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.
#### Codable types
`SKCache` supports any objects that conform to [Codable](https://developer.apple.com/documentation/swift/codable) protocol. You can [make your own things conform to Codable](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types) so that can be saved and loaded from `SKCache`.
The supported types are
- Primitives like `Int`, `Float`, `String`, `Bool`, ...
- Array of primitives like `[Int]`, `[Float]`, `[Double]`, ...
- Set of primitives like `Set`, `Set`, ...
- Simply dictionary like `[String: Int]`, `[String: String]`, ...
- `Date`
- `URL`
- `Data`#### Error handling
Error handling is done via `try catch`. `SKCache` throws errors in terms of `Operations`.
```swift
/// 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 SKCache
public enum Operations: Swift.Error {
case fetchFail
case deletaFail
case saveFail
case 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 do `try catch`
```swift
do {
try SKCache.save()
} catch {
print(error)
}
``````swift
do {
try SKCache.load()
} catch {
print(error)
}
```### Configuration
Here is how you can setup some configuration options
```swift
SKCache.elementsCount = 1000 // setup total count of elements saved into the cacheSKCache.elementsCostLimit = 1024 * 1024 // setup the cost limit of the cache
SKCache.shared.expiration = .everyDay // setup expiration date of each object in the cache
```### Expiry date
By default, all saved objects have the same expiry as the expiry you specify in `SKCache.shared.expiration` . You can overwrite this for a specific object by specifying `expiry` in the constructor of `SKObject`
```swift
// Default expiry date from configuration will be applied to the item
let object = SKObject(value: "This is a string", key: "string")// A given expiry date will be applied to the item
let object = 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:
```swift
//1. Create a SKObject
let object = SKObject(value: "This is a string", key: "string")//2. Add it to the cache
SKCache.shared.add(object: object)//3. Fetch an object from the cache
let string: 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.## Installation
### Cocoapods
**SKCache** is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'SKCache'
```## Author
`SKCache` was created and is maintaned by Dev Labs. You can find us [@devlabsbg](https://twitter.com/devlabsbg) or [devlabs.bg](http://devlabs.bg/)
## Contributing
We would love you to contribute to **SKCache**, 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## License
**SKCache** is available under the MIT license. See the [LICENSE](https://github.com/dev-labs-bg/SKCache/blob/master/LICENSE)