Skip to content

Commit

Permalink
Merge pull request #34 from aciidb0mb3r/remove_all_cache
Browse files Browse the repository at this point in the history
Add initial implementation of remove all cache
  • Loading branch information
nickoneill committed May 30, 2016
2 parents 506794f + 323bfbb commit 31bb78e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Pantry/JSONWarehouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public class JSONWarehouse: Warehouseable, WarehouseCacheable {
}
}

static func removeAllCache() {
try! NSFileManager.defaultManager().removeItemAtURL(JSONWarehouse.cacheDirectory)
}

func loadCache() -> AnyObject? {

guard context == nil else {
Expand Down Expand Up @@ -163,13 +167,17 @@ public class JSONWarehouse: Warehouseable, WarehouseCacheable {
}
}

func cacheFileURL() -> NSURL {
static var cacheDirectory: NSURL {
let url = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first!

let writeDirectory = url.URLByAppendingPathComponent("com.thatthinginswift.pantry")
let cacheLocation = writeDirectory.URLByAppendingPathComponent(self.key)

try! NSFileManager.defaultManager().createDirectoryAtURL(writeDirectory, withIntermediateDirectories: true, attributes: nil)
return writeDirectory
}

func cacheFileURL() -> NSURL {
let cacheDirectory = JSONWarehouse.cacheDirectory
let cacheLocation = cacheDirectory.URLByAppendingPathComponent(self.key)
try! NSFileManager.defaultManager().createDirectoryAtURL(cacheDirectory, withIntermediateDirectories: true, attributes: nil)

return cacheLocation
}
Expand Down
4 changes: 4 additions & 0 deletions Pantry/MemoryWarehouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ extension MemoryWarehouse: WarehouseCacheable {
func removeCache() {
MemoryWarehouse.globalCache.removeValueForKey(key)
}

static func removeAllCache() {
MemoryWarehouse.globalCache = [:]
}

func loadCache() -> AnyObject? {

Expand Down
9 changes: 9 additions & 0 deletions Pantry/Pantry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ public class Pantry {

warehouse.removeCache()
}

/// Deletes all the cache
///
/// - Note: This will clear in-memory as well as JSON cache
public static func removeAllCache() {
///Blindly remove all the data!
MemoryWarehouse.removeAllCache()
JSONWarehouse.removeAllCache()
}

public static func itemExistsForKey(key: String) -> Bool {
let warehouse = getWarehouse(key)
Expand Down
1 change: 1 addition & 0 deletions Pantry/WarehouseCacheable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation
protocol WarehouseCacheable {
func write(object: AnyObject, expires: StorageExpiry)
func removeCache()
static func removeAllCache()
func loadCache() -> AnyObject?
func cacheExists() -> Bool
}

0 comments on commit 31bb78e

Please sign in to comment.