Skip to content

Commit ca87dc5

Browse files
Add RemoveAllExpiredGlobal (#17)
1 parent e7c6526 commit ca87dc5

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/PgKeyValueDB/PgKeyValueDB.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static IEnumerable<NpgsqlParameter> CreateParams<T>(string pid, string? id, T? v
7070
string DeleteSql => $"delete from {tableRef} where pid = @pid and id = @id";
7171
string DeleteAllSql => $"delete from {tableRef} where pid = @pid";
7272
string DeleteAllExpiredSql => $"delete from {tableRef} where pid = @pid and now() >= expires";
73+
string DeleteAllExpiredGlobalSql => $"delete from {tableRef} where now() >= expires";
7374
string ExistsSql => $"select exists(select 1 from {tableRef} where pid = @pid and id = @id and (expires is null or now() < expires))";
7475
string CountSql => $"select count(1) from {tableRef} where pid = @pid and (expires is null or now() < expires)";
7576

@@ -101,6 +102,10 @@ public int RemoveAllExpired(string pid = DEFAULT_PID) =>
101102
dataSource.Execute(new Ctx(DeleteAllExpiredSql, CreateParams(pid)));
102103
public async Task<int> RemoveAllExpiredAsync(string pid = DEFAULT_PID) =>
103104
await dataSource.ExecuteAsync(new Ctx(DeleteAllExpiredSql, CreateParams(pid)));
105+
public int RemoveAllExpiredGlobal() =>
106+
dataSource.Execute(new Ctx(DeleteAllExpiredGlobalSql));
107+
public async Task<int> RemoveAllExpiredGlobalAsync() =>
108+
await dataSource.ExecuteAsync(new Ctx(DeleteAllExpiredGlobalSql));
104109
public T? Get<T>(string id, string pid = DEFAULT_PID) =>
105110
dataSource.Execute<T>(new Ctx(SelectSql, CreateParams(pid, id)));
106111
public async Task<T?> GetAsync<T>(string id, string pid = DEFAULT_PID) =>

test/PgKeyValueDB.Tests/PgKeyValueDBTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ public void RemoveAllExpiredTest()
129129
Assert.AreEqual(1, result);
130130
}
131131

132+
[TestMethod]
133+
public void RemoveAllExpiredGlobalTest()
134+
{
135+
var key = nameof(RemoveAllExpiredGlobalTest);
136+
var pid = nameof(RemoveAllExpiredGlobalTest);
137+
kv.Upsert(key, new Poco { Value = key }, pid);
138+
var result = kv.RemoveAllExpiredGlobal();
139+
Assert.AreEqual(0, result);
140+
kv.Upsert(key, new Poco { Value = key }, pid, DateTimeOffset.UtcNow.AddMinutes(-1));
141+
result = kv.RemoveAllExpiredGlobal();
142+
Assert.AreEqual(1, result);
143+
}
144+
132145
[TestMethod]
133146
public void DuplicateKeyTest()
134147
{

0 commit comments

Comments
 (0)