Skip to content

Commit

Permalink
Rename cache class
Browse files Browse the repository at this point in the history
  • Loading branch information
aschuch committed Jan 31, 2015
1 parent 8876af3 commit b4fed4f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
20 changes: 10 additions & 10 deletions AwesomeCache/AwesomeCache.swift → AwesomeCache/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
/**
* Represents the expiry of a cached object
*/
public enum AwesomeCacheExpiry {
public enum CacheExpiry {
case Never
case Seconds(NSTimeInterval)
case Date(NSDate)
Expand All @@ -25,7 +25,7 @@ public enum AwesomeCacheExpiry {
* Subclassing notes: This class fully supports subclassing.
* The easiest way to implement a subclass is to override `objectForKey` and `setObject:forKey:expires:`, e.g. to modify values prior to reading/writing to the cache.
*/
public class AwesomeCache<T: NSCoding> {
public class Cache<T: NSCoding> {
public let name: String
public let cacheDirectory: String

Expand Down Expand Up @@ -90,11 +90,11 @@ public class AwesomeCache<T: NSCoding> {
* If the error block is called, the object is not cached and the completion block is invoked with this error.
* @param completion Called as soon as a cached object is available to use. The second parameter is true if the object was already cached.
*/
public func setObjectForKey(key: String, cacheBlock: ((T, AwesomeCacheExpiry) -> (), (NSError?) -> ()) -> (), completion: (T?, Bool, NSError?) -> ()) {
public func setObjectForKey(key: String, cacheBlock: ((T, CacheExpiry) -> (), (NSError?) -> ()) -> (), completion: (T?, Bool, NSError?) -> ()) {
if let object = objectForKey(key) {
completion(object, true, nil)
} else {
let successBlock: (T, AwesomeCacheExpiry) -> () = { (obj, expires) in
let successBlock: (T, CacheExpiry) -> () = { (obj, expires) in
self.setObject(obj, forKey: key, expires: expires)
completion(obj, false, nil)
}
Expand All @@ -118,17 +118,17 @@ public class AwesomeCache<T: NSCoding> {
* @return The cached object for the given name, or nil
*/
public func objectForKey(key: String) -> T? {
var possibleObject: AwesomeCacheObject?
var possibleObject: CacheObject?

// Check if object exists in local cache
possibleObject = cache.objectForKey(key) as? AwesomeCacheObject
possibleObject = cache.objectForKey(key) as? CacheObject

if possibleObject == nil {
// Try to load object from disk (synchronously)
dispatch_sync(diskQueue) {
let path = self.pathForKey(key)
if self.fileManager.fileExistsAtPath(path) {
possibleObject = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? AwesomeCacheObject
possibleObject = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? CacheObject
}
}
}
Expand Down Expand Up @@ -166,9 +166,9 @@ public class AwesomeCache<T: NSCoding> {
* @param object The object that should be cached
* @param forKey A key that represents this object in the cache
*/
public func setObject(object: T, forKey key: String, expires: AwesomeCacheExpiry) {
public func setObject(object: T, forKey key: String, expires: CacheExpiry) {
let expiryDate = expiryDateForCacheExpiry(expires)
let cacheObject = AwesomeCacheObject(value: object, expiryDate: expiryDate)
let cacheObject = CacheObject(value: object, expiryDate: expiryDate)

// Set object in local cache
cache.setObject(cacheObject, forKey: key)
Expand Down Expand Up @@ -255,7 +255,7 @@ public class AwesomeCache<T: NSCoding> {
return cacheDirectory.stringByAppendingPathComponent(key).stringByAppendingPathExtension("cache")!
}

private func expiryDateForCacheExpiry(expiry: AwesomeCacheExpiry) -> NSDate {
private func expiryDateForCacheExpiry(expiry: CacheExpiry) -> NSDate {
switch expiry {
case .Never:
return NSDate.distantFuture() as NSDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation
* NOTE: It is currently not possible to use generics with a subclass of NSObject
* However, NSKeyedArchiver needs a concrete subclass of NSObject to work correctly
*/
class AwesomeCacheObject : NSObject, NSCoding {
class CacheObject : NSObject, NSCoding {
let value: AnyObject
let expiryDate: NSDate

Expand Down
4 changes: 2 additions & 2 deletions AwesomeCacheTests/AwesomeCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import AwesomeCache

class AwesomeCacheTests: XCTestCase {

var cache: AwesomeCache<NSString> = AwesomeCache<NSString>(name: "awesomeCache")
var cache: Cache<NSString> = Cache<NSString>(name: "awesomeCache")

override func setUp() {
cache = AwesomeCache<NSString>(name: "awesomeCache")
cache = Cache<NSString>(name: "awesomeCache")
cache.removeAllObjects()

super.setUp()
Expand Down
20 changes: 10 additions & 10 deletions Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
4DC91FAC1A7C54B200C81E10 /* AwesomeCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DC91FAB1A7C54B200C81E10 /* AwesomeCacheTests.swift */; };
4DC91FAF1A7C54B200C81E10 /* AwesomeCache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DC91F981A7C54B200C81E10 /* AwesomeCache.framework */; };
4DC91FB01A7C54B200C81E10 /* AwesomeCache.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 4DC91F981A7C54B200C81E10 /* AwesomeCache.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
4DC91FBA1A7C552900C81E10 /* AwesomeCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DC91FB81A7C552900C81E10 /* AwesomeCache.swift */; };
4DC91FBB1A7C552900C81E10 /* AwesomeCacheObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DC91FB91A7C552900C81E10 /* AwesomeCacheObject.swift */; };
4DC91FBA1A7C552900C81E10 /* Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DC91FB81A7C552900C81E10 /* Cache.swift */; };
4DC91FBB1A7C552900C81E10 /* CacheObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DC91FB91A7C552900C81E10 /* CacheObject.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -73,8 +73,8 @@
4DC91FA21A7C54B200C81E10 /* AwesomeCacheTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AwesomeCacheTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4DC91FAA1A7C54B200C81E10 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4DC91FAB1A7C54B200C81E10 /* AwesomeCacheTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AwesomeCacheTests.swift; sourceTree = "<group>"; };
4DC91FB81A7C552900C81E10 /* AwesomeCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AwesomeCache.swift; sourceTree = "<group>"; };
4DC91FB91A7C552900C81E10 /* AwesomeCacheObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AwesomeCacheObject.swift; sourceTree = "<group>"; };
4DC91FB81A7C552900C81E10 /* Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cache.swift; sourceTree = "<group>"; };
4DC91FB91A7C552900C81E10 /* CacheObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheObject.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -148,8 +148,8 @@
4DC91F991A7C54B200C81E10 /* AwesomeCache */ = {
isa = PBXGroup;
children = (
4DC91FB81A7C552900C81E10 /* AwesomeCache.swift */,
4DC91FB91A7C552900C81E10 /* AwesomeCacheObject.swift */,
4DC91FB81A7C552900C81E10 /* Cache.swift */,
4DC91FB91A7C552900C81E10 /* CacheObject.swift */,
4DC91F9C1A7C54B200C81E10 /* AwesomeCache.h */,
4DC91F9A1A7C54B200C81E10 /* Supporting Files */,
);
Expand Down Expand Up @@ -333,8 +333,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4DC91FBA1A7C552900C81E10 /* AwesomeCache.swift in Sources */,
4DC91FBB1A7C552900C81E10 /* AwesomeCacheObject.swift in Sources */,
4DC91FBA1A7C552900C81E10 /* Cache.swift in Sources */,
4DC91FBB1A7C552900C81E10 /* CacheObject.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -420,7 +420,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -456,7 +456,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
Expand Down
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Backed by NSCache for maximum performance and support for expiry of single objec
## Usage

```swift
let cache = AwesomeCache<NSString>(name: "awesomeCache")
let cache = Cache<NSString>(name: "awesomeCache")

cache["name"] = "Alex"
let name = cache["name"]
Expand Down Expand Up @@ -57,19 +57,40 @@ The completion block is invoked as soon as the cacheBlock is finished and the ob

## Installation

<strike>pod "AwesomeCache", "~> 0.2"</strike>
#### Carthage

For now, just drag and drop the two classes in the `Source` folder into your project.
Add the following line to your [Cartfile](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md#cartfile).

```
github "aschuch/AwesomeCache"
```

Then run `carthage update`.

#### Cocoapods

**NOTE:** Cocoapods does not officially support Swift projects yet. Make sure you have Cocoapods 0.36 beta installed by running `gem install cocoapods --pre`.

Add the following line to your Podfile.

```
pod "AwesomeCache", "~> 1.0"
```

Then run `pod install` with Cocoapods 0.36 or newer.

#### Manually

Just drag and drop the two `.swift` files in the `AwesomeCache` folder into your project.

## Tests

Open the Xcode project and press `⌘-U` to run the tests.

Alternatively, all tests can be run in the terminal using [xctool](https://github.com/facebook/xctool) (once it is ready for Xcode 6).
Alternatively, all tests can be run in the terminal using [xctool](https://github.com/facebook/xctool).

```bash
xctool -scheme Tests -sdk iphonesimulator test
xctool -scheme AwesomeCacheTests -sdk iphonesimulator test
```

## Contributing
Expand Down

0 comments on commit b4fed4f

Please sign in to comment.