Skip to content

Commit 9174a0b

Browse files
committed
Merge pull request #35 from hyperoslo/feature/sync-cache
Feature: sync cache
2 parents 4eeeaee + efba4ab commit 9174a0b

28 files changed

+865
-15
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ language: objective-c
33

44
before_install:
55
- brew update
6-
- if brew outdated | grep -qx xctool; then brew upgrade xctool; fi
76
- if brew outdated | grep -qx carthage; then brew upgrade carthage; fi
8-
- travis_wait 35 carthage update --platform Mac,iOS
7+
- travis_wait 35 carthage bootstrap --platform Mac,iOS
98

109
script:
1110
- xcodebuild clean build -project Cache.xcodeproj -scheme "Cache-Mac" -sdk macosx

Cache.xcodeproj/project.pbxproj

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
D5291C3A1C28220B00B702C9 /* StorageAware.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C261C28220B00B702C9 /* StorageAware.swift */; };
2828
D5291C3B1C28220B00B702C9 /* StorageFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C271C28220B00B702C9 /* StorageFactory.swift */; };
2929
D5291C6C1C2827FB00B702C9 /* BasicHybridCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C6A1C2827FB00B702C9 /* BasicHybridCache.swift */; };
30-
D5291C6D1C2827FB00B702C9 /* Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C6B1C2827FB00B702C9 /* Cache.swift */; };
3130
D5291C731C28296B00B702C9 /* HybridCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C721C28296B00B702C9 /* HybridCache.swift */; };
3231
D5291D1D1C2837DB00B702C9 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5DC59E01C20593E003BD79B /* Cache.framework */; };
3332
D5291D351C28389B00B702C9 /* SpecHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291CDF1C28374800B702C9 /* SpecHelper.swift */; };
@@ -53,7 +52,6 @@
5352
D5291D7E1C283BF200B702C9 /* HybridCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291D7A1C283BE300B702C9 /* HybridCache.swift */; };
5453
D5291D851C283C7C00B702C9 /* SpecHelper+OSX.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291D811C283C7000B702C9 /* SpecHelper+OSX.swift */; };
5554
D5291D861C283CFB00B702C9 /* BasicHybridCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C6A1C2827FB00B702C9 /* BasicHybridCache.swift */; };
56-
D5291D871C283CFB00B702C9 /* Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C6B1C2827FB00B702C9 /* Cache.swift */; };
5755
D5291D881C283CFB00B702C9 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C151C28220B00B702C9 /* Config.swift */; };
5856
D5291D891C283CFB00B702C9 /* Cachable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C171C28220B00B702C9 /* Cachable.swift */; };
5957
D5291D8A1C283CFB00B702C9 /* Capsule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C181C28220B00B702C9 /* Capsule.swift */; };
@@ -74,6 +72,15 @@
7472
D5291D9E1C283EC000B702C9 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5291CDA1C2835D000B702C9 /* Nimble.framework */; };
7573
D5291DA11C28405900B702C9 /* UIImage+CacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291DA01C28405900B702C9 /* UIImage+CacheTests.swift */; };
7674
D5291DA31C2841D200B702C9 /* NSImage+CacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291DA21C2841D200B702C9 /* NSImage+CacheTests.swift */; };
75+
D5ACACC91CD01F3400567809 /* SyncCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACC81CD01F3400567809 /* SyncCache.swift */; };
76+
D5ACACCA1CD01F3400567809 /* SyncCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACC81CD01F3400567809 /* SyncCache.swift */; };
77+
D5ACACCC1CD0207300567809 /* SyncHybridCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACCB1CD0207300567809 /* SyncHybridCache.swift */; };
78+
D5ACACCD1CD0207300567809 /* SyncHybridCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACCB1CD0207300567809 /* SyncHybridCache.swift */; };
79+
D5ACACD11CD0228400567809 /* SyncCacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACCE1CD0227200567809 /* SyncCacheSpec.swift */; };
80+
D5ACACD51CD0256700567809 /* SyncHybridCacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACD21CD0254300567809 /* SyncHybridCacheSpec.swift */; };
81+
D5ACACD61CD0256700567809 /* SyncHybridCacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACD21CD0254300567809 /* SyncHybridCacheSpec.swift */; };
82+
D5ACACDE1CD0272600567809 /* Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACDD1CD0272600567809 /* Cache.swift */; };
83+
D5ACACDF1CD0272600567809 /* Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACACDD1CD0272600567809 /* Cache.swift */; };
7784
/* End PBXBuildFile section */
7885

7986
/* Begin PBXContainerItemProxy section */
@@ -114,7 +121,6 @@
114121
D5291C261C28220B00B702C9 /* StorageAware.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageAware.swift; sourceTree = "<group>"; };
115122
D5291C271C28220B00B702C9 /* StorageFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageFactory.swift; sourceTree = "<group>"; };
116123
D5291C6A1C2827FB00B702C9 /* BasicHybridCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BasicHybridCache.swift; sourceTree = "<group>"; };
117-
D5291C6B1C2827FB00B702C9 /* Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cache.swift; sourceTree = "<group>"; };
118124
D5291C721C28296B00B702C9 /* HybridCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HybridCache.swift; sourceTree = "<group>"; };
119125
D5291CDA1C2835D000B702C9 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = Carthage/Build/Mac/Nimble.framework; sourceTree = "<group>"; };
120126
D5291CDF1C28374800B702C9 /* SpecHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpecHelper.swift; sourceTree = "<group>"; };
@@ -147,6 +153,11 @@
147153
D5291DA21C2841D200B702C9 /* NSImage+CacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSImage+CacheTests.swift"; sourceTree = "<group>"; };
148154
D5643E351C43F2CC00582E17 /* CustomCache.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = CustomCache.playground; sourceTree = "<group>"; };
149155
D5643E361C43F2CC00582E17 /* HybridCache.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = HybridCache.playground; sourceTree = "<group>"; };
156+
D5ACACC81CD01F3400567809 /* SyncCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SyncCache.swift; sourceTree = "<group>"; };
157+
D5ACACCB1CD0207300567809 /* SyncHybridCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SyncHybridCache.swift; sourceTree = "<group>"; };
158+
D5ACACCE1CD0227200567809 /* SyncCacheSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SyncCacheSpec.swift; sourceTree = "<group>"; };
159+
D5ACACD21CD0254300567809 /* SyncHybridCacheSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SyncHybridCacheSpec.swift; sourceTree = "<group>"; };
160+
D5ACACDD1CD0272600567809 /* Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cache.swift; sourceTree = "<group>"; };
150161
D5DC59E01C20593E003BD79B /* Cache.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Cache.framework; sourceTree = BUILT_PRODUCTS_DIR; };
151162
D5DC5A181C205AC9003BD79B /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
152163
D5DC5A6C1C205C2A003BD79B /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quick.framework; path = Carthage/Build/iOS/Quick.framework; sourceTree = "<group>"; };
@@ -224,7 +235,7 @@
224235
isa = PBXGroup;
225236
children = (
226237
D5291C6A1C2827FB00B702C9 /* BasicHybridCache.swift */,
227-
D5291C6B1C2827FB00B702C9 /* Cache.swift */,
238+
D5ACACDD1CD0272600567809 /* Cache.swift */,
228239
D5291C151C28220B00B702C9 /* Config.swift */,
229240
D5291C161C28220B00B702C9 /* DataStructures */,
230241
D5291C1C1C28220B00B702C9 /* Extensions */,
@@ -261,6 +272,8 @@
261272
isa = PBXGroup;
262273
children = (
263274
D5291C221C28220B00B702C9 /* DefaultCacheConverter.swift */,
275+
D5ACACC81CD01F3400567809 /* SyncCache.swift */,
276+
D5ACACCB1CD0207300567809 /* SyncHybridCache.swift */,
264277
);
265278
path = Library;
266279
sourceTree = "<group>";
@@ -336,6 +349,8 @@
336349
isa = PBXGroup;
337350
children = (
338351
D5291CEF1C28374800B702C9 /* DefaultCacheConverterSpec.swift */,
352+
D5ACACCE1CD0227200567809 /* SyncCacheSpec.swift */,
353+
D5ACACD21CD0254300567809 /* SyncHybridCacheSpec.swift */,
339354
);
340355
path = Library;
341356
sourceTree = "<group>";
@@ -702,6 +717,8 @@
702717
D5291DA11C28405900B702C9 /* UIImage+CacheTests.swift in Sources */,
703718
D5291D381C28389B00B702C9 /* ConfigSpec.swift in Sources */,
704719
D5291D431C28389B00B702C9 /* MemoryStorageSpec.swift in Sources */,
720+
D5ACACD11CD0228400567809 /* SyncCacheSpec.swift in Sources */,
721+
D5ACACD51CD0256700567809 /* SyncHybridCacheSpec.swift in Sources */,
705722
D5291D401C28389B00B702C9 /* String+CacheSpec.swift in Sources */,
706723
D5291D9A1C283DB300B702C9 /* UIImage+CacheSpec.swift in Sources */,
707724
D5291D411C28389B00B702C9 /* DefaultCacheConverterSpec.swift in Sources */,
@@ -716,7 +733,9 @@
716733
files = (
717734
D5291D941C283CFB00B702C9 /* MemoryStorage.swift in Sources */,
718735
D5291D921C283CFB00B702C9 /* DefaultCacheConverter.swift in Sources */,
736+
D5ACACCA1CD01F3400567809 /* SyncCache.swift in Sources */,
719737
D5291D8B1C283CFB00B702C9 /* Expiry.swift in Sources */,
738+
D5ACACDF1CD0272600567809 /* Cache.swift in Sources */,
720739
D5291D891C283CFB00B702C9 /* Cachable.swift in Sources */,
721740
D5291D961C283CFB00B702C9 /* StorageFactory.swift in Sources */,
722741
D5291D7E1C283BF200B702C9 /* HybridCache.swift in Sources */,
@@ -726,11 +745,11 @@
726745
D5291D7D1C283BF200B702C9 /* NSImage+Cache.swift in Sources */,
727746
D5291D881C283CFB00B702C9 /* Config.swift in Sources */,
728747
D5291D951C283CFB00B702C9 /* StorageAware.swift in Sources */,
729-
D5291D871C283CFB00B702C9 /* Cache.swift in Sources */,
730748
D5291D911C283CFB00B702C9 /* String+Cache.swift in Sources */,
731749
D5291D901C283CFB00B702C9 /* NSDate+Cache.swift in Sources */,
732750
D5291D931C283CFB00B702C9 /* DiskStorage.swift in Sources */,
733751
D5291D861C283CFB00B702C9 /* BasicHybridCache.swift in Sources */,
752+
D5ACACCD1CD0207300567809 /* SyncHybridCache.swift in Sources */,
734753
D5291D8D1C283CFB00B702C9 /* StorageKind.swift in Sources */,
735754
D5291D8F1C283CFB00B702C9 /* NSData+Cache.swift in Sources */,
736755
);
@@ -742,6 +761,7 @@
742761
files = (
743762
D5291D9C1C283DD900B702C9 /* NSImage+CacheSpec.swift in Sources */,
744763
D5291D851C283C7C00B702C9 /* SpecHelper+OSX.swift in Sources */,
764+
D5ACACD61CD0256700567809 /* SyncHybridCacheSpec.swift in Sources */,
745765
D5291DA31C2841D200B702C9 /* NSImage+CacheTests.swift in Sources */,
746766
);
747767
runOnlyForDeploymentPostprocessing = 0;
@@ -752,7 +772,9 @@
752772
files = (
753773
D5291C2E1C28220B00B702C9 /* Cachable.swift in Sources */,
754774
D5291C311C28220B00B702C9 /* JSON.swift in Sources */,
775+
D5ACACC91CD01F3400567809 /* SyncCache.swift in Sources */,
755776
D5291C361C28220B00B702C9 /* String+Cache.swift in Sources */,
777+
D5ACACDE1CD0272600567809 /* Cache.swift in Sources */,
756778
D5291C341C28220B00B702C9 /* NSData+Cache.swift in Sources */,
757779
D5291C3A1C28220B00B702C9 /* StorageAware.swift in Sources */,
758780
D5291C381C28220B00B702C9 /* DiskStorage.swift in Sources */,
@@ -761,12 +783,12 @@
761783
D5291C291C28220B00B702C9 /* UIImage+Cache.swift in Sources */,
762784
D5291C351C28220B00B702C9 /* NSDate+Cache.swift in Sources */,
763785
D5291C371C28220B00B702C9 /* DefaultCacheConverter.swift in Sources */,
764-
D5291C6D1C2827FB00B702C9 /* Cache.swift in Sources */,
765786
D5291C321C28220B00B702C9 /* StorageKind.swift in Sources */,
766787
D5291C301C28220B00B702C9 /* Expiry.swift in Sources */,
767788
D5291C331C28220B00B702C9 /* JSON+Cache.swift in Sources */,
768789
D5291C2F1C28220B00B702C9 /* Capsule.swift in Sources */,
769790
D5291C2D1C28220B00B702C9 /* Config.swift in Sources */,
791+
D5ACACCC1CD0207300567809 /* SyncHybridCache.swift in Sources */,
770792
D5291C6C1C2827FB00B702C9 /* BasicHybridCache.swift in Sources */,
771793
D5291C731C28296B00B702C9 /* HybridCache.swift in Sources */,
772794
);

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
66
[![License](https://img.shields.io/cocoapods/l/Cache.svg?style=flat)](http://cocoadocs.org/docsets/Cache)
77
[![Platform](https://img.shields.io/cocoapods/p/Cache.svg?style=flat)](http://cocoadocs.org/docsets/Cache)
8+
[![Documentation](https://img.shields.io/cocoapods/metrics/doc-percent/Cache.svg?style=flat)](http://cocoadocs.org/docsets/Cache)
89
![Swift](https://img.shields.io/badge/%20in-swift%202.2-orange.svg)
910

1011
## Table of Contents
@@ -14,6 +15,8 @@
1415
* [Usage](#usage)
1516
* [Hybrid cache](#hybrid-cache)
1617
* [Type safe cache](#type-safe-cache)
18+
* [SyncCache](#sync-cache)
19+
* [SyncHybridCache](#sync-hybrid-cache)
1720
* [Expiry date](#expiry-date)
1821
* [Cachable protocol](#cachable-protocol)
1922
* [Optional bonuses](#optional-bonuses)
@@ -158,6 +161,43 @@ cache.remove("image")
158161
cache.clear()
159162
```
160163

164+
### SyncHybridCache
165+
166+
`Cache` was born to be async, but if for some reason you need to perform cache
167+
operations synchronously, there is a helper for that.
168+
169+
```swift
170+
let cache = HybridCache(name: "Mix")
171+
let syncCache = SyncHybridCache(cache)
172+
173+
// Add UIImage to cache synchronously
174+
syncCache.add("image", object: UIImage(named: "image.png"))
175+
176+
// Retrieve image from cache synchronously
177+
let image: UIImage? = syncCache.object("image")
178+
179+
// Remove an object from the cache
180+
syncCache.remove("image")
181+
182+
// Clean the cache
183+
syncCache.clear()
184+
```
185+
186+
### SyncCache
187+
188+
`SyncCache` works exactly in the same way as `SyncHybridCache`, the only
189+
difference is that it's a wrapper around a type safe cache.
190+
191+
```swift
192+
let cache = Cache<UIImage>(name: "ImageCache")
193+
let syncCache = SyncCache(cache)
194+
195+
syncCache.add("image", object: UIImage(named: "image.png"))
196+
let image = syncCache.object("image")
197+
syncCache.remove("image")
198+
syncCache.clear()
199+
```
200+
161201
### Expiry date
162202

163203
```swift

Source/Mac/Extensions/NSImage+Cache.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@ import Cocoa
33

44
// MARK: - Cachable
55

6+
/**
7+
Implementation of Cachable protocol.
8+
*/
69
extension NSImage: Cachable {
710

811
public typealias CacheType = NSImage
912

13+
/**
14+
Creates UIImage from NSData
15+
16+
- Parameter data: Data to decode from
17+
*/
1018
public static func decode(data: NSData) -> CacheType? {
1119
let image = NSImage(data: data)
1220
return image
1321
}
1422

23+
/**
24+
Encodes UIImage to NSData
25+
*/
1526
public func encode() -> NSData? {
1627
guard let data = TIFFRepresentation else { return nil }
1728

@@ -25,8 +36,14 @@ extension NSImage: Cachable {
2536

2637
// MARK: - Helpers
2738

39+
/**
40+
Helper UIImage extension.
41+
*/
2842
extension NSImage {
2943

44+
/**
45+
Checks if image has alpha component
46+
*/
3047
var hasAlpha: Bool {
3148
var imageRect:CGRect = CGRectMake(0, 0, size.width, size.height)
3249
let imageRef = CGImageForProposedRect(&imageRect, context: nil, hints: nil)

Source/Mac/HybridCache.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import Cocoa
22

3+
/**
4+
HybridCache supports storing all kinds of objects, as long as they conform to
5+
Cachable protocol. It's two layered cache (with front and back storages), as well as Cache.
6+
Subscribes to system notifications to clear expired cached objects.
7+
*/
38
public class HybridCache: BasicHybridCache {
49

510
// MARK: - Inititalization
611

12+
/**
13+
Creates a new instance of BasicHybridCache and subscribes to system notifications.
14+
15+
- Parameter name: A name of the cache
16+
- Parameter config: Cache configuration
17+
*/
718
public override init(name: String, config: Config = Config.defaultConfig) {
819
super.init(name: name, config: config)
920

@@ -15,20 +26,32 @@ public class HybridCache: BasicHybridCache {
1526
name: NSApplicationDidResignActiveNotification, object: nil)
1627
}
1728

29+
/**
30+
Removes notification center observer.
31+
*/
1832
deinit {
1933
NSNotificationCenter.defaultCenter().removeObserver(self)
2034
}
2135

2236
// MARK: - Notifications
2337

38+
/**
39+
Clears expired cache items when the app recieves memory warning.
40+
*/
2441
func applicationDidReceiveMemoryWarning() {
2542
frontStorage.clearExpired(nil)
2643
}
2744

45+
/**
46+
Clears expired cache items when the app terminates.
47+
*/
2848
func applicationWillTerminate() {
2949
backStorage.clearExpired(nil)
3050
}
3151

52+
/**
53+
Clears expired cache items when the app resign active.
54+
*/
3255
func applicationDidResignActive() {
3356
backStorage.clearExpired(nil)
3457
}

Source/Shared/BasicHybridCache.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import Foundation
22

3+
/**
4+
BasicHybridCache supports storing all kinds of objects, as long as they conform to
5+
Cachable protocol. It's two layered cache (with front and back storages)
6+
*/
37
public class BasicHybridCache: NSObject {
48

9+
/// A name of the cache
510
public let name: String
611

12+
/// Cache configuration
713
let config: Config
14+
/// Front cache (should be less time and memory consuming)
815
let frontStorage: StorageAware
16+
// BAck cache (used for content that outlives the application life-cycle)
917
var backStorage: StorageAware
1018

1119
// MARK: - Inititalization
1220

21+
/**
22+
Creates a new instance of BasicHybridCache.
23+
24+
- Parameter name: A name of the cache
25+
- Parameter config: Cache configuration
26+
*/
1327
public init(name: String, config: Config = Config.defaultConfig) {
1428
self.name = name
1529
self.config = config
@@ -22,6 +36,14 @@ public class BasicHybridCache: NSObject {
2236

2337
// MARK: - Caching
2438

39+
/**
40+
Adds passed object to the front and back cache storages.
41+
42+
- Parameter key: Unique key to identify the object in the cache
43+
- Parameter object: Object that needs to be cached
44+
- Parameter expiry: Expiration date for the cached object
45+
- Parameter completion: Completion closure to be called when the task is done
46+
*/
2547
public func add<T: Cachable>(key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) {
2648
let expiry = expiry ?? config.expiry
2749

@@ -37,6 +59,12 @@ public class BasicHybridCache: NSObject {
3759
}
3860
}
3961

62+
/**
63+
Tries to retrieve the object from to the front and back cache storages.
64+
65+
- Parameter key: Unique key to identify the object in the cache
66+
- Parameter completion: Completion closure returns object or nil
67+
*/
4068
public func object<T: Cachable>(key: String, completion: (object: T?) -> Void) {
4169
frontStorage.object(key) { [weak self] (object: T?) in
4270
if let object = object {
@@ -55,6 +83,12 @@ public class BasicHybridCache: NSObject {
5583
}
5684
}
5785

86+
/**
87+
Removes the object from to the front and back cache storages.
88+
89+
- Parameter key: Unique key to identify the object in the cache
90+
- Parameter completion: Completion closure to be called when the task is done
91+
*/
5892
public func remove(key: String, completion: (() -> Void)? = nil) {
5993
frontStorage.remove(key) { [weak self] in
6094
guard let weakSelf = self else {
@@ -68,6 +102,11 @@ public class BasicHybridCache: NSObject {
68102
}
69103
}
70104

105+
/**
106+
Clears the front and back cache storages.
107+
108+
- Parameter completion: Completion closure to be called when the task is done
109+
*/
71110
public func clear(completion: (() -> Void)? = nil) {
72111
frontStorage.clear() { [weak self] in
73112
guard let weakSelf = self else {

0 commit comments

Comments
 (0)