Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A caching library for each type in iOS

License

NotificationsYou must be signed in to change notification settings

dev-labs-bg/SKCache

Repository files navigation

skcache-logo

PlatformCocoaPodsLicenseTwitter URLWebsite

Table of Contents

Description

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.

Key features

  • Work with Swift 4.2, Swift 4 and Swift 3.2.
  • Disk storage is optional.
  • Supportexpiry and clean up of expired objects.
  • Extensive unit test coverage
  • iOS, tvOS support.

Usage

Storage

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.

Codable types

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 likeInt,Float,String,Bool, ...
  • Array of primitives like[Int],[Float],[Double], ...
  • Set of primitives likeSet<String>,Set<Int>, ...
  • Simply dictionary like[String: Int],[String: String], ...
  • Date
  • URL
  • Data

Error handling

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)}

Configuration

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

Expiry date

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

Adding/Fetching objects

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

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 throughCocoaPods. To installit, 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 ordevlabs.bg

Contributing

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

License

SKCache is available under the MIT license. See theLICENSE


[8]ページ先頭

©2009-2025 Movatter.jp