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

[Cache] Added size property to get the total size of cache located on… #301

Merged
merged 1 commit into from
Mar 29, 2016
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
10 changes: 10 additions & 0 deletions Haneke/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ public class Cache<T: DataConvertible where T.Result == T, T : DataRepresentable
}
}

// MARK: Size

public var size: UInt64 {
var size: UInt64 = 0
for (_, (_, _, diskCache)) in self.formats {
dispatch_sync(diskCache.cacheQueue) { size += diskCache.size }
}
return size
}

// MARK: Notifications

func onMemoryWarning() {
Expand Down
42 changes: 42 additions & 0 deletions HanekeTests/CacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CacheTests: XCTestCase {

XCTAssertNotNil(sut.memoryWarningObserver)
XCTAssertEqual(name, sut.name)
XCTAssertEqual(Int(sut.size), 0)
}

func testDeinit() {
Expand Down Expand Up @@ -78,6 +79,47 @@ class CacheTests: XCTestCase {
sut.addFormat(format)
}

// size

func testSize_WithOneFormat() {
let data = NSData.dataWithLength(6)
let key = self.name
let format = Format<NSData>(name: self.name)
sut.addFormat(format)

var finished = false
sut.set(value: data, key: key, formatName : format.name, success: { _ in
finished = true
})

XCTAssert(finished, "set completed not in main queue")
XCTAssertEqual(sut.size, UInt64(data.length))
}

func testSize_WithTwoFormats() {
let lengths = [4, 7]
let formats = (0..<lengths.count).map { (index: Int) -> Format<NSData> in
let formatName = self.name + String(index)
return Format<NSData>(name: formatName)
}
formats.forEach(sut.addFormat)
let lenghtsByFormats = zip(lengths, formats)

lenghtsByFormats.forEach { (length: Int, format: Format<NSData>) in
let data = NSData.dataWithLength(length)
let key = self.name

var finished = false
sut.set(value: data, key: key, formatName : format.name, success: { _ in
finished = true
})

XCTAssert(finished, "set completed not in main queue")
}

XCTAssertEqual(sut.size, UInt64(lengths.reduce(0, combine: +)))
}

// MARK: set

func testSet_WithIdentityFormat_ExpectSyncSuccess() {
Expand Down