Skip to content

Commit

Permalink
Merge pull request #16 from seesharper/bugfix/concurrent-querycache
Browse files Browse the repository at this point in the history
Start using ConcurrentDictionary  in the query cache
  • Loading branch information
seesharper committed Mar 7, 2018
2 parents 9700456 + 1267752 commit 74613a1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/DbReader.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public void ShouldReadCustomers()
}
}

public async Task ShouldHandleRunningInParallel()
{
var task1 = ShouldReadCustomersAndOrdersAsync();
var task2 = ShouldReadCustomersAndOrdersAsync();
var task3 = ShouldReadCustomersAndOrdersAsync();
var task4 = ShouldReadCustomersAndOrdersAsync();

await Task.WhenAll(task1, task2, task3, task4);
}



public async Task ShouldReadCustomersAsync()
{
using (var connection = CreateConnection())
Expand All @@ -94,6 +106,17 @@ public void ShouldReadCustomersAndOrders()
}
}

public async Task ShouldReadCustomersAndOrdersAsync()
{
using (var connection = CreateConnection())
{
await Task.Delay(10);
var customers = await connection.ReadAsync<CustomerWithOrders>(SQL.CustomersAndOrders);
customers.Count().ShouldBe(89);
customers.SelectMany(c => c.Orders).Count().ShouldBe(830);
}
}


public void ShouldReadCustomerById()
{
Expand Down
2 changes: 1 addition & 1 deletion src/DbReader/DbReader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<Authors>Bernhard Richter</Authors>
<PackageProjectUrl>https://github.com/seesharper/DbReader</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
Expand Down
4 changes: 2 additions & 2 deletions src/DbReader/Readers/CachedInstanceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CachedInstanceReader<T> : IInstanceReader<T>

private readonly IOneToManyMethodBuilder<T> oneToManyMethodBuilder;

private readonly Dictionary<IStructuralEquatable, T> queryCache = new Dictionary<IStructuralEquatable, T>();
private readonly ConcurrentDictionary<IStructuralEquatable, T> queryCache = new ConcurrentDictionary<IStructuralEquatable, T>();

/// <summary>
/// Initializes a new instance of the <see cref="CachedInstanceReader{T}"/> class.
Expand Down Expand Up @@ -60,7 +60,7 @@ private T ReadInstance(IDataRecord dataRecord, string currentPrefix)
if (!queryCache.TryGetValue(key, out instance))
{
instance = instanceReader.Read(dataRecord, currentPrefix);
queryCache.Add(key, instance);
queryCache.TryAdd(key, instance);
}
return instance;

Expand Down

0 comments on commit 74613a1

Please sign in to comment.