Skip to content

dev-labs-bg/SKCache

Repository files navigation

skcache-logo

Platform CocoaPods License Twitter URL Website

Table of Contents

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

  • 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.

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 protocol. You can make your own things conform to Codable 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<String>, Set<Int>, ...
  • 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.

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

do {
try SKCache.save()
} catch {
print(error)
}
do {
try SKCache.load()
} catch {
print(error)
}

Configuration

Here is how you can setup some configuration options

SKCache.elementsCount = 1000 // setup total count of elements saved into the cache

SKCache.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

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

Adding/Fetching objects

If you want to add or fetch an object you just follow thise simple steps:

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

Enable disk storage

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. To install it, simply add the following line to your Podfile:

pod 'SKCache'

Author

SKCache was created and is maintaned by Dev Labs. You can find us @devlabsbg or 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