-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# SwiftCache | ||
|
||
A light-weight caching framework in Swift | ||
|
||
[](https://github.com/Carthage/Carthage) | ||
|
||
## Installation | ||
### Carthage | ||
To integrate **SwiftCache** into your Xcode project using [Carthage](https://github.com/Carthage/Carthage), specify it in your `Cartfile`: | ||
|
||
```ogdl | ||
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. | ||
|
||
```swift | ||
|
||
// 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](https://github.com/zetasq/SwiftCache/blob/master/LICENSE) for details. |