Create ThreadSafeCache, which FaviconDownloader uses.

This commit is contained in:
Brent Simmons 2017-11-19 23:58:35 -08:00
parent cb7f324922
commit 86907f6aab
2 changed files with 54 additions and 0 deletions

View File

@ -86,6 +86,8 @@
8461387F1DB3F5BE00048B83 /* RSToolbarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8461387E1DB3F5BE00048B83 /* RSToolbarItem.swift */; };
8479213C1FBA426B004AD08C /* WebViewWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8479213B1FBA426B004AD08C /* WebViewWindow.xib */; };
8479213E1FBA4285004AD08C /* WebViewWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8479213D1FBA4285004AD08C /* WebViewWindowController.swift */; };
848F6AE91FC2BC50002D422E /* ThreadSafeCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848F6AE81FC2BC50002D422E /* ThreadSafeCache.swift */; };
848F6AEA1FC2BC50002D422E /* ThreadSafeCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848F6AE81FC2BC50002D422E /* ThreadSafeCache.swift */; };
849A339D1AC90A0A0015BA09 /* NSTableView+RSCore.h in Headers */ = {isa = PBXBuildFile; fileRef = 849A339B1AC90A0A0015BA09 /* NSTableView+RSCore.h */; settings = {ATTRIBUTES = (Public, ); }; };
849A339E1AC90A0A0015BA09 /* NSTableView+RSCore.m in Sources */ = {isa = PBXBuildFile; fileRef = 849A339C1AC90A0A0015BA09 /* NSTableView+RSCore.m */; };
849B08971BF7BCE30090CEE4 /* NSPasteboard+RSCore.h in Headers */ = {isa = PBXBuildFile; fileRef = 849B08951BF7BCE30090CEE4 /* NSPasteboard+RSCore.h */; settings = {ATTRIBUTES = (Public, ); }; };
@ -202,6 +204,7 @@
8461387E1DB3F5BE00048B83 /* RSToolbarItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RSToolbarItem.swift; sourceTree = "<group>"; };
8479213B1FBA426B004AD08C /* WebViewWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = WebViewWindow.xib; path = AppKit/WebViewWindow.xib; sourceTree = "<group>"; };
8479213D1FBA4285004AD08C /* WebViewWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WebViewWindowController.swift; path = AppKit/WebViewWindowController.swift; sourceTree = "<group>"; };
848F6AE81FC2BC50002D422E /* ThreadSafeCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ThreadSafeCache.swift; path = RSCore/ThreadSafeCache.swift; sourceTree = "<group>"; };
849A339B1AC90A0A0015BA09 /* NSTableView+RSCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSTableView+RSCore.h"; sourceTree = "<group>"; };
849A339C1AC90A0A0015BA09 /* NSTableView+RSCore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTableView+RSCore.m"; sourceTree = "<group>"; };
849B08951BF7BCE30090CEE4 /* NSPasteboard+RSCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSPasteboard+RSCore.h"; sourceTree = "<group>"; };
@ -346,6 +349,7 @@
842E45CB1ED623C7000A8B52 /* UniqueIdentifier.swift */,
84E34DA51F9FA1070077082F /* UndoableCommand.swift */,
8402047D1FBCE77900D94C1A /* BatchUpdate.swift */,
848F6AE81FC2BC50002D422E /* ThreadSafeCache.swift */,
84CFF5241AC3C8A200CEA6C8 /* Foundation */,
84CFF5551AC3CF4A00CEA6C8 /* AppKit */,
84E72E0F1FBD647500B873C1 /* Inspector */,
@ -718,6 +722,7 @@
842DD7DB1E14996300E061EB /* NSData+RSCore.m in Sources */,
842DD7D51E14995C00E061EB /* PlistProviderProtocol.swift in Sources */,
842DD7CE1E14995C00E061EB /* RSPlist.m in Sources */,
848F6AEA1FC2BC50002D422E /* ThreadSafeCache.swift in Sources */,
842DD7EF1E14996300E061EB /* NSString+RSCore.m in Sources */,
842DD7F21E14996B00E061EB /* Date+Extensions.swift in Sources */,
842DD7ED1E14996300E061EB /* NSSet+RSCore.m in Sources */,
@ -790,6 +795,7 @@
849A339E1AC90A0A0015BA09 /* NSTableView+RSCore.m in Sources */,
84CFF51B1AC3C77500CEA6C8 /* RSPlatform.m in Sources */,
84CFF52C1AC3CA9700CEA6C8 /* NSData+RSCore.m in Sources */,
848F6AE91FC2BC50002D422E /* ThreadSafeCache.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -0,0 +1,48 @@
//
// ThreadSafeCache.swift
// RSCore
//
// Created by Brent Simmons on 11/19/17.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public final class ThreadSafeCache<T> {
private var cache = [String: T]()
private let lock = NSLock()
public init() {}
public subscript(_ key: String) -> T? {
get {
return cachedObject(key)
}
set {
if let newValue = newValue {
cacheObject(key, newValue)
}
}
}
private func cachedObject(_ key: String) -> T? {
lock.lock()
defer {
lock.unlock()
}
return cache[key]
}
private func cacheObject(_ key: String, _ value: T) {
lock.lock()
defer {
lock.unlock()
}
cache[key] = value
}
}