Skip to content

Commit

Permalink
feat(LBE-426): Set TTL to entire hashes collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathas Costa authored and jonathascosta committed Jul 24, 2023
1 parent eaefa73 commit ca11646
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
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);
}
}

0 comments on commit ca11646

Please sign in to comment.