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

Add method to clear internal memory cache #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions AwesomeCache/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ open class Cache<T: NSCoding> {
})
}

/// Removes all objects from memory (and keep them in disk cache).
open func removeAllObjectsFromMemory() {
cache.removeAllObjects()
}

// MARK: Subscripting

open subscript(key: String) -> T? {
Expand Down
16 changes: 16 additions & 0 deletions AwesomeCacheTests/AwesomeCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ class AwesomeCacheTests: XCTestCase {
XCTAssertNil(cache.object(forKey: "remove 2"), "Removed object should be nil")
}

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

cache.setObject("AddedString 1", forKey: "remove only from memory 1")
cache.setObject("AddedString 2", forKey: "remove only from memory 2")
XCTAssertNotNil(cache.object(forKey: "remove only from memory 1"), "Added object should not be nil")
XCTAssertNotNil(cache.object(forKey: "remove only from memory 2"), "Added object should not be nil")

cache.removeAllObjectsFromMemory()
XCTAssertNotNil(cache.object(forKey: "remove only from memory 1"), "Removed object should not be nil")
XCTAssertNotNil(cache.object(forKey: "remove only from memory 2"), "Removed object should not be nil")

XCTAssertNil(cache.cache.object(forKey: "remove only from memory 1"), "Removed object should be nil in NSCache")
XCTAssertNil(cache.cache.object(forKey: "remove only from memory 2"), "Removed object should be nil in NSCache")
}

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

Expand Down