Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove cache task from protocol #9

Merged
merged 5 commits into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?()
}
}