@@ -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