Skip to content
Open
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
12 changes: 8 additions & 4 deletions AwesomeCache/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,15 @@ open class Cache<T: NSCoding> {

// Otherwise, read from disk
let path = urlForKey(key).path
if fileManager.fileExists(atPath: path) {
return _awesomeCache_unarchiveObjectSafely(path) as? CacheObject
guard
fileManager.fileExists(atPath: path),
let object = _awesomeCache_unarchiveObjectSafely(path) as? CacheObject else {
return nil
}

return nil

// Keep in memory:
cache.setObject(object, forKey: key as NSString)
return object
}

// Deletes an object from disk
Expand Down
15 changes: 15 additions & 0 deletions AwesomeCacheTests/AwesomeCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ class AwesomeCacheTests: XCTestCase {
XCTAssertNotNil(cache.object(forKey: "add"), "Added object should not be nil")
XCTAssertEqual("AddedString", cache.object(forKey: "add")!, "Fetched object should be equal to the inserted object")
}

func testObjectIsRetainedInMemoryWhenLoadedFromDisk() {
let cache = try! Cache<NSString>(name: "testObjectMemoryRetention")

//Add to cache, both in-memory and on-disk:
cache.setObject("AddedString", forKey: "add")

//purge in-memory cache
cache.cache.removeAllObjects()

//load from disk
_ = cache.object(forKey: "add")

XCTAssertTrue(cache.isOnMemory(forKey: "add"), "Cached object should be loaded from disk and retained in memory")
}

func testGetExpiredObjectIfPresent() {
let cache = try! Cache<NSString>(name: "testGetExpiredObject")
Expand Down