Skip to content

Commit c31527e

Browse files
Fix session factory deserialization with Prevalence Cache
1 parent a8f4ea3 commit c31527e

File tree

6 files changed

+402
-16
lines changed

6 files changed

+402
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections;
12+
using NHibernate.Cache;
13+
14+
namespace NHibernate.Test.CacheTest.Caches
15+
{
16+
using System.Threading.Tasks;
17+
using System.Threading;
18+
public partial class SerializingCache : CacheBase
19+
{
20+
21+
public override Task<object> GetAsync(object key, CancellationToken cancellationToken)
22+
{
23+
try
24+
{
25+
return Task.FromResult<object>(_hashtable[key]);
26+
}
27+
catch (System.Exception ex)
28+
{
29+
return Task.FromException<object>(ex);
30+
}
31+
}
32+
33+
public override Task PutAsync(object key, object value, CancellationToken cancellationToken)
34+
{
35+
try
36+
{
37+
_hashtable[key] = value;
38+
return Task.CompletedTask;
39+
}
40+
catch (System.Exception ex)
41+
{
42+
return Task.FromException<object>(ex);
43+
}
44+
}
45+
46+
public override Task RemoveAsync(object key, CancellationToken cancellationToken)
47+
{
48+
try
49+
{
50+
_hashtable.Remove(key);
51+
return Task.CompletedTask;
52+
}
53+
catch (System.Exception ex)
54+
{
55+
return Task.FromException<object>(ex);
56+
}
57+
}
58+
59+
public override Task ClearAsync(CancellationToken cancellationToken)
60+
{
61+
try
62+
{
63+
_hashtable.Clear();
64+
return Task.CompletedTask;
65+
}
66+
catch (System.Exception ex)
67+
{
68+
return Task.FromException<object>(ex);
69+
}
70+
}
71+
72+
public override Task<object> LockAsync(object key, CancellationToken cancellationToken)
73+
{
74+
// local cache, no need to actually lock.
75+
return Task.FromResult<object>(null);
76+
}
77+
78+
public override Task UnlockAsync(object key, object lockValue, CancellationToken cancellationToken)
79+
{
80+
return Task.CompletedTask;
81+
// local cache, no need to actually lock.
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Cfg;
13+
using NHibernate.Linq;
14+
using NHibernate.Test.CacheTest.Caches;
15+
using NUnit.Framework;
16+
using Environment = NHibernate.Cfg.Environment;
17+
18+
namespace NHibernate.Test.CacheTest
19+
{
20+
using System.Threading.Tasks;
21+
[TestFixture]
22+
public class SerializingCacheFixtureAsync : TestCase
23+
{
24+
protected override string[] Mappings => new[]
25+
{
26+
"CacheTest.ReadOnly.hbm.xml"
27+
};
28+
29+
protected override string MappingsAssembly => "NHibernate.Test";
30+
31+
protected override string CacheConcurrencyStrategy => null;
32+
33+
protected override void Configure(Configuration configuration)
34+
{
35+
configuration.SetProperty(Environment.UseSecondLevelCache, "true");
36+
configuration.SetProperty(Environment.UseQueryCache, "true");
37+
configuration.SetProperty(Environment.CacheProvider, typeof(SerializingCacheProvider).AssemblyQualifiedName);
38+
configuration.SetProperty(Environment.SessionFactoryName, "SerializingCacheFactory");
39+
}
40+
41+
protected override void OnSetUp()
42+
{
43+
using (var s = Sfi.OpenSession())
44+
using (var tx = s.BeginTransaction())
45+
{
46+
var totalItems = 6;
47+
for (var i = 1; i <= totalItems; i++)
48+
{
49+
var parent = new ReadOnly
50+
{
51+
Name = $"Name{i}"
52+
};
53+
for (var j = 1; j <= totalItems; j++)
54+
{
55+
var child = new ReadOnlyItem
56+
{
57+
Parent = parent
58+
};
59+
parent.Items.Add(child);
60+
}
61+
s.Save(parent);
62+
}
63+
tx.Commit();
64+
}
65+
}
66+
67+
protected override void OnTearDown()
68+
{
69+
using (var s = OpenSession())
70+
using (var tx = s.BeginTransaction())
71+
{
72+
s.CreateQuery("delete from ReadOnlyItem").ExecuteUpdate();
73+
s.CreateQuery("delete from ReadOnly").ExecuteUpdate();
74+
tx.Commit();
75+
}
76+
}
77+
78+
[Test]
79+
public async Task CachedQueryTestAsync()
80+
{
81+
// Put things in cache
82+
using (var s = Sfi.OpenSession())
83+
using (var tx = s.BeginTransaction())
84+
{
85+
var items = await (s.Query<ReadOnly>().WithOptions(o => o.SetCacheable(true)).ToListAsync());
86+
Assert.That(items, Has.Count.GreaterThan(0));
87+
await (tx.CommitAsync());
88+
}
89+
90+
RebuildSessionFactory();
91+
}
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Collections;
2+
using NHibernate.Cache;
3+
4+
namespace NHibernate.Test.CacheTest.Caches
5+
{
6+
public partial class SerializingCache : CacheBase
7+
{
8+
private readonly IDictionary _hashtable;
9+
10+
public SerializingCache(string regionName, IDictionary data)
11+
{
12+
RegionName = regionName;
13+
_hashtable = data;
14+
}
15+
16+
public override object Get(object key)
17+
{
18+
return _hashtable[key];
19+
}
20+
21+
public override void Put(object key, object value)
22+
{
23+
_hashtable[key] = value;
24+
}
25+
26+
public override void Remove(object key)
27+
{
28+
_hashtable.Remove(key);
29+
}
30+
31+
public override void Clear()
32+
{
33+
_hashtable.Clear();
34+
}
35+
36+
public override void Destroy()
37+
{
38+
}
39+
40+
public override object Lock(object key)
41+
{
42+
// local cache, no need to actually lock.
43+
return null;
44+
}
45+
46+
public override void Unlock(object key, object lockValue)
47+
{
48+
// local cache, no need to actually lock.
49+
}
50+
51+
public override long NextTimestamp()
52+
{
53+
return Timestamper.Next();
54+
}
55+
56+
public override int Timeout => Timestamper.OneMs * 60000;
57+
58+
public override string RegionName { get; }
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Runtime.Serialization.Formatters.Binary;
6+
using NHibernate.Cache;
7+
8+
namespace NHibernate.Test.CacheTest.Caches
9+
{
10+
public class SerializingCacheProvider : ICacheProvider
11+
{
12+
#region ICacheProvider Members
13+
14+
// Since 5.2
15+
[Obsolete]
16+
ICache ICacheProvider.BuildCache(string regionName, IDictionary<string, string> properties)
17+
{
18+
return BuildCache(regionName, properties);
19+
}
20+
21+
public CacheBase BuildCache(string regionName, IDictionary<string, string> properties)
22+
{
23+
if (!CacheData.TryGetValue(regionName ?? string.Empty, out var data))
24+
{
25+
data = new Hashtable();
26+
CacheData.Add(regionName ?? string.Empty, data);
27+
}
28+
29+
return new SerializingCache(regionName, data);
30+
}
31+
32+
public long NextTimestamp()
33+
{
34+
return Timestamper.Next();
35+
}
36+
37+
public void Start(IDictionary<string, string> properties)
38+
{
39+
var serializer = new BinaryFormatter();
40+
foreach (var cache in SerializedCacheData)
41+
{
42+
using (var stream = new MemoryStream(cache.Value))
43+
{
44+
CacheData.Add(cache.Key, (IDictionary) serializer.Deserialize(stream));
45+
}
46+
}
47+
}
48+
49+
public void Stop()
50+
{
51+
var serializer = new BinaryFormatter();
52+
foreach (var cache in CacheData)
53+
{
54+
using (var stream = new MemoryStream())
55+
{
56+
serializer.Serialize(stream, cache.Value);
57+
SerializedCacheData[cache.Key] = stream.ToArray();
58+
}
59+
}
60+
}
61+
62+
#endregion
63+
64+
private readonly Dictionary<string, IDictionary> CacheData = new Dictionary<string, IDictionary>();
65+
private static readonly Dictionary<string, byte[]> SerializedCacheData = new Dictionary<string, byte[]>();
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Linq;
2+
using NHibernate.Cfg;
3+
using NHibernate.Linq;
4+
using NHibernate.Test.CacheTest.Caches;
5+
using NUnit.Framework;
6+
using Environment = NHibernate.Cfg.Environment;
7+
8+
namespace NHibernate.Test.CacheTest
9+
{
10+
[TestFixture]
11+
public class SerializingCacheFixture : TestCase
12+
{
13+
protected override string[] Mappings => new[]
14+
{
15+
"CacheTest.ReadOnly.hbm.xml"
16+
};
17+
18+
protected override string MappingsAssembly => "NHibernate.Test";
19+
20+
protected override string CacheConcurrencyStrategy => null;
21+
22+
protected override void Configure(Configuration configuration)
23+
{
24+
configuration.SetProperty(Environment.UseSecondLevelCache, "true");
25+
configuration.SetProperty(Environment.UseQueryCache, "true");
26+
configuration.SetProperty(Environment.CacheProvider, typeof(SerializingCacheProvider).AssemblyQualifiedName);
27+
configuration.SetProperty(Environment.SessionFactoryName, "SerializingCacheFactory");
28+
}
29+
30+
protected override void OnSetUp()
31+
{
32+
using (var s = Sfi.OpenSession())
33+
using (var tx = s.BeginTransaction())
34+
{
35+
var totalItems = 6;
36+
for (var i = 1; i <= totalItems; i++)
37+
{
38+
var parent = new ReadOnly
39+
{
40+
Name = $"Name{i}"
41+
};
42+
for (var j = 1; j <= totalItems; j++)
43+
{
44+
var child = new ReadOnlyItem
45+
{
46+
Parent = parent
47+
};
48+
parent.Items.Add(child);
49+
}
50+
s.Save(parent);
51+
}
52+
tx.Commit();
53+
}
54+
}
55+
56+
protected override void OnTearDown()
57+
{
58+
using (var s = OpenSession())
59+
using (var tx = s.BeginTransaction())
60+
{
61+
s.CreateQuery("delete from ReadOnlyItem").ExecuteUpdate();
62+
s.CreateQuery("delete from ReadOnly").ExecuteUpdate();
63+
tx.Commit();
64+
}
65+
}
66+
67+
[Test]
68+
public void CachedQueryTest()
69+
{
70+
// Put things in cache
71+
using (var s = Sfi.OpenSession())
72+
using (var tx = s.BeginTransaction())
73+
{
74+
var items = s.Query<ReadOnly>().WithOptions(o => o.SetCacheable(true)).ToList();
75+
Assert.That(items, Has.Count.GreaterThan(0));
76+
tx.Commit();
77+
}
78+
79+
RebuildSessionFactory();
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)