@@ -13,50 +13,53 @@ public MySqlTimeoutStore(string connectionString, string tablePrefix = "")
13
13
{
14
14
_connectionString = connectionString ;
15
15
_tablePrefix = tablePrefix . ToLower ( ) ;
16
- }
17
-
16
+ }
17
+
18
+ private string ? _initializeSql ;
18
19
public async Task Initialize ( )
19
20
{
20
21
await using var conn = await CreateConnection ( ) ;
21
- var sql = @$ "
22
+ _initializeSql ?? = @$ "
22
23
CREATE TABLE IF NOT EXISTS { _tablePrefix } rfunctions_timeouts (
23
24
function_type_id VARCHAR(255),
24
25
function_instance_id VARCHAR(255),
25
26
timeout_id VARCHAR(255),
26
27
expires BIGINT,
27
28
PRIMARY KEY (function_type_id, function_instance_id, timeout_id)
28
29
)" ;
29
- var command = new MySqlCommand ( sql , conn ) ;
30
+ var command = new MySqlCommand ( _initializeSql , conn ) ;
30
31
await command . ExecuteNonQueryAsync ( ) ;
31
32
}
32
33
34
+ private string ? _truncateSql ;
33
35
public async Task Truncate ( )
34
36
{
35
37
await using var conn = await CreateConnection ( ) ;
36
- var sql = @ $ "TRUNCATE TABLE { _tablePrefix } rfunctions_timeouts";
37
- var command = new MySqlCommand ( sql , conn ) ;
38
+ _truncateSql ??= $ "TRUNCATE TABLE { _tablePrefix } rfunctions_timeouts";
39
+ var command = new MySqlCommand ( _truncateSql , conn ) ;
38
40
await command . ExecuteNonQueryAsync ( ) ;
39
41
}
40
42
43
+ private string ? _upsertTimeoutSql ;
44
+ private string ? _insertTimeoutSql ;
41
45
public async Task UpsertTimeout ( StoredTimeout storedTimeout , bool overwrite )
42
46
{
43
47
var ( functionId , timeoutId , expiry ) = storedTimeout ;
44
48
await using var conn = await CreateConnection ( ) ;
45
- var sql = @$ "
49
+ _upsertTimeoutSql ?? = @$ "
46
50
INSERT INTO { _tablePrefix } rfunctions_timeouts
47
51
(function_type_id, function_instance_id, timeout_id, expires)
48
52
VALUES
49
53
(?, ?, ?, ?)
50
54
ON DUPLICATE KEY UPDATE
51
55
expires = ?" ;
52
-
53
- if ( ! overwrite )
54
- sql = @$ "
56
+ _insertTimeoutSql ??= @$ "
55
57
INSERT IGNORE INTO { _tablePrefix } rfunctions_timeouts
56
58
(function_type_id, function_instance_id, timeout_id, expires)
57
59
VALUES
58
60
(?, ?, ?, ?)" ;
59
-
61
+
62
+ var sql = overwrite ? _upsertTimeoutSql : _insertTimeoutSql ;
60
63
await using var command = new MySqlCommand ( sql , conn )
61
64
{
62
65
Parameters =
@@ -72,17 +75,18 @@ INSERT IGNORE INTO {_tablePrefix}rfunctions_timeouts
72
75
await command . ExecuteNonQueryAsync ( ) ;
73
76
}
74
77
78
+ private string ? _removeTimeoutSql ;
75
79
public async Task RemoveTimeout ( FunctionId functionId , string timeoutId )
76
80
{
77
81
await using var conn = await CreateConnection ( ) ;
78
- var sql = @$ "
82
+ _removeTimeoutSql ?? = @$ "
79
83
DELETE FROM { _tablePrefix } rfunctions_timeouts
80
84
WHERE
81
85
function_type_id = ? AND
82
86
function_instance_id = ? AND
83
87
timeout_id = ?" ;
84
88
85
- await using var command = new MySqlCommand ( sql , conn )
89
+ await using var command = new MySqlCommand ( _removeTimeoutSql , conn )
86
90
{
87
91
Parameters =
88
92
{
@@ -95,14 +99,15 @@ DELETE FROM {_tablePrefix}rfunctions_timeouts
95
99
await command . ExecuteNonQueryAsync ( ) ;
96
100
}
97
101
102
+ private string ? _removeSql ;
98
103
public async Task Remove ( FunctionId functionId )
99
104
{
100
105
await using var conn = await CreateConnection ( ) ;
101
- var sql = @$ "
106
+ _removeSql ?? = @$ "
102
107
DELETE FROM { _tablePrefix } rfunctions_timeouts
103
108
WHERE function_type_id = ? AND function_instance_id = ?" ;
104
109
105
- await using var command = new MySqlCommand ( sql , conn )
110
+ await using var command = new MySqlCommand ( _removeSql , conn )
106
111
{
107
112
Parameters =
108
113
{
@@ -114,15 +119,16 @@ DELETE FROM {_tablePrefix}rfunctions_timeouts
114
119
await command . ExecuteNonQueryAsync ( ) ;
115
120
}
116
121
122
+ private string ? _getTimeoutsSqlExpiresBefore ;
117
123
public async Task < IEnumerable < StoredTimeout > > GetTimeouts ( string functionTypeId , long expiresBefore )
118
124
{
119
125
await using var conn = await DatabaseHelper . CreateOpenConnection ( _connectionString ) ; ;
120
- var sql = @$ "
126
+ _getTimeoutsSqlExpiresBefore ?? = @$ "
121
127
SELECT function_instance_id, timeout_id, expires
122
128
FROM { _tablePrefix } rfunctions_timeouts
123
129
WHERE function_type_id = ? AND expires <= ?" ;
124
130
125
- await using var command = new MySqlCommand ( sql , conn )
131
+ await using var command = new MySqlCommand ( _getTimeoutsSqlExpiresBefore , conn )
126
132
{
127
133
Parameters =
128
134
{
@@ -144,17 +150,18 @@ public async Task<IEnumerable<StoredTimeout>> GetTimeouts(string functionTypeId,
144
150
145
151
return storedTimeouts ;
146
152
}
147
-
153
+
154
+ private string ? _getFunctionTimeoutsSql ;
148
155
public async Task < IEnumerable < StoredTimeout > > GetTimeouts ( FunctionId functionId )
149
156
{
150
157
var ( typeId , instanceId ) = functionId ;
151
158
await using var conn = await DatabaseHelper . CreateOpenConnection ( _connectionString ) ; ;
152
- var sql = @$ "
159
+ _getFunctionTimeoutsSql ?? = @$ "
153
160
SELECT timeout_id, expires
154
161
FROM { _tablePrefix } rfunctions_timeouts
155
162
WHERE function_type_id = ? AND function_instance_id = ?" ;
156
163
157
- await using var command = new MySqlCommand ( sql , conn )
164
+ await using var command = new MySqlCommand ( _getFunctionTimeoutsSql , conn )
158
165
{
159
166
Parameters =
160
167
{
@@ -177,11 +184,12 @@ public async Task<IEnumerable<StoredTimeout>> GetTimeouts(FunctionId functionId)
177
184
178
185
private Task < MySqlConnection > CreateConnection ( ) => DatabaseHelper . CreateOpenConnection ( _connectionString ) ;
179
186
187
+ private string ? _dropUnderlyingTable ;
180
188
public async Task DropUnderlyingTable ( )
181
189
{
182
190
await using var conn = await DatabaseHelper . CreateOpenConnection ( _connectionString ) ;
183
- var sql = $ "DROP TABLE IF EXISTS { _tablePrefix } rfunctions_timeouts";
184
- await using var command = new MySqlCommand ( sql , conn ) ;
191
+ _dropUnderlyingTable ?? = $ "DROP TABLE IF EXISTS { _tablePrefix } rfunctions_timeouts";
192
+ await using var command = new MySqlCommand ( _dropUnderlyingTable , conn ) ;
185
193
await command . ExecuteNonQueryAsync ( ) ;
186
194
}
187
195
}
0 commit comments