Skip to content

Commit f6d6931

Browse files
committed
Use Hasher for generating cache key
1 parent f804f60 commit f6d6931

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

Sources/SwiftyImage.swift

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -121,42 +121,30 @@ open class ImageDrawer {
121121
// MARK: Cache
122122

123123
private static let lock: NSLock = NSLock()
124-
private static var cachedImages = [String: UIImage]()
125-
private var cacheKey: String {
126-
var attributes = [String: String]()
127-
attributes["colors"] = String(self.colors.description.hashValue)
128-
attributes["colorLocations"] = String(self.colorLocations.description.hashValue)
129-
attributes["colorStartPoint"] = String(String(describing: self.colorStartPoint).hashValue)
130-
attributes["colorEndPoint"] = String(String(describing: self.colorEndPoint).hashValue)
131-
132-
attributes["borderColors"] = String(self.borderColors.description.hashValue)
133-
attributes["borderColorLocations"] = String(self.borderColorLocations.description.hashValue)
134-
attributes["borderColorStartPoint"] = String(String(describing: self.borderColorStartPoint).hashValue)
135-
attributes["borderColorEndPoint"] = String(String(describing: self.borderColorEndPoint).hashValue)
136-
attributes["borderWidth"] = String(self.borderWidth.hashValue)
137-
attributes["borderAlignment"] = String(self.borderAlignment.hashValue)
138-
139-
attributes["cornerRadiusTopLeft"] = String(self.cornerRadiusTopLeft.hashValue)
140-
attributes["cornerRadiusTopRight"] = String(self.cornerRadiusTopRight.hashValue)
141-
attributes["cornerRadiusBottomLeft"] = String(self.cornerRadiusBottomLeft.hashValue)
142-
attributes["cornerRadiusBottomRight"] = String(self.cornerRadiusBottomRight.hashValue)
143-
144-
switch self.size {
145-
case .fixed(let size):
146-
attributes["size"] = "Fixed(\(size.width), \(size.height))"
147-
case .resizable:
148-
attributes["size"] = "Resizable"
149-
}
150-
151-
var serializedAttributes = [String]()
152-
for key in attributes.keys.sorted() {
153-
if let value = attributes[key] {
154-
serializedAttributes.append("\(key):\(value)")
155-
}
156-
}
157-
158-
let cacheKey = serializedAttributes.joined(separator: "|")
159-
return cacheKey
124+
private static var cachedImages = [AnyHashable: UIImage]()
125+
private var cacheKey: AnyHashable {
126+
var hasher = Hasher()
127+
128+
hasher.combine(self.colors)
129+
hasher.combine(self.colorLocations)
130+
hasher.combine(String(describing: self.colorStartPoint))
131+
hasher.combine(String(describing: self.colorEndPoint))
132+
133+
hasher.combine(self.borderColors)
134+
hasher.combine(self.borderColorLocations)
135+
hasher.combine(String(describing: self.borderColorStartPoint))
136+
hasher.combine(String(describing: self.borderColorEndPoint))
137+
hasher.combine(self.borderWidth)
138+
hasher.combine(self.borderAlignment)
139+
140+
hasher.combine(self.cornerRadiusTopLeft)
141+
hasher.combine(self.cornerRadiusTopRight)
142+
hasher.combine(self.cornerRadiusBottomLeft)
143+
hasher.combine(self.cornerRadiusBottomRight)
144+
145+
hasher.combine(String(describing: self.size))
146+
147+
return hasher.finalize()
160148
}
161149

162150

SwiftyImageTests/SwiftyImageTests.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,24 @@
88

99
import UIKit
1010
import XCTest
11-
import SwiftyImage
11+
@testable import SwiftyImage
1212

1313
class SwiftyImageTests: XCTestCase {
14+
func testCache() {
15+
XCTAssertEqual(
16+
UIImage.size(width: 10, height: 20).color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image,
17+
UIImage.size(width: 10, height: 20).color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image
18+
)
19+
XCTAssertEqual(
20+
UIImage.resizable().color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image,
21+
UIImage.resizable().color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image
22+
)
23+
XCTAssertNotEqual(
24+
UIImage.resizable().color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image,
25+
UIImage.resizable().color(.red).corner(radius: 5).border(width: 12).border(color: .blue).image
26+
)
27+
}
28+
1429
func testCacheLock() {
1530
for _ in 0..<100 {
1631
DispatchQueue.global().async {

0 commit comments

Comments
 (0)