|
23 | 23 | using System.Collections.Generic;
|
24 | 24 | using System.Data;
|
25 | 25 | using System.Globalization;
|
| 26 | +using System.Linq; |
26 | 27 | using Dapper;
|
27 | 28 | using Hangfire.Common;
|
28 | 29 | using Hangfire.States;
|
@@ -336,13 +337,107 @@ public override void RemoveHash(string key)
|
336 | 337 | if (key == null) throw new ArgumentNullException(nameof(key));
|
337 | 338 |
|
338 | 339 | QueueCommand((con, trx) => con.Execute(
|
339 |
| - @" |
340 |
| - DELETE FROM """ + _options.SchemaName + @""".""hash"" |
341 |
| - WHERE ""key"" = @key; |
342 |
| - ", |
| 340 | + $@"DELETE FROM ""{_options.SchemaName}"".""hash"" WHERE ""key"" = @key", |
343 | 341 | new {key}, trx));
|
344 | 342 | }
|
345 | 343 |
|
| 344 | + public override void ExpireSet(string key, TimeSpan expireIn) |
| 345 | + { |
| 346 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
| 347 | + |
| 348 | + string query = $@"UPDATE ""{_options.SchemaName}"".""set"" SET ""expireat"" = @expireAt WHERE ""key"" = @key"; |
| 349 | + |
| 350 | + QueueCommand((connection, transaction) => connection.Execute( |
| 351 | + query, |
| 352 | + new { key, expireAt = DateTime.UtcNow.Add(expireIn) }, |
| 353 | + transaction)); |
| 354 | + } |
| 355 | + |
| 356 | + public override void ExpireList(string key, TimeSpan expireIn) |
| 357 | + { |
| 358 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
| 359 | + |
| 360 | + string query = $@"UPDATE ""{_options.SchemaName}"".""list"" SET ""expireat"" = @expireAt WHERE ""key"" = @key"; |
| 361 | + |
| 362 | + QueueCommand((connection, transaction) => connection.Execute( |
| 363 | + query, |
| 364 | + new { key, expireAt = DateTime.UtcNow.Add(expireIn) }, |
| 365 | + transaction)); |
| 366 | + } |
| 367 | + |
| 368 | + public override void ExpireHash(string key, TimeSpan expireIn) |
| 369 | + { |
| 370 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
| 371 | + |
| 372 | + string query = $@"UPDATE ""{_options.SchemaName}"".""hash"" SET expireat = @expireAt WHERE ""key"" = @key"; |
| 373 | + |
| 374 | + QueueCommand((connection, transaction) => connection.Execute( |
| 375 | + query, |
| 376 | + new { key, expireAt = DateTime.UtcNow.Add(expireIn) }, |
| 377 | + transaction)); |
| 378 | + } |
| 379 | + |
| 380 | + public override void PersistSet(string key) |
| 381 | + { |
| 382 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
| 383 | + |
| 384 | + string query = $@"UPDATE ""{_options.SchemaName}"".""set"" SET expireat = null WHERE ""key"" = @key"; |
| 385 | + |
| 386 | + QueueCommand((connection, transaction) => connection.Execute( |
| 387 | + query, |
| 388 | + new { key }, |
| 389 | + transaction)); |
| 390 | + } |
| 391 | + |
| 392 | + public override void PersistList(string key) |
| 393 | + { |
| 394 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
| 395 | + |
| 396 | + string query = $@"UPDATE ""{_options.SchemaName}"".""list"" SET expireat = null WHERE ""key"" = @key"; |
| 397 | + |
| 398 | + QueueCommand((connection, transaction) => connection.Execute( |
| 399 | + query, |
| 400 | + new { key }, |
| 401 | + transaction)); |
| 402 | + } |
| 403 | + |
| 404 | + public override void PersistHash(string key) |
| 405 | + { |
| 406 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
| 407 | + |
| 408 | + string query = $@"UPDATE ""{_options.SchemaName}"".""hash"" SET expireat = null WHERE ""key"" = @key"; |
| 409 | + |
| 410 | + QueueCommand((connection, transaction) => connection.Execute( |
| 411 | + query, |
| 412 | + new { key }, |
| 413 | + transaction)); |
| 414 | + } |
| 415 | + |
| 416 | + public override void AddRangeToSet(string key, IList<string> items) |
| 417 | + { |
| 418 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
| 419 | + if (items == null) throw new ArgumentNullException(nameof(items)); |
| 420 | + |
| 421 | + string query = $@"INSERT INTO ""{_options.SchemaName}"".""set"" (""key"", ""value"", ""score"") VALUES (@key, @value, 0.0)"; |
| 422 | + |
| 423 | + QueueCommand((connection, transaction) => connection.Execute( |
| 424 | + query, |
| 425 | + items.Select(value => new { key, value }).ToList(), |
| 426 | + transaction)); |
| 427 | + } |
| 428 | + |
| 429 | + public override void RemoveSet(string key) |
| 430 | + { |
| 431 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
| 432 | + |
| 433 | + string query = $@"DELETE FROM ""{_options.SchemaName}"".""set"" WHERE ""key"" = @key"; |
| 434 | + |
| 435 | + QueueCommand((connection, transaction) => connection.Execute( |
| 436 | + query, |
| 437 | + new { key }, |
| 438 | + transaction)); |
| 439 | + } |
| 440 | + |
346 | 441 | internal void QueueCommand(Action<NpgsqlConnection, NpgsqlTransaction> action)
|
347 | 442 | {
|
348 | 443 | _commandQueue.Enqueue(action);
|
|
0 commit comments