Skip to content

Commit

Permalink
fixing ForceRefresh flow
Browse files Browse the repository at this point in the history
updating packages
  • Loading branch information
fernandoescolar committed Jan 29, 2025
1 parent f55a2f6 commit 28addb6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/CacheService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.1" />
</ItemGroup>
<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="" />
Expand Down
18 changes: 11 additions & 7 deletions src/Core/UglyCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ public UglyCacheService(IOptions<CacheServiceConfiguration> configuration, Deleg

var result = default(T);
AddOrUpdateJob(key, ops, getter);
if (TryGetFromMemory(key, ref result))
{
return result;
}

result = await TryGetFromDistributedAsync<T>(key, ops, cancellationToken);
if (result is not null)
if (!(options?.ForceRefresh ?? false))
{
return result;
if (TryGetFromMemory(key, ref result))
{
return result;
}

result = await TryGetFromDistributedAsync<T>(key, ops, cancellationToken);
if (result is not null)
{
return result;
}
}

return await TryGetFromSourceAsync<T>(key, getter, ops, cancellationToken);
Expand Down
32 changes: 31 additions & 1 deletion tests/Integration/CacheService_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public async Task Write_DistributedCache_When_Value_Is_Too_Big()
var distributedValue = DistributedCache[key];

Assert.NotNull(distributedValue);
}
}

[Fact]
public async Task Delete_MemoryCache_Value_When_It_Is_Invalidated()
Expand All @@ -117,5 +117,35 @@ public async Task Delete_DistributedCache_Value_When_It_Is_Invalidated()
Assert.False(DistributedCache.ContainsKey(key));
}

[Fact]
public async Task Replace_Memory_Value_When_Force_Refresh_Option_Is_Set()
{
var unexpected = new DummyObject();
MemoryCache.Add(key, new DummyCacheEntry(key) { Value = unexpected });

var actual = await Target.GetOrSetAsync(key, new CacheServiceOptions { ForceRefresh = true }, () => expected, CancellationToken);
Assert.Equal(expected, actual);

var memoryValue = MemoryCache[key].Value;
Assert.Equal(expected, memoryValue);
}

[Fact]
public async Task Replace_Distributed_Value_When_Force_Refresh_Option_Is_Set()
{
var unexpected = new DummyObject();
var unexpectedSerialized = System.Text.Encoding.UTF8.GetBytes($@"{{""Id"":""{unexpected.Id}""}}");
DistributedCache.Add(key, unexpectedSerialized);

var actual = await Target.GetOrSetAsync(key, new CacheServiceOptions { ForceRefresh = true }, () => expected, CancellationToken);
Assert.Equal(expected, actual);

// Set operation is async, so we need to wait a bit
await Task.Delay(1000);

var distributedValue = DistributedCache[key];
Assert.Equal(serialized, distributedValue);
}

private sealed record TestData(string Id, string Field1, string Field2);
}

0 comments on commit 28addb6

Please sign in to comment.