10000 Use Hasher for generating cache key · devxoul/SwiftyImage@f6d6931 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
Use Hasher for generating cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
devxoul committed Jun 6, 2019
1 parent f804f60 commit f6d6931
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
60 changes: 24 additions & 36 deletions Sources/SwiftyImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,30 @@ open class ImageDrawer {
// MARK: Cache

private static let lock: NSLock = NSLock()
private static var cachedImages = [String: UIImage]()
private var cacheKey: String {
var attributes = [String: String]()
attributes["colors"] = String(self.colors.description.hashValue)
attributes["colorLocations"] = String(self.colorLocations.description.hashValue)
attributes["colorStartPoint"] = String(String(describing: self.colorStartPoint).hashValue)
attributes["colorEndPoint"] = String(String(describing: self.colorEndPoint).hashValue)

attributes["borderColors"] = String(self.borderColors.description.hashValue)
attributes["borderColorLocations"] = String(self.borderColorLocations.description.hashValue)
attributes["borderColorStartPoint"] = String(String(describing: self.borderColorStartPoint).hashValue)
attributes["borderColorEndPoint"] = String(String(describing: self.borderColorEndPoint).hashValue)
attributes["borderWidth"] = String(self.borderWidth.hashValue)
attributes["borderAlignment"] = String(self.borderAlignment.hashValue)

attributes["cornerRadiusTopLeft"] = String(self.cornerRadiusTopLeft.hashValue)
attributes["cornerRadiusTopRight"] = String(self.cornerRadiusTopRight.hashValue)
attributes["cornerRadiusBottomLeft"] = String(self.cornerRadiusBottomLeft.hashValue)
attributes["cornerRadiusBottomRight"] = String(self.cornerRadiusBottomRight.hashValue)

switch self.size {
case .fixed(let size):
attributes["size"] = "Fixed(\(size.width), \(size.height))"
case .resizable:
attributes["size"] = "Resizable"
}

var serializedAttributes = [String]()
for key in attributes.keys.sorted() {
if let value = attributes[key] {
serializedAttributes.append("\(key):\(value)")
}
}

let cacheKey = serializedAttributes.joined(separator: "|")
return cacheKey
private static var cachedImages = [AnyHashable: UIImage]()
private var cacheKey: AnyHashable {
var hasher = Hasher()

hasher.combine(self.colors)
hasher.combine(self.colorLocations)
hasher.combine(String(describing: self.colorStartPoint))
hasher.combine(String(describing: self.colorEndPoint))

hasher.combine(self.borderColors)
hasher.combine(self.borderColorLocations)
hasher.combine(String(describing: self.borderColorStartPoint))
hasher.combine(String(describing: self.borderColorEndPoint))
hasher.combine(self.borderWidth)
hasher.combine(self.borderAlignment)

hasher.combine(self.cornerRadiusTopLeft)
hasher.combine(self.cornerRadiusTopRight)
hasher.combine(self.cornerRadiusBottomLeft)
hasher.combine(self.cornerRadiusBottomRight)

hasher.combine(String(describing: self.size))

return hasher.finalize()
}


Expand Down
17 changes: 16 additions & 1 deletion SwiftyImageTests/SwiftyImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@

import UIKit
import XCTest
import SwiftyImage
@testable import SwiftyImage

class SwiftyImageTests: XCTestCase {
func testCache() {
XCTAssertEqual(
UIImage.size(width: 10, height: 20).color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image,
UIImage.size(width: 10, height: 20).color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image
)
XCTAssertEqual(
UIImage.resizable().color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image,
UIImage.resizable().color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image
)
XCTAssertNotEqual(
UIImage.resizable().color(.blue).corner(radius: 5).border(width: 12).border(color: .red).image,
UIImage.resizable().color(.red).corner(radius: 5).border(width: 12).border(color: .blue).image
)
}

func testCacheLock() {
for _ in 0..<100 {
DispatchQueue.global().async {
Expand Down

0 comments on commit f6d6931

Please sign in to comment.
0