@@ -15,28 +15,30 @@ public PostgreSqlTimeoutStore(string connectionString, string tablePrefix = "")
15
15
{
16
16
_connectionString = connectionString ;
17
17
_tablePrefix = tablePrefix . ToLower ( ) ;
18
- }
19
-
18
+ }
19
+
20
+ private string ? _initializeSql ;
20
21
public async Task Initialize ( )
21
22
{
22
23
await using var conn = await CreateConnection ( ) ;
23
- var sql = @$ "
24
+ _initializeSql ?? = @$ "
24
25
CREATE TABLE IF NOT EXISTS { _tablePrefix } _timeouts (
25
26
function_type_id VARCHAR(255),
26
27
function_instance_id VARCHAR(255),
27
28
timeout_id VARCHAR(255),
28
29
expires BIGINT,
29
30
PRIMARY KEY (function_type_id, function_instance_id, timeout_id)
30
31
)" ;
31
- var command = new NpgsqlCommand ( sql , conn ) ;
32
+ var command = new NpgsqlCommand ( _initializeSql , conn ) ;
32
33
await command . ExecuteNonQueryAsync ( ) ;
33
34
}
34
-
35
+
36
+ private string ? _truncateSql ;
35
37
public async Task Truncate ( )
36
38
{
37
39
await using var conn = await CreateConnection ( ) ;
38
- var sql = $ "TRUNCATE TABLE { _tablePrefix } _timeouts";
39
- var command = new NpgsqlCommand ( sql , conn ) ;
40
+ _truncateSql ?? = $ "TRUNCATE TABLE { _tablePrefix } _timeouts";
41
+ var command = new NpgsqlCommand ( _truncateSql , conn ) ;
40
42
await command . ExecuteNonQueryAsync ( ) ;
41
43
}
42
44
@@ -46,27 +48,29 @@ private async Task<NpgsqlConnection> CreateConnection()
46
48
await conn . OpenAsync ( ) ;
47
49
return conn ;
48
50
}
49
-
51
+
52
+ private string ? _upsertTimeoutSql ;
53
+ private string ? _insertTimeoutSql ;
50
54
public async Task UpsertTimeout ( StoredTimeout storedTimeout , bool overwrite )
51
55
{
52
56
var ( functionId , timeoutId , expiry ) = storedTimeout ;
53
57
await using var conn = await CreateConnection ( ) ;
54
- var sql = @$ "
58
+ _upsertTimeoutSql ?? = @$ "
55
59
INSERT INTO { _tablePrefix } _timeouts
56
60
(function_type_id, function_instance_id, timeout_id, expires)
57
61
VALUES
58
62
($1, $2, $3, $4)
59
63
ON CONFLICT (function_type_id, function_instance_id, timeout_id)
60
64
DO UPDATE SET expires = EXCLUDED.expires" ;
61
-
62
- if ( ! overwrite )
63
- sql = @$ "
64
- INSERT INTO { _tablePrefix } _timeouts
65
- (function_type_id, function_instance_id, timeout_id, expires)
66
- VALUES
67
- ($1, $2, $3, $4)
68
- ON CONFLICT DO NOTHING" ;
69
-
65
+
66
+ _insertTimeoutSql ??= @$ "
67
+ INSERT INTO { _tablePrefix } _timeouts
68
+ (function_type_id, function_instance_id, timeout_id, expires)
69
+ VALUES
70
+ ($1, $2, $3, $4)
71
+ ON CONFLICT DO NOTHING" ;
72
+
73
+ var sql = overwrite ? _upsertTimeoutSql : _insertTimeoutSql ;
70
74
await using var command = new NpgsqlCommand ( sql , conn )
71
75
{
72
76
Parameters =
@@ -81,17 +85,18 @@ INSERT INTO {_tablePrefix}_timeouts
81
85
await command . ExecuteNonQueryAsync ( ) ;
82
86
}
83
87
88
+ private string ? _removeTimeoutSql ;
84
89
public async Task RemoveTimeout ( FunctionId functionId , string timeoutId )
85
90
{
86
91
await using var conn = await CreateConnection ( ) ;
87
- var sql = @$ "
92
+ _removeTimeoutSql ?? = @$ "
88
93
DELETE FROM { _tablePrefix } _timeouts
89
94
WHERE
90
95
function_type_id = $1 AND
91
96
function_instance_id = $2 AND
92
97
timeout_id = $3" ;
93
98
94
- await using var command = new NpgsqlCommand ( sql , conn )
99
+ await using var command = new NpgsqlCommand ( _removeTimeoutSql , conn )
95
100
{
96
101
Parameters =
97
102
{
@@ -104,14 +109,15 @@ DELETE FROM {_tablePrefix}_timeouts
104
109
await command . ExecuteNonQueryAsync ( ) ;
105
110
}
106
111
112
+ private string ? _removeSql ;
107
113
public async Task Remove ( FunctionId functionId )
108
114
{
109
115
await using var conn = await CreateConnection ( ) ;
110
- var sql = @$ "
116
+ _removeSql ?? = @$ "
111
117
DELETE FROM { _tablePrefix } _timeouts
112
118
WHERE function_type_id = $1 AND function_instance_id = $2" ;
113
119
114
- await using var command = new NpgsqlCommand ( sql , conn )
120
+ await using var command = new NpgsqlCommand ( _removeSql , conn )
115
121
{
116
122
Parameters =
117
123
{
@@ -123,17 +129,18 @@ DELETE FROM {_tablePrefix}_timeouts
123
129
await command . ExecuteNonQueryAsync ( ) ;
124
130
}
125
131
132
+ private string ? _getTimeoutsSqlExpiresBefore ;
126
133
public async Task < IEnumerable < StoredTimeout > > GetTimeouts ( string functionTypeId , long expiresBefore )
127
134
{
128
135
await using var conn = await CreateConnection ( ) ;
129
- var sql = @$ "
136
+ _getTimeoutsSqlExpiresBefore ?? = @$ "
130
137
SELECT function_instance_id, timeout_id, expires
131
138
FROM { _tablePrefix } _timeouts
132
139
WHERE
133
140
function_type_id = $1 AND
134
141
expires <= $2" ;
135
142
136
- await using var command = new NpgsqlCommand ( sql , conn )
143
+ await using var command = new NpgsqlCommand ( _getTimeoutsSqlExpiresBefore , conn )
137
144
{
138
145
Parameters =
139
146
{
@@ -156,16 +163,17 @@ public async Task<IEnumerable<StoredTimeout>> GetTimeouts(string functionTypeId,
156
163
return storedMessages ;
157
164
}
158
165
166
+ private string ? _getTimeoutsSql ;
159
167
public async Task < IEnumerable < StoredTimeout > > GetTimeouts ( FunctionId functionId )
160
168
{
161
169
var ( typeId , instanceId ) = functionId ;
162
170
await using var conn = await CreateConnection ( ) ;
163
- var sql = @$ "
171
+ _getTimeoutsSql ?? = @$ "
164
172
SELECT timeout_id, expires
165
173
FROM { _tablePrefix } _timeouts
166
174
WHERE function_type_id = $1 AND function_instance_id = $2" ;
167
175
168
- await using var command = new NpgsqlCommand ( sql , conn )
176
+ await using var command = new NpgsqlCommand ( _getTimeoutsSql , conn )
169
177
{
170
178
Parameters =
171
179
{
@@ -186,11 +194,12 @@ public async Task<IEnumerable<StoredTimeout>> GetTimeouts(FunctionId functionId)
186
194
return storedMessages ;
187
195
}
188
196
197
+ private string ? _dropUnderlyingTableSql ;
189
198
public async Task DropUnderlyingTable ( )
190
199
{
191
200
await using var conn = await CreateConnection ( ) ;
192
- var sql = $ "DROP TABLE IF EXISTS { _tablePrefix } _timeouts;";
193
- var command = new NpgsqlCommand ( sql , conn ) ;
201
+ _dropUnderlyingTableSql ?? = $ "DROP TABLE IF EXISTS { _tablePrefix } _timeouts;";
202
+ var command = new NpgsqlCommand ( _dropUnderlyingTableSql , conn ) ;
194
203
await command . ExecuteNonQueryAsync ( ) ;
195
204
}
196
205
}
0 commit comments