Skip to content

Commit

Permalink
Merge pull request #9 from hyperoslo/refactor/cache-task
Browse files Browse the repository at this point in the history
Remove cache task from protocol
  • Loading branch information
zenangst committed Nov 11, 2015
2 parents ec8fd21 + 4f04b82 commit 5aa7964
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 89 deletions.
8 changes: 4 additions & 4 deletions Source/CacheAware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public protocol CacheAware {

init(name: String)

func add<T: Cachable>(key: String, object: T, start: Bool, completion: (() -> Void)?) -> CacheTask?
func object<T: Cachable>(key: String, start: Bool, completion: (object: T?) -> Void) -> CacheTask?
func remove(key: String, start: Bool, completion: (() -> Void)?) -> CacheTask?
func clear(start: Bool, completion: (() -> Void)?) -> CacheTask?
func add<T: Cachable>(key: String, object: T, completion: (() -> Void)?)
func object<T: Cachable>(key: String, completion: (object: T?) -> Void)
func remove(key: String, completion: (() -> Void)?)
func clear(completion: (() -> Void)?)
}
2 changes: 1 addition & 1 deletion Source/CacheTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CacheTask {
}
}

func start() -> Self {
func start() -> CacheTask {
dispatch_async(dispatch_get_main_queue(), block)
return self
}
Expand Down
87 changes: 34 additions & 53 deletions Source/DiskCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,83 +27,64 @@ public class DiskCache: CacheAware {

// MARK: - CacheAware

public func add<T: Cachable>(key: String, object: T, start: Bool = true, completion: (() -> Void)? = nil) -> CacheTask? {
let task = CacheTask { [weak self] in
guard let weakSelf = self else { return }

if !weakSelf.fileManager.fileExistsAtPath(weakSelf.path) {
do {
try weakSelf.fileManager.createDirectoryAtPath(weakSelf.path,
withIntermediateDirectories: true, attributes: nil)
} catch _ {}
}
public func add<T: Cachable>(key: String, object: T, completion: (() -> Void)? = nil) {
if !fileManager.fileExistsAtPath(path) {
do {
try fileManager.createDirectoryAtPath(path,
withIntermediateDirectories: true, attributes: nil)
} catch _ {}
}

weakSelf.fileManager.createFileAtPath(
weakSelf.filePath(key),
contents: object.encode(), attributes: nil)
fileManager.createFileAtPath(filePath(key),
contents: object.encode(), attributes: nil)

dispatch_async(dispatch_get_main_queue()) {
completion?()
}
dispatch_async(dispatch_get_main_queue()) {
completion?()
}

return start ? task.start() : task
}

public func object<T: Cachable>(key: String, start: Bool = true, completion: (object: T?) -> Void) -> CacheTask? {
let task = CacheTask { [weak self] in
public func object<T: Cachable>(key: String, completion: (object: T?) -> Void) {
dispatch_async(ioQueue) { [weak self] in
guard let weakSelf = self else { return }

dispatch_async(weakSelf.ioQueue) {
let filePath = weakSelf.filePath(key)
var cachedObject: T?
if let data = NSData(contentsOfFile: filePath) {
cachedObject = T.decode(data)
}
let filePath = weakSelf.filePath(key)
var cachedObject: T?
if let data = NSData(contentsOfFile: filePath) {
cachedObject = T.decode(data)
}

dispatch_async(dispatch_get_main_queue()) {
completion(object: cachedObject)
}
dispatch_async(dispatch_get_main_queue()) {
completion(object: cachedObject)
}
}

return start ? task.start() : task
}

public func remove(key: String, start: Bool = true, completion: (() -> Void)? = nil) -> CacheTask? {
let task = CacheTask { [weak self] in
public func remove(key: String, completion: (() -> Void)? = nil) {
dispatch_async(ioQueue) { [weak self] in
guard let weakSelf = self else { return }

dispatch_async(weakSelf.ioQueue) {
do {
try weakSelf.fileManager.removeItemAtPath(weakSelf.filePath(key))
} catch _ {}
do {
try weakSelf.fileManager.removeItemAtPath(weakSelf.filePath(key))
} catch _ {}

dispatch_async(dispatch_get_main_queue()) {
completion?()
}
dispatch_async(dispatch_get_main_queue()) {
completion?()
}
}

return start ? task.start() : task
}

public func clear(start: Bool = true, completion: (() -> Void)? = nil) -> CacheTask? {
let task = CacheTask { [weak self] in
public func clear(completion: (() -> Void)? = nil) {
dispatch_async(ioQueue) { [weak self] in
guard let weakSelf = self else { return }

dispatch_async(weakSelf.ioQueue) {
do {
try weakSelf.fileManager.removeItemAtPath(weakSelf.path)
} catch _ {}
do {
try weakSelf.fileManager.removeItemAtPath(weakSelf.path)
} catch _ {}

dispatch_async(dispatch_get_main_queue()) {
completion?()
}
dispatch_async(dispatch_get_main_queue()) {
completion?()
}
}

return start ? task.start() : task
}

// MARK: - Helpers
Expand Down
42 changes: 11 additions & 31 deletions Source/MemoryCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,22 @@ public class MemoryCache: CacheAware {

// MARK: - CacheAware

public func add<T: Cachable>(key: String, object: T, start: Bool = true, completion: (() -> Void)? = nil) -> CacheTask? {
let task = CacheTask { [weak self] in
guard let weakSelf = self else { return }
weakSelf.cache.setObject(object, forKey: key)
}

return start ? task.start() : task
public func add<T: Cachable>(key: String, object: T, completion: (() -> Void)? = nil) {
cache.setObject(object, forKey: key)
}

public func object<T: Cachable>(key: String, start: Bool = true, completion: (object: T?) -> Void) -> CacheTask? {
let task = CacheTask { [weak self] in
guard let weakSelf = self else { return }
let cachedObject = weakSelf.cache.objectForKey(key) as? T
completion(object: cachedObject)
}

return start ? task.start() : task
public func object<T: Cachable>(key: String, completion: (object: T?) -> Void) {
let cachedObject = cache.objectForKey(key) as? T
completion(object: cachedObject)
}

public func remove(key: String, start: Bool = true, completion: (() -> Void)? = nil) -> CacheTask? {
let task = CacheTask { [weak self] in
guard let weakSelf = self else { return }
weakSelf.cache.removeObjectForKey(key)
completion?()
}

return start ? task.start() : task
public func remove(key: String, completion: (() -> Void)? = nil) {
cache.removeObjectForKey(key)
completion?()
}

public func clear(start: Bool = true, completion: (() -> Void)? = nil) -> CacheTask? {
let task = CacheTask { [weak self] in
guard let weakSelf = self else { return }
weakSelf.cache.removeAllObjects()
completion?()
}

return start ? task.start() : task
public func clear(completion: (() -> Void)? = nil) {
cache.removeAllObjects()
completion?()
}
}

0 comments on commit 5aa7964

Please sign in to comment.