Create ObjectCache and start using it.
This commit is contained in:
parent
bf11f88ea7
commit
32ae8ab1d8
|
@ -14,9 +14,9 @@ import Data
|
|||
|
||||
final class StatusesTable: DatabaseTable {
|
||||
|
||||
var cachedStatuses = [String: ArticleStatus]()
|
||||
let name: String
|
||||
let queue: RSDatabaseQueue
|
||||
let cache = ObjectCache<ArticleStatus>(keyPathForID: \ArticleStatus.articleID)
|
||||
|
||||
init(name: String, queue: RSDatabaseQueue) {
|
||||
|
||||
|
@ -35,11 +35,11 @@ final class StatusesTable: DatabaseTable {
|
|||
|
||||
articles.forEach { (oneArticle) in
|
||||
|
||||
if let cachedStatus = cachedStatusForArticleID(oneArticle.articleID) {
|
||||
if let cachedStatus = cache[oneArticle.articleID] {
|
||||
oneArticle.status = cachedStatus
|
||||
}
|
||||
else if let oneArticleStatus = oneArticle.status {
|
||||
cacheStatus(oneArticleStatus)
|
||||
cache.add(oneArticleStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -170,11 +170,6 @@ private extension StatusesManager {
|
|||
|
||||
// MARK: Cache
|
||||
|
||||
func cachedStatusForArticleID(_ articleID: String) -> ArticleStatus? {
|
||||
|
||||
return cachedStatuses[articleID]
|
||||
}
|
||||
|
||||
func cacheStatus(_ status: ArticleStatus) {
|
||||
|
||||
cacheStatuses(Set([status]))
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
84419AE71B5ABD7F00C26BB2 /* RSDatabaseQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = 84419ADD1B5ABD7F00C26BB2 /* RSDatabaseQueue.m */; };
|
||||
84419B051B5ABFF700C26BB2 /* FMResultSet+RSExtras.h in Headers */ = {isa = PBXBuildFile; fileRef = 84419B031B5ABFF700C26BB2 /* FMResultSet+RSExtras.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
84419B061B5ABFF700C26BB2 /* FMResultSet+RSExtras.m in Sources */ = {isa = PBXBuildFile; fileRef = 84419B041B5ABFF700C26BB2 /* FMResultSet+RSExtras.m */; };
|
||||
844D97411F2D32F300CEDDEA /* ObjectCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844D97401F2D32F300CEDDEA /* ObjectCache.swift */; };
|
||||
849BF8C61C94FB8E0071D1DA /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 849BF8C51C94FB8E0071D1DA /* libsqlite3.tbd */; };
|
||||
84DDF1961C94FC45005E6CF5 /* FMDatabase.h in Headers */ = {isa = PBXBuildFile; fileRef = 84DDF18B1C94FC45005E6CF5 /* FMDatabase.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
84DDF1971C94FC45005E6CF5 /* FMDatabase.m in Sources */ = {isa = PBXBuildFile; fileRef = 84DDF18C1C94FC45005E6CF5 /* FMDatabase.m */; };
|
||||
|
@ -68,6 +69,7 @@
|
|||
84419ADD1B5ABD7F00C26BB2 /* RSDatabaseQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RSDatabaseQueue.m; path = RSDatabase/RSDatabaseQueue.m; sourceTree = "<group>"; };
|
||||
84419B031B5ABFF700C26BB2 /* FMResultSet+RSExtras.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "FMResultSet+RSExtras.h"; path = "RSDatabase/FMResultSet+RSExtras.h"; sourceTree = "<group>"; };
|
||||
84419B041B5ABFF700C26BB2 /* FMResultSet+RSExtras.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "FMResultSet+RSExtras.m"; path = "RSDatabase/FMResultSet+RSExtras.m"; sourceTree = "<group>"; };
|
||||
844D97401F2D32F300CEDDEA /* ObjectCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ObjectCache.swift; path = RSDatabase/ObjectCache.swift; sourceTree = "<group>"; };
|
||||
849BF8C51C94FB8E0071D1DA /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = usr/lib/libsqlite3.tbd; sourceTree = SDKROOT; };
|
||||
84DDF18B1C94FC45005E6CF5 /* FMDatabase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FMDatabase.h; sourceTree = "<group>"; };
|
||||
84DDF18C1C94FC45005E6CF5 /* FMDatabase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FMDatabase.m; sourceTree = "<group>"; };
|
||||
|
@ -154,6 +156,7 @@
|
|||
84419AD81B5ABD7400C26BB2 /* NSString+RSDatabase.h */,
|
||||
84419AD91B5ABD7400C26BB2 /* NSString+RSDatabase.m */,
|
||||
840405DA1F1C158C00DF0296 /* DatabaseTable.swift */,
|
||||
844D97401F2D32F300CEDDEA /* ObjectCache.swift */,
|
||||
84DDF18A1C94FC45005E6CF5 /* FMDB */,
|
||||
84F22C5A1B52E0D9000060CE /* Info.plist */,
|
||||
849BF8C51C94FB8E0071D1DA /* libsqlite3.tbd */,
|
||||
|
@ -368,6 +371,7 @@
|
|||
84DDF1A01C94FC45005E6CF5 /* FMResultSet.m in Sources */,
|
||||
84419B061B5ABFF700C26BB2 /* FMResultSet+RSExtras.m in Sources */,
|
||||
84DDF1991C94FC45005E6CF5 /* FMDatabaseAdditions.m in Sources */,
|
||||
844D97411F2D32F300CEDDEA /* ObjectCache.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
//
|
||||
// ObjectCache.swift
|
||||
// RSDatabase
|
||||
//
|
||||
// Created by Brent Simmons on 7/29/17.
|
||||
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public final class ObjectCache<T> {
|
||||
|
||||
private let keyPathForID: KeyPath<T,String>
|
||||
private var dictionary = [String: T]()
|
||||
|
||||
public init(keyPathForID: KeyPath<T,String>) {
|
||||
|
||||
self.keyPathForID = keyPathForID
|
||||
}
|
||||
|
||||
public func addObjects(_ objects: [T]) {
|
||||
|
||||
objects.forEach { addObject($0) }
|
||||
}
|
||||
|
||||
public func add(_ object: T) {
|
||||
|
||||
let identifier = identifierForObject(object)
|
||||
self[identifier] = object
|
||||
}
|
||||
|
||||
public func removeObjects(_ objects: [T]) {
|
||||
|
||||
objects.forEach { removeObject($0) }
|
||||
}
|
||||
|
||||
public func removeObject(_ object: T) {
|
||||
|
||||
let identifier = identifierForObject(object)
|
||||
self[identifier] = nil
|
||||
}
|
||||
|
||||
public subscript(_ identifier: String) -> T? {
|
||||
get {
|
||||
return dictionary[identifier]
|
||||
}
|
||||
set {
|
||||
dictionary[identifier] = T
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension ObjectCache {
|
||||
|
||||
func identifierForObject(_ object: T) -> String {
|
||||
|
||||
return object[keyPath: keyPathForID]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue