Skip to content

Commit b002beb

Browse files
committed
Minimize sql-script string generations in PostgreSqlTimeoutStore
1 parent fedb049 commit b002beb

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL/PostgreSqlTimeoutStore.cs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,30 @@ public PostgreSqlTimeoutStore(string connectionString, string tablePrefix = "")
1515
{
1616
_connectionString = connectionString;
1717
_tablePrefix = tablePrefix.ToLower();
18-
}
19-
18+
}
19+
20+
private string? _initializeSql;
2021
public async Task Initialize()
2122
{
2223
await using var conn = await CreateConnection();
23-
var sql = @$"
24+
_initializeSql ??= @$"
2425
CREATE TABLE IF NOT EXISTS {_tablePrefix}_timeouts (
2526
function_type_id VARCHAR(255),
2627
function_instance_id VARCHAR(255),
2728
timeout_id VARCHAR(255),
2829
expires BIGINT,
2930
PRIMARY KEY (function_type_id, function_instance_id, timeout_id)
3031
)";
31-
var command = new NpgsqlCommand(sql, conn);
32+
var command = new NpgsqlCommand(_initializeSql, conn);
3233
await command.ExecuteNonQueryAsync();
3334
}
34-
35+
36+
private string? _truncateSql;
3537
public async Task Truncate()
3638
{
3739
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);
4042
await command.ExecuteNonQueryAsync();
4143
}
4244

@@ -46,27 +48,29 @@ private async Task<NpgsqlConnection> CreateConnection()
4648
await conn.OpenAsync();
4749
return conn;
4850
}
49-
51+
52+
private string? _upsertTimeoutSql;
53+
private string? _insertTimeoutSql;
5054
public async Task UpsertTimeout(StoredTimeout storedTimeout, bool overwrite)
5155
{
5256
var (functionId, timeoutId, expiry) = storedTimeout;
5357
await using var conn = await CreateConnection();
54-
var sql = @$"
58+
_upsertTimeoutSql ??= @$"
5559
INSERT INTO {_tablePrefix}_timeouts
5660
(function_type_id, function_instance_id, timeout_id, expires)
5761
VALUES
5862
($1, $2, $3, $4)
5963
ON CONFLICT (function_type_id, function_instance_id, timeout_id)
6064
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;
7074
await using var command = new NpgsqlCommand(sql, conn)
7175
{
7276
Parameters =
@@ -81,17 +85,18 @@ INSERT INTO {_tablePrefix}_timeouts
8185
await command.ExecuteNonQueryAsync();
8286
}
8387

88+
private string? _removeTimeoutSql;
8489
public async Task RemoveTimeout(FunctionId functionId, string timeoutId)
8590
{
8691
await using var conn = await CreateConnection();
87-
var sql = @$"
92+
_removeTimeoutSql ??= @$"
8893
DELETE FROM {_tablePrefix}_timeouts
8994
WHERE
9095
function_type_id = $1 AND
9196
function_instance_id = $2 AND
9297
timeout_id = $3";
9398

94-
await using var command = new NpgsqlCommand(sql, conn)
99+
await using var command = new NpgsqlCommand(_removeTimeoutSql, conn)
95100
{
96101
Parameters =
97102
{
@@ -104,14 +109,15 @@ DELETE FROM {_tablePrefix}_timeouts
104109
await command.ExecuteNonQueryAsync();
105110
}
106111

112+
private string? _removeSql;
107113
public async Task Remove(FunctionId functionId)
108114
{
109115
await using var conn = await CreateConnection();
110-
var sql = @$"
116+
_removeSql ??= @$"
111117
DELETE FROM {_tablePrefix}_timeouts
112118
WHERE function_type_id = $1 AND function_instance_id = $2";
113119

114-
await using var command = new NpgsqlCommand(sql, conn)
120+
await using var command = new NpgsqlCommand(_removeSql, conn)
115121
{
116122
Parameters =
117123
{
@@ -123,17 +129,18 @@ DELETE FROM {_tablePrefix}_timeouts
123129
await command.ExecuteNonQueryAsync();
124130
}
125131

132+
private string? _getTimeoutsSqlExpiresBefore;
126133
public async Task<IEnumerable<StoredTimeout>> GetTimeouts(string functionTypeId, long expiresBefore)
127134
{
128135
await using var conn = await CreateConnection();
129-
var sql = @$"
136+
_getTimeoutsSqlExpiresBefore ??= @$"
130137
SELECT function_instance_id, timeout_id, expires
131138
FROM {_tablePrefix}_timeouts
132139
WHERE
133140
function_type_id = $1 AND
134141
expires <= $2";
135142

136-
await using var command = new NpgsqlCommand(sql, conn)
143+
await using var command = new NpgsqlCommand(_getTimeoutsSqlExpiresBefore, conn)
137144
{
138145
Parameters =
139146
{
@@ -156,16 +163,17 @@ public async Task<IEnumerable<StoredTimeout>> GetTimeouts(string functionTypeId,
156163
return storedMessages;
157164
}
158165

166+
private string? _getTimeoutsSql;
159167
public async Task<IEnumerable<StoredTimeout>> GetTimeouts(FunctionId functionId)
160168
{
161169
var (typeId, instanceId) = functionId;
162170
await using var conn = await CreateConnection();
163-
var sql = @$"
171+
_getTimeoutsSql ??= @$"
164172
SELECT timeout_id, expires
165173
FROM {_tablePrefix}_timeouts
166174
WHERE function_type_id = $1 AND function_instance_id = $2";
167175

168-
await using var command = new NpgsqlCommand(sql, conn)
176+
await using var command = new NpgsqlCommand(_getTimeoutsSql, conn)
169177
{
170178
Parameters =
171179
{
@@ -186,11 +194,12 @@ public async Task<IEnumerable<StoredTimeout>> GetTimeouts(FunctionId functionId)
186194
return storedMessages;
187195
}
188196

197+
private string? _dropUnderlyingTableSql;
189198
public async Task DropUnderlyingTable()
190199
{
191200
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);
194203
await command.ExecuteNonQueryAsync();
195204
}
196205
}

0 commit comments

Comments
 (0)