Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(LBE-426): Set TTL to entire hashes collection #9

Merged
merged 1 commit into from
Jul 24, 2023
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
12 changes: 12 additions & 0 deletions src/CascadeRepos/RedisHashRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ protected override async Task CoreSet(TK key, T item, CancellationToken cancella
{
await _database.HashSetAsync(_redisKeyAdapter()!.ToString(), KeyToKeyAdapter(key)!.ToString(),
JsonConvert.SerializeObject(item));

await SetExpiration();
}

/// <inheritdoc />
Expand All @@ -104,6 +106,8 @@ protected override async Task CoreSetAll(IList<T> items, CancellationToken cance
foreach (var item in items)
await _database.HashSetAsync(GetSetAllKey, ObjectToKeyAdapter(item)!.ToString(),
JsonConvert.SerializeObject(item));

await SetExpiration();
}

/// <inheritdoc />
Expand All @@ -121,4 +125,12 @@ protected override async Task CoreDelete(TK key, CancellationToken cancellationT
{
await _database.HashDeleteAsync(_redisKeyAdapter()!.ToString(), KeyToKeyAdapter(key)!.ToString());
}

private async Task SetExpiration()
{
var expirationTime = CalculateExpirationTime();

if (expirationTime != null)
await _database.KeyExpireAsync(_redisKeyAdapter()!.ToString(), expirationTime.Value.DateTime);
}
}
26 changes: 24 additions & 2 deletions tests/CascadeRepos.UnitTests/RedisHashRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public async Task GetAll_Adds_Items_Using_CoreSetAll()
_databaseMock.Verify(x => x.HashGetAllAsync(It.IsAny<RedisKey>(), CommandFlags.None), Times.Once);
}


[Fact]
public async Task GetList_Returns_ListOfItems()
{
Expand Down Expand Up @@ -255,7 +254,6 @@ public async Task GetList_Adds_Items_Using_CoreSetList()
_databaseMock.Verify(x => x.HashGetAllAsync(It.IsAny<RedisKey>(), CommandFlags.None), Times.Once);
}


[Fact]
public async Task Throws_When_ObjectToKey_Is_Not_Defined()
{
Expand Down Expand Up @@ -285,4 +283,28 @@ public async Task Throws_When_ObjectToKey_Is_Not_Defined()
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await _repository.GetList(listId, true, CancellationToken.None));
}

[Fact]
public async Task CoreSet_Sets_Hash_And_Expires_Key()
{
// Arrange
var key = "cacheKey";
var value = new SomeObject
{
Id = key,
Name = "Some Name"
};

// Act
_repository.SetTimeToLive(TimeSpan.FromSeconds(2));
await _repository.Set(key, value);

// Assert
_databaseMock.Verify(
d => d.HashSetAsync(It.IsAny<RedisKey>(), It.IsAny<RedisValue>(),It.IsAny<RedisValue>(), It.IsAny<When>(),
It.IsAny<CommandFlags>()), Times.Once);
_databaseMock.Verify(
d => d.KeyExpireAsync(It.IsAny<RedisKey>(), It.IsAny<DateTime?>(), It.IsAny<CommandFlags>()),
Times.Once);
}
}