Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/PgKeyValueDB/PgKeyValueDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static IEnumerable<NpgsqlParameter> CreateParams<T>(string pid, string? id, T? v
string DeleteSql => $"delete from {tableRef} where pid = @pid and id = @id";
string DeleteAllSql => $"delete from {tableRef} where pid = @pid";
string DeleteAllExpiredSql => $"delete from {tableRef} where pid = @pid and now() >= expires";
string DeleteAllExpiredGlobalSql => $"delete from {tableRef} where now() >= expires";
string ExistsSql => $"select exists(select 1 from {tableRef} where pid = @pid and id = @id and (expires is null or now() < expires))";
string CountSql => $"select count(1) from {tableRef} where pid = @pid and (expires is null or now() < expires)";

Expand Down Expand Up @@ -101,6 +102,10 @@ public int RemoveAllExpired(string pid = DEFAULT_PID) =>
dataSource.Execute(new Ctx(DeleteAllExpiredSql, CreateParams(pid)));
public async Task<int> RemoveAllExpiredAsync(string pid = DEFAULT_PID) =>
await dataSource.ExecuteAsync(new Ctx(DeleteAllExpiredSql, CreateParams(pid)));
public int RemoveAllExpiredGlobal() =>
dataSource.Execute(new Ctx(DeleteAllExpiredGlobalSql));
public async Task<int> RemoveAllExpiredGlobalAsync() =>
await dataSource.ExecuteAsync(new Ctx(DeleteAllExpiredGlobalSql));
public T? Get<T>(string id, string pid = DEFAULT_PID) =>
dataSource.Execute<T>(new Ctx(SelectSql, CreateParams(pid, id)));
public async Task<T?> GetAsync<T>(string id, string pid = DEFAULT_PID) =>
Expand Down
13 changes: 13 additions & 0 deletions test/PgKeyValueDB.Tests/PgKeyValueDBTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ public void RemoveAllExpiredTest()
Assert.AreEqual(1, result);
}

[TestMethod]
public void RemoveAllExpiredGlobalTest()
{
var key = nameof(RemoveAllExpiredGlobalTest);
var pid = nameof(RemoveAllExpiredGlobalTest);
kv.Upsert(key, new Poco { Value = key }, pid);
var result = kv.RemoveAllExpiredGlobal();
Assert.AreEqual(0, result);
kv.Upsert(key, new Poco { Value = key }, pid, DateTimeOffset.UtcNow.AddMinutes(-1));
result = kv.RemoveAllExpiredGlobal();
Assert.AreEqual(1, result);
}

[TestMethod]
public void DuplicateKeyTest()
{
Expand Down