Skip to content

Commit ac8b6ae

Browse files
committed
Fixed #36 - Expression.Body is const when variables are passed
1 parent a52799f commit ac8b6ae

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

LinkDotNet.Blog.Infrastructure/Persistence/CachedRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task<IPagedList<T>> GetAllAsync(
4848
int page = 1,
4949
int pageSize = int.MaxValue)
5050
{
51-
var key = $"{filter?.Body}-{orderBy?.Body}-{descending}-{page}-{pageSize}";
51+
var key = $"{filter?.GetHashCode()}-{orderBy?.GetHashCode()}-{descending}-{page}-{pageSize}";
5252
return await memoryCache.GetOrCreate(key, async e =>
5353
{
5454
e.SetOptions(Options);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using FluentAssertions;
4+
using LinkDotNet.Blog.Domain;
5+
using LinkDotNet.Blog.Infrastructure.Persistence;
6+
using LinkDotNet.Blog.TestUtilities;
7+
using Microsoft.Extensions.Caching.Memory;
8+
using Xunit;
9+
10+
namespace LinkDotNet.Blog.IntegrationTests.Infrastructure.Persistence
11+
{
12+
public class CachedRepositoryTests : SqlDatabaseTestBase<BlogPost>
13+
{
14+
[Fact]
15+
public async Task ShouldNotCacheWhenDifferentQueries()
16+
{
17+
var bp1 = new BlogPostBuilder().WithTags("tag 1").Build();
18+
var bp2 = new BlogPostBuilder().WithTags("tag 2").Build();
19+
var bp3 = new BlogPostBuilder().WithTags("tag 1").Build();
20+
await Repository.StoreAsync(bp1);
21+
await Repository.StoreAsync(bp2);
22+
await Repository.StoreAsync(bp3);
23+
var searchTerm = "tag 1";
24+
var sut = new CachedRepository<BlogPost>(Repository, new MemoryCache(new MemoryCacheOptions()));
25+
await sut.GetAllAsync(f => f.Tags.Any(t => t.Content == searchTerm));
26+
searchTerm = "tag 2";
27+
28+
var allWithTag2 = await sut.GetAllAsync(f => f.Tags.Any(t => t.Content == searchTerm));
29+
30+
allWithTag2.Count.Should().Be(1);
31+
allWithTag2.Single().Tags.Single().Content.Should().Be("tag 2");
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)