Skip to content

Commit 625711e

Browse files
committed
Minimize sql-script string generations in MySqlTimeoutStore
1 parent 985fa04 commit 625711e

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

Stores/MySQL/Cleipnir.ResilientFunctions.MySQL/MySqlTimeoutStore.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,53 @@ public MySqlTimeoutStore(string connectionString, string tablePrefix = "")
1313
{
1414
_connectionString = connectionString;
1515
_tablePrefix = tablePrefix.ToLower();
16-
}
17-
16+
}
17+
18+
private string? _initializeSql;
1819
public async Task Initialize()
1920
{
2021
await using var conn = await CreateConnection();
21-
var sql = @$"
22+
_initializeSql ??= @$"
2223
CREATE TABLE IF NOT EXISTS {_tablePrefix}rfunctions_timeouts (
2324
function_type_id VARCHAR(255),
2425
function_instance_id VARCHAR(255),
2526
timeout_id VARCHAR(255),
2627
expires BIGINT,
2728
PRIMARY KEY (function_type_id, function_instance_id, timeout_id)
2829
)";
29-
var command = new MySqlCommand(sql, conn);
30+
var command = new MySqlCommand(_initializeSql, conn);
3031
await command.ExecuteNonQueryAsync();
3132
}
3233

34+
private string? _truncateSql;
3335
public async Task Truncate()
3436
{
3537
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);
3840
await command.ExecuteNonQueryAsync();
3941
}
4042

43+
private string? _upsertTimeoutSql;
44+
private string? _insertTimeoutSql;
4145
public async Task UpsertTimeout(StoredTimeout storedTimeout, bool overwrite)
4246
{
4347
var (functionId, timeoutId, expiry) = storedTimeout;
4448
await using var conn = await CreateConnection();
45-
var sql = @$"
49+
_upsertTimeoutSql ??= @$"
4650
INSERT INTO {_tablePrefix}rfunctions_timeouts
4751
(function_type_id, function_instance_id, timeout_id, expires)
4852
VALUES
4953
(?, ?, ?, ?)
5054
ON DUPLICATE KEY UPDATE
5155
expires = ?";
52-
53-
if (!overwrite)
54-
sql = @$"
56+
_insertTimeoutSql ??= @$"
5557
INSERT IGNORE INTO {_tablePrefix}rfunctions_timeouts
5658
(function_type_id, function_instance_id, timeout_id, expires)
5759
VALUES
5860
(?, ?, ?, ?)";
59-
61+
62+
var sql = overwrite ? _upsertTimeoutSql : _insertTimeoutSql;
6063
await using var command = new MySqlCommand(sql, conn)
6164
{
6265
Parameters =
@@ -72,17 +75,18 @@ INSERT IGNORE INTO {_tablePrefix}rfunctions_timeouts
7275
await command.ExecuteNonQueryAsync();
7376
}
7477

78+
private string? _removeTimeoutSql;
7579
public async Task RemoveTimeout(FunctionId functionId, string timeoutId)
7680
{
7781
await using var conn = await CreateConnection();
78-
var sql = @$"
82+
_removeTimeoutSql ??= @$"
7983
DELETE FROM {_tablePrefix}rfunctions_timeouts
8084
WHERE
8185
function_type_id = ? AND
8286
function_instance_id = ? AND
8387
timeout_id = ?";
8488

85-
await using var command = new MySqlCommand(sql, conn)
89+
await using var command = new MySqlCommand(_removeTimeoutSql, conn)
8690
{
8791
Parameters =
8892
{
@@ -95,14 +99,15 @@ DELETE FROM {_tablePrefix}rfunctions_timeouts
9599
await command.ExecuteNonQueryAsync();
96100
}
97101

102+
private string? _removeSql;
98103
public async Task Remove(FunctionId functionId)
99104
{
100105
await using var conn = await CreateConnection();
101-
var sql = @$"
106+
_removeSql ??= @$"
102107
DELETE FROM {_tablePrefix}rfunctions_timeouts
103108
WHERE function_type_id = ? AND function_instance_id = ?";
104109

105-
await using var command = new MySqlCommand(sql, conn)
110+
await using var command = new MySqlCommand(_removeSql, conn)
106111
{
107112
Parameters =
108113
{
@@ -114,15 +119,16 @@ DELETE FROM {_tablePrefix}rfunctions_timeouts
114119
await command.ExecuteNonQueryAsync();
115120
}
116121

122+
private string? _getTimeoutsSqlExpiresBefore;
117123
public async Task<IEnumerable<StoredTimeout>> GetTimeouts(string functionTypeId, long expiresBefore)
118124
{
119125
await using var conn = await DatabaseHelper.CreateOpenConnection(_connectionString);;
120-
var sql = @$"
126+
_getTimeoutsSqlExpiresBefore ??= @$"
121127
SELECT function_instance_id, timeout_id, expires
122128
FROM {_tablePrefix}rfunctions_timeouts
123129
WHERE function_type_id = ? AND expires <= ?";
124130

125-
await using var command = new MySqlCommand(sql, conn)
131+
await using var command = new MySqlCommand(_getTimeoutsSqlExpiresBefore, conn)
126132
{
127133
Parameters =
128134
{
@@ -144,17 +150,18 @@ public async Task<IEnumerable<StoredTimeout>> GetTimeouts(string functionTypeId,
144150

145151
return storedTimeouts;
146152
}
147-
153+
154+
private string? _getFunctionTimeoutsSql;
148155
public async Task<IEnumerable<StoredTimeout>> GetTimeouts(FunctionId functionId)
149156
{
150157
var (typeId, instanceId) = functionId;
151158
await using var conn = await DatabaseHelper.CreateOpenConnection(_connectionString);;
152-
var sql = @$"
159+
_getFunctionTimeoutsSql ??= @$"
153160
SELECT timeout_id, expires
154161
FROM {_tablePrefix}rfunctions_timeouts
155162
WHERE function_type_id = ? AND function_instance_id = ?";
156163

157-
await using var command = new MySqlCommand(sql, conn)
164+
await using var command = new MySqlCommand(_getFunctionTimeoutsSql, conn)
158165
{
159166
Parameters =
160167
{
@@ -177,11 +184,12 @@ public async Task<IEnumerable<StoredTimeout>> GetTimeouts(FunctionId functionId)
177184

178185
private Task<MySqlConnection> CreateConnection() => DatabaseHelper.CreateOpenConnection(_connectionString);
179186

187+
private string? _dropUnderlyingTable;
180188
public async Task DropUnderlyingTable()
181189
{
182190
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);
185193
await command.ExecuteNonQueryAsync();
186194
}
187195
}

0 commit comments

Comments
 (0)