|
| 1 | +using NHibernate.Cfg; |
| 2 | +using NUnit.Framework; |
| 3 | +using System.Threading.Tasks; |
| 4 | + |
| 5 | +namespace NHibernate.Test.NHSpecificTest.GH1730 |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class Fixture : BugTestCase |
| 9 | + { |
| 10 | + protected override void Configure(Configuration configuration) |
| 11 | + { |
| 12 | + configuration.SetProperty("show_sql", "true"); |
| 13 | + configuration.SetProperty("generate_statistics", "true"); |
| 14 | + } |
| 15 | + |
| 16 | + protected override void OnTearDown() |
| 17 | + { |
| 18 | + using (var session = OpenSession()) |
| 19 | + using (var transaction = session.BeginTransaction()) |
| 20 | + { |
| 21 | + session.CreateQuery("delete from Entity").ExecuteUpdate(); |
| 22 | + |
| 23 | + transaction.Commit(); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public void HitCacheInSameSession() |
| 29 | + { |
| 30 | + Sfi.EvictQueries(); |
| 31 | + Sfi.Statistics.Clear(); |
| 32 | + var entities = new System.Collections.Generic.List<Entity>(); |
| 33 | + |
| 34 | + using (var session = OpenSession()) |
| 35 | + { |
| 36 | + using (var transaction = session.BeginTransaction()) |
| 37 | + { |
| 38 | + for (int i = 0; i < 3; i++) |
| 39 | + { |
| 40 | + var e = new Entity { Name = "Name" + i }; |
| 41 | + entities.Add(e); |
| 42 | + session.Save(e); |
| 43 | + } |
| 44 | + transaction.Commit(); |
| 45 | + } |
| 46 | + |
| 47 | + var queryString = "from Entity"; |
| 48 | + |
| 49 | + using (var tx = session.BeginTransaction()) |
| 50 | + { |
| 51 | + // this query will hit the database and create the cache |
| 52 | + session.CreateQuery(queryString).SetCacheable(true).List(); |
| 53 | + tx.Commit(); |
| 54 | + } |
| 55 | + |
| 56 | + using (var transaction = session.BeginTransaction()) |
| 57 | + { |
| 58 | + //and this one SHOULD served by the cache |
| 59 | + session.CreateQuery(queryString).SetCacheable(true).List(); |
| 60 | + transaction.Commit(); |
| 61 | + } |
| 62 | + |
| 63 | + var qs = Sfi.Statistics.GetQueryStatistics(queryString); |
| 64 | + Assert.AreEqual(1, qs.CacheHitCount); |
| 65 | + Assert.AreEqual(1, qs.CachePutCount); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + [Test] |
| 70 | + public async Task HitCacheInSameSessionAsync() |
| 71 | + { |
| 72 | + Sfi.EvictQueries(); |
| 73 | + Sfi.Statistics.Clear(); |
| 74 | + var entities = new System.Collections.Generic.List<Entity>(); |
| 75 | + |
| 76 | + using (var session = OpenSession()) |
| 77 | + { |
| 78 | + using (var transaction = session.BeginTransaction()) |
| 79 | + { |
| 80 | + for (int i = 0; i < 3; i++) |
| 81 | + { |
| 82 | + var e = new Entity { Name = "Name" + i }; |
| 83 | + entities.Add(e); |
| 84 | + await session.SaveAsync(e); |
| 85 | + } |
| 86 | + await transaction.CommitAsync(); |
| 87 | + } |
| 88 | + |
| 89 | + var queryString = "from Entity"; |
| 90 | + |
| 91 | + using (var tx = session.BeginTransaction()) |
| 92 | + { |
| 93 | + // this query will hit the database and create the cache |
| 94 | + await session.CreateQuery(queryString).SetCacheable(true).ListAsync(); |
| 95 | + await tx.CommitAsync(); |
| 96 | + } |
| 97 | + |
| 98 | + using (var transaction = session.BeginTransaction()) |
| 99 | + { |
| 100 | + //and this one SHOULD served by the cache |
| 101 | + await session.CreateQuery(queryString).SetCacheable(true).ListAsync(); |
| 102 | + await transaction.CommitAsync(); |
| 103 | + } |
| 104 | + |
| 105 | + var qs = Sfi.Statistics.GetQueryStatistics(queryString); |
| 106 | + Assert.AreEqual(1, qs.CacheHitCount); |
| 107 | + Assert.AreEqual(1, qs.CachePutCount); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments