zetasq / SwiftCache

A light-weight caching framework in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwiftCache

A light-weight caching framework in Swift

Carthage Compatible

Installation

Carthage

To integrate SwiftCache into your Xcode project using Carthage, specify it in your Cartfile:

github "zetasq/SwiftCache"

Run carthage update to build the framework. Drag the built SwiftCache.framework into your Xcode project.

Usage

You can use either SwiftMemoryCache to have a memory cache, or SwiftDiskCache to have a disk cache. You can also use SwiftCache to combine a memory cache and disk cache.

// Memory cache
let memoryCache = SwiftMemoryCache<Data>(
  cacheName: "dataMemoryCache",
  costLimit: 100 * 1024 * 1024,
  ageLimit: 30 * 24 * 60 * 60
)

let objectForMemoryCache: Data = ... // create data object
memoryCache.setObject(objectForMemoryCache, forKey: "key-for-data-in-memory", cost: objectForMemoryCache.count) // save data

let cachedDataInMemory = memoryCache.fetchObject(forKey: "key-for-data-in-memory") // retrieve cached data

// Disk cache
let diskCache = SwiftDiskCache<Data>(
  cacheName: "diskCache",
  byteLimit: 100 * 1024 * 1024,
  ageLimit: 30 * 24 * 60 * 60,
  objectEncoder: { $0 }, 
  objectDecoder: { $0 }
)

let objectForDiskCache: Data = ... // create data object
diskCache.asyncSetObject(objectForDiskCache, forKey: "key-for-data-on-disk") // save data

diskCache.asyncFetchObject(forKey: "key-for-data-on-disk") { cachedDataOnDisk in
  // do something with the data object
}

License

SwiftCache is released under the MIT license. See LICENSE for details.

About

A light-weight caching framework in Swift

License:MIT License


Languages

Language:Swift 98.1%Language:Objective-C 1.9%