Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41832e0

Browse files
Merge pull request #31 from vapor/nio
thread specific table name caches
2 parents 9424b61 + 518f073 commit 41832e0

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

‎Sources/FluentPostgreSQL/FluentPostgreSQLProvider.swift‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ public final class FluentPostgreSQLProvider: Provider {
4040

4141
/// See `Provider.boot(_:)`
4242
public func didBoot(_ worker: Container) throws -> Future<Void> {
43-
return worker.withConnection(to: .psql) { conn in
44-
return conn.simpleQuery("select * from pg_class").map(to: Void.self) { rows in
45-
print(rows)
46-
}
47-
}
43+
return .done(on: worker)
4844
}
4945
}
5046

‎Sources/FluentPostgreSQL/PostgreSQLTableNameCache.swift‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,34 @@ final class PostgreSQLTableNameCache {
66
var storage: [Int32: String]
77

88
/// Static shared cache, stored by connections.
9-
private static var shared: [ObjectIdentifier: PostgreSQLTableNameCache] = [:]
9+
private static var _shared: ThreadSpecificVariable<PostgreSQLTableNameCaches> = .init()
10+
11+
/// Getter for `_shared` that will initialize if it has not already been initialized.
12+
private static var shared: PostgreSQLTableNameCaches {
13+
get {
14+
if let existing = _shared.currentValue {
15+
return existing
16+
} else {
17+
let new = PostgreSQLTableNameCaches()
18+
_shared.currentValue = new
19+
return new
20+
}
21+
}
22+
}
1023

1124
/// Creates a new cache.
12-
private init(cache: [Int32: String]) {
25+
private init(_ cache: [Int32: String]) {
1326
self.storage = cache
1427
}
1528

1629
/// Invalidates the cache for the supplied connection.
1730
static func invalidate(for connection: PostgreSQLConnection) {
18-
shared[ObjectIdentifier(connection)] = nil
31+
shared.storage[ObjectIdentifier(connection)] = nil
1932
}
2033

2134
/// Generates a cache for the supplied connection.
2235
static func get(for connection: PostgreSQLConnection) -> Future<PostgreSQLTableNameCache> {
23-
if let existing = shared[ObjectIdentifier(connection)] {
36+
if let existing = shared.storage[ObjectIdentifier(connection)] {
2437
return Future.map(on: connection) { existing }
2538
} else {
2639
return connection.simpleQuery("select oid, relname from pg_class").map(to: PostgreSQLTableNameCache.self) { rows in
@@ -32,10 +45,21 @@ final class PostgreSQLTableNameCache {
3245
cache[oid] = name
3346
}
3447

35-
let new = PostgreSQLTableNameCache(cache: cache)
36-
shared[ObjectIdentifier(connection)] = new
48+
let new = PostgreSQLTableNameCache(cache)
49+
shared.storage[ObjectIdentifier(connection)] = new
3750
return new
3851
}
3952
}
4053
}
4154
}
55+
56+
/// Stores connection caches per thread.
57+
final class PostgreSQLTableNameCaches {
58+
/// Psql connection is used as object id.
59+
var storage: [ObjectIdentifier: PostgreSQLTableNameCache]
60+
61+
/// Creates a new cache.
62+
internal init() {
63+
self.storage = [:]
64+
}
65+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /