Skip to content

Commit 5bbfdd9

Browse files
Add a .Net Core distributed cache
* Fixes #28
1 parent 0f99cee commit 5bbfdd9

26 files changed

+1277
-52
lines changed

AsyncGenerator.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
projects:
2+
- filePath: CoreDistributedCache\NHibernate.Caches.CoreDistributedCache\NHibernate.Caches.CoreDistributedCache.csproj
3+
targetFramework: net461
4+
concurrentRun: true
5+
applyChanges: true
6+
analyzation:
7+
methodConversion:
8+
- conversion: Ignore
9+
hasAttributeName: ObsoleteAttribute
10+
callForwarding: true
11+
cancellationTokens:
12+
guards: true
13+
methodParameter:
14+
- anyInterfaceRule: PubliclyExposedType
15+
parameter: Optional
16+
- parameter: Optional
17+
rule: PubliclyExposedType
18+
- parameter: Required
19+
scanMethodBody: true
20+
scanForMissingAsyncMembers:
21+
- all: true
22+
transformation:
23+
configureAwaitArgument: false
24+
localFunctions: true
25+
asyncLock:
26+
type: NHibernate.Util.AsyncLock
27+
methodName: LockAsync
28+
registerPlugin:
29+
- type: AsyncGenerator.Core.Plugins.EmptyRegionRemover
30+
assemblyName: AsyncGenerator.Core
231
- filePath: CoreMemoryCache\NHibernate.Caches.CoreMemoryCache\NHibernate.Caches.CoreMemoryCache.csproj
332
targetFramework: net461
433
concurrentRun: true
@@ -293,6 +322,47 @@
293322
registerPlugin:
294323
- type: AsyncGenerator.Core.Plugins.NUnitAsyncCounterpartsFinder
295324
assemblyName: AsyncGenerator.Core
325+
- filePath: CoreDistributedCache\NHibernate.Caches.CoreDistributedCache.Tests\NHibernate.Caches.CoreDistributedCache.Tests.csproj
326+
targetFramework: net461
327+
concurrentRun: true
328+
applyChanges: true
329+
analyzation:
330+
methodConversion:
331+
- conversion: Ignore
332+
hasAttributeName: OneTimeSetUpAttribute
333+
- conversion: Ignore
334+
hasAttributeName: OneTimeTearDownAttribute
335+
- conversion: Ignore
336+
hasAttributeName: SetUpAttribute
337+
- conversion: Ignore
338+
hasAttributeName: TearDownAttribute
339+
- conversion: Smart
340+
hasAttributeName: TestAttribute
341+
- conversion: Smart
342+
hasAttributeName: TheoryAttribute
343+
preserveReturnType:
344+
- hasAttributeName: TestAttribute
345+
- hasAttributeName: TheoryAttribute
346+
typeConversion:
347+
- conversion: Ignore
348+
hasAttributeName: IgnoreAttribute
349+
- conversion: Partial
350+
hasAttributeName: TestFixtureAttribute
351+
- conversion: Partial
352+
anyBaseTypeRule: HasTestFixtureAttribute
353+
ignoreSearchForMethodReferences:
354+
- hasAttributeName: TheoryAttribute
355+
- hasAttributeName: TestAttribute
356+
cancellationTokens:
357+
withoutCancellationToken:
358+
- hasAttributeName: TestAttribute
359+
- hasAttributeName: TheoryAttribute
360+
scanMethodBody: true
361+
scanForMissingAsyncMembers:
362+
- all: true
363+
registerPlugin:
364+
- type: AsyncGenerator.Core.Plugins.NUnitAsyncCounterpartsFinder
365+
assemblyName: AsyncGenerator.Core
296366
- filePath: CoreMemoryCache\NHibernate.Caches.CoreMemoryCache.Tests\NHibernate.Caches.CoreMemoryCache.Tests.csproj
297367
targetFramework: net461
298368
concurrentRun: true
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<configSections>
4+
<section name="coredistributedcache" type="NHibernate.Caches.CoreDistributedCache.CoreDistributedCacheSectionHandler,NHibernate.Caches.CoreDistributedCache" />
5+
<section name="hibernate-configuration"
6+
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
7+
</configSections>
8+
9+
<coredistributedcache factory-class="NHibernate.Caches.CoreDistributedCache.Tests.MemoryDistributedCacheFactory,NHibernate.Caches.CoreDistributedCache.Tests">
10+
<cache region="foo" expiration="500" sliding="true" />
11+
<cache region="noExplicitExpiration" sliding="true" />
12+
</coredistributedcache>
13+
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
14+
<session-factory>
15+
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
16+
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
17+
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
18+
<property name="connection.connection_string">
19+
Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI
20+
</property>
21+
<property name="cache.provider_class">NHibernate.Caches.CoreDistributedCache.CoreDistributedCacheProvider,NHibernate.Caches.CoreDistributedCache</property>
22+
</session-factory>
23+
</hibernate-configuration>
24+
</configuration>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#region License
2+
3+
//
4+
// CoreDistributedCache - A cache provider for NHibernate using Microsoft.Extensions.Caching.Distributed.IDistributedCache.
5+
//
6+
// This library is free software; you can redistribute it and/or
7+
// modify it under the terms of the GNU Lesser General Public
8+
// License as published by the Free Software Foundation; either
9+
// version 2.1 of the License, or (at your option) any later version.
10+
//
11+
// This library is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
// Lesser General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Lesser General Public
17+
// License along with this library; if not, write to the Free Software
18+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
//
20+
21+
#endregion
22+
23+
using System;
24+
using NHibernate.Cache;
25+
using NHibernate.Caches.Common.Tests;
26+
using NUnit.Framework;
27+
28+
namespace NHibernate.Caches.CoreDistributedCache.Tests
29+
{
30+
[TestFixture]
31+
public class CoreDistributedCacheFixture : CacheFixture
32+
{
33+
protected override bool SupportsSlidingExpiration => true;
34+
protected override bool SupportsClear => false;
35+
36+
protected override Func<ICacheProvider> ProviderBuilder =>
37+
() => new CoreDistributedCacheProvider();
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#region License
2+
3+
//
4+
// CoreDistributedCache - A cache provider for NHibernate using Microsoft.Extensions.Caching.Distributed.IDistributedCache.
5+
//
6+
// This library is free software; you can redistribute it and/or
7+
// modify it under the terms of the GNU Lesser General Public
8+
// License as published by the Free Software Foundation; either
9+
// version 2.1 of the License, or (at your option) any later version.
10+
//
11+
// This library is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
// Lesser General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Lesser General Public
17+
// License along with this library; if not, write to the Free Software
18+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
//
20+
21+
#endregion
22+
23+
using System;
24+
using System.Collections.Generic;
25+
using NHibernate.Cache;
26+
using NHibernate.Caches.Common.Tests;
27+
using NUnit.Framework;
28+
29+
namespace NHibernate.Caches.CoreDistributedCache.Tests
30+
{
31+
[TestFixture]
32+
public class CoreDistributedCacheProviderFixture : CacheProviderFixture
33+
{
34+
protected override Func<ICacheProvider> ProviderBuilder =>
35+
() => new CoreDistributedCacheProvider();
36+
37+
[Test]
38+
public void TestBuildCacheFromConfig()
39+
{
40+
var cache = DefaultProvider.BuildCache("foo", null);
41+
Assert.That(cache, Is.Not.Null, "pre-configured cache not found");
42+
}
43+
44+
[Test]
45+
public void TestExpiration()
46+
{
47+
var cache = DefaultProvider.BuildCache("foo", null) as CoreDistributedCache;
48+
Assert.That(cache, Is.Not.Null, "pre-configured foo cache not found");
49+
Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(500)), "Unexpected expiration value for foo region");
50+
51+
cache = (CoreDistributedCache)DefaultProvider.BuildCache("noExplicitExpiration", null);
52+
Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(300)),
53+
"Unexpected expiration value for noExplicitExpiration region");
54+
Assert.That(cache.UseSlidingExpiration, Is.True, "Unexpected sliding value for noExplicitExpiration region");
55+
56+
cache = (CoreDistributedCache)DefaultProvider
57+
.BuildCache("noExplicitExpiration", new Dictionary<string, string> { { "expiration", "100" } });
58+
Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(100)),
59+
"Unexpected expiration value for noExplicitExpiration region with default expiration");
60+
61+
cache = (CoreDistributedCache)DefaultProvider
62+
.BuildCache("noExplicitExpiration", new Dictionary<string, string> { { Cfg.Environment.CacheDefaultExpiration, "50" } });
63+
Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(50)),
64+
"Unexpected expiration value for noExplicitExpiration region with cache.default_expiration");
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#region License
2+
3+
//
4+
// CoreDistributedCache - A cache provider for NHibernate using Microsoft.Extensions.Caching.Distributed.IDistributedCache.
5+
//
6+
// This library is free software; you can redistribute it and/or
7+
// modify it under the terms of the GNU Lesser General Public
8+
// License as published by the Free Software Foundation; either
9+
// version 2.1 of the License, or (at your option) any later version.
10+
//
11+
// This library is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
// Lesser General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Lesser General Public
17+
// License along with this library; if not, write to the Free Software
18+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
//
20+
21+
#endregion
22+
23+
using System.Xml;
24+
using NUnit.Framework;
25+
26+
namespace NHibernate.Caches.CoreDistributedCache.Tests
27+
{
28+
[TestFixture]
29+
public class CoreDistributedCacheSectionHandlerFixture
30+
{
31+
private static XmlNode GetConfigurationSection(string xml)
32+
{
33+
var doc = new XmlDocument();
34+
doc.LoadXml(xml);
35+
return doc.DocumentElement;
36+
}
37+
38+
[Test]
39+
public void TestGetConfigNullSection()
40+
{
41+
var handler = new CoreDistributedCacheSectionHandler();
42+
var section = new XmlDocument();
43+
var result = handler.Create(null, null, section);
44+
Assert.That(result, Is.Not.Null);
45+
Assert.IsTrue(result is CacheConfig[]);
46+
var caches = result as CacheConfig[];
47+
Assert.That(caches.Length, Is.EqualTo(0));
48+
}
49+
50+
[Test]
51+
public void TestGetConfigFromFile()
52+
{
53+
const string xmlSimple = "<coredistributedcache><cache region=\"foo\" expiration=\"500\" sliding=\"true\" /></coredistributedcache>";
54+
55+
var handler = new CoreDistributedCacheSectionHandler();
56+
var section = GetConfigurationSection(xmlSimple);
57+
var result = handler.Create(null, null, section);
58+
Assert.That(result, Is.Not.Null);
59+
Assert.IsTrue(result is CacheConfig[]);
60+
var caches = result as CacheConfig[];
61+
Assert.That(caches.Length, Is.EqualTo(1));
62+
Assert.That(caches[0].Properties, Does.ContainKey("cache.use_sliding_expiration"));
63+
}
64+
}
65+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.Extensions.Caching.Distributed;
2+
using Microsoft.Extensions.Caching.Memory;
3+
using Microsoft.Extensions.Options;
4+
5+
namespace NHibernate.Caches.CoreDistributedCache.Tests
6+
{
7+
public class MemoryDistributedCacheFactory : ICoreDistributedCacheFactory
8+
{
9+
public IDistributedCache BuildCache()
10+
{
11+
return new MemoryDistributedCache(new Options());
12+
}
13+
14+
private class Options : MemoryDistributedCacheOptions, IOptions<MemoryDistributedCacheOptions>
15+
{
16+
MemoryDistributedCacheOptions IOptions<MemoryDistributedCacheOptions>.Value => this;
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="../../NHibernate.Caches.props" />
3+
<PropertyGroup>
4+
<Product>NHibernate.Caches.CoreDistributedCache</Product>
5+
<Description>Unit tests of cache provider for NHibernate using .Net Core IDistributedCache (Microsoft.Extensions.Caching.Abstractions).</Description>
6+
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
7+
<IsTestProject>true</IsTestProject>
8+
</PropertyGroup>
9+
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">
10+
<DefineConstants>NETFX;$(DefineConstants)</DefineConstants>
11+
</PropertyGroup>
12+
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
13+
<OutputType>Exe</OutputType>
14+
<GenerateProgramFile>false</GenerateProgramFile>
15+
</PropertyGroup>
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\NHibernate.Caches.Common.Tests\NHibernate.Caches.Common.Tests.csproj" />
18+
<ProjectReference Include="..\NHibernate.Caches.CoreDistributedCache\NHibernate.Caches.CoreDistributedCache.csproj" />
19+
</ItemGroup>
20+
<ItemGroup>
21+
<PackageReference Include="log4net" Version="2.0.8" />
22+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
23+
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
24+
</ItemGroup>
25+
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.0'">
26+
<PackageReference Include="NUnitLite" Version="3.9.0" />
27+
</ItemGroup>
28+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#if !NETFX
2+
namespace NHibernate.Caches.CoreDistributedCache.Tests
3+
{
4+
public class Program
5+
{
6+
public static int Main(string[] args)
7+
{
8+
return new NUnitLite.AutoRun(typeof(Program).Assembly).Execute(args);
9+
}
10+
}
11+
}
12+
#endif
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System;
2+
3+
[assembly: CLSCompliant(false)]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#if !NETFX
2+
using log4net.Repository.Hierarchy;
3+
using NHibernate.Caches.Common.Tests;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Caches.CoreDistributedCache.Tests
7+
{
8+
[SetUpFixture]
9+
public class TestsContext
10+
{
11+
[OneTimeSetUp]
12+
public void RunBeforeAnyTests()
13+
{
14+
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, "coredistributedcache");
15+
//When .NET Core App 2.0 tests run from VS/VSTest the entry assembly is "testhost.dll"
16+
//so we need to explicitly load the configuration
17+
if (TestsContextHelper.ExecutingWithVsTest)
18+
{
19+
ConfigureLog4Net();
20+
}
21+
}
22+
23+
[OneTimeTearDown]
24+
public void RunAfterAnyTests()
25+
{
26+
TestsContextHelper.RunAfterAnyTests();
27+
}
28+
29+
private static void ConfigureLog4Net()
30+
{
31+
var hierarchy = (Hierarchy)log4net.LogManager.GetRepository(typeof(TestsContext).Assembly);
32+
33+
var consoleAppender = new log4net.Appender.ConsoleAppender
34+
{
35+
Layout = new log4net.Layout.PatternLayout("%d{ABSOLUTE} %-5p %c{1}:%L - %m%n"),
36+
};
37+
hierarchy.Root.Level = log4net.Core.Level.Info;
38+
hierarchy.Root.AddAppender(consoleAppender);
39+
hierarchy.Configured = true;
40+
}
41+
}
42+
}
43+
#endif

0 commit comments

Comments
 (0)