Skip to content

Commit 5c6f609

Browse files
Add an option for appending hashcode to key
1 parent f97404a commit 5c6f609

File tree

12 files changed

+110
-10
lines changed

12 files changed

+110
-10
lines changed

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Memcached/NHibernate.Caches.CoreDistributedCache.Memcached.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
99
<SignAssembly>False</SignAssembly>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
11-
<PackageReleaseNotes>* Task
11+
<PackageReleaseNotes>* New feature
12+
* #47 - Add an option for appending hashcode to key
13+
14+
* Task
1215
* #46 - Update NHibernate to 5.1.0</PackageReleaseNotes>
1316
</PropertyGroup>
1417
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Memory/NHibernate.Caches.CoreDistributedCache.Memory.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ Meant for testing purpose, consider NHibernate.Caches.CoreMemoryCache for other
1010
<SignAssembly>True</SignAssembly>
1111
<AssemblyOriginatorKeyFile>..\..\NHibernate.Caches.snk</AssemblyOriginatorKeyFile>
1212
<GenerateDocumentationFile>true</GenerateDocumentationFile>
13-
<PackageReleaseNotes>* Task
13+
<PackageReleaseNotes>* New feature
14+
* #47 - Add an option for appending hashcode to key
15+
16+
* Task
1417
* #46 - Update NHibernate to 5.1.0</PackageReleaseNotes>
1518
</PropertyGroup>
1619
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Redis/NHibernate.Caches.CoreDistributedCache.Redis.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
<SignAssembly>True</SignAssembly>
1010
<AssemblyOriginatorKeyFile>..\..\NHibernate.Caches.snk</AssemblyOriginatorKeyFile>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
12-
<PackageReleaseNotes>* Task
12+
<PackageReleaseNotes>* New feature
13+
* #47 - Add an option for appending hashcode to key
14+
15+
* Task
1316
* #46 - Update NHibernate to 5.1.0</PackageReleaseNotes>
1417
</PropertyGroup>
1518
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.SqlServer/NHibernate.Caches.CoreDistributedCache.SqlServer.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
<SignAssembly>True</SignAssembly>
1010
<AssemblyOriginatorKeyFile>..\..\NHibernate.Caches.snk</AssemblyOriginatorKeyFile>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
12-
<PackageReleaseNotes>* Task
12+
<PackageReleaseNotes>* New feature
13+
* #47 - Add an option for appending hashcode to key
14+
15+
* Task
1316
* #46 - Update NHibernate to 5.1.0</PackageReleaseNotes>
1417
</PropertyGroup>
1518
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheProviderFixture.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,25 @@ public void TestExpiration()
6464
Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(50)),
6565
"Unexpected expiration value for noExplicitExpiration region with cache.default_expiration");
6666
}
67+
68+
[Test]
69+
public void TestAppendHashcodeToKey()
70+
{
71+
Assert.That(CoreDistributedCacheProvider.AppendHashcodeToKey, Is.True, "Default is not true");
72+
73+
var cache = DefaultProvider.BuildCache("foo", null) as CoreDistributedCache;
74+
Assert.That(cache.AppendHashcodeToKey, Is.True, "First built cache not correctly set");
75+
76+
CoreDistributedCacheProvider.AppendHashcodeToKey = false;
77+
try
78+
{
79+
cache = DefaultProvider.BuildCache("foo", null) as CoreDistributedCache;
80+
Assert.That(cache.AppendHashcodeToKey, Is.False, "Second built cache not correctly set");
81+
}
82+
finally
83+
{
84+
CoreDistributedCacheProvider.AppendHashcodeToKey = true;
85+
}
86+
}
6787
}
6888
}

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheSectionHandlerFixture.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ public void TestGetConfigNullSection()
4848
Assert.That(config.Properties.Count, Is.EqualTo(0), "Properties count");
4949
Assert.That(config.Regions, Is.Not.Null, "Regions");
5050
Assert.That(config.Regions.Length, Is.EqualTo(0));
51+
Assert.That(config.AppendHashcodeToKey, Is.True);
5152
}
5253

5354
[Test]
5455
public void TestGetConfigFromFile()
5556
{
56-
const string xmlSimple = "<coredistributedcache factory-class=\"factory1\"><properties><property name=\"prop1\">Value1</property></properties><cache region=\"foo\" expiration=\"500\" sliding=\"true\" /></coredistributedcache>";
57+
const string xmlSimple = "<coredistributedcache factory-class=\"factory1\" append-hashcode=\"false\"><properties><property name=\"prop1\">Value1</property></properties><cache region=\"foo\" expiration=\"500\" sliding=\"true\" /></coredistributedcache>";
5758

5859
var handler = new CoreDistributedCacheSectionHandler();
5960
var section = GetConfigurationSection(xmlSimple);
@@ -76,6 +77,8 @@ public void TestGetConfigFromFile()
7677
Assert.That(config.Regions[0].Properties["cache.use_sliding_expiration"], Is.EqualTo("true"));
7778
Assert.That(config.Regions[0].Properties, Does.ContainKey("expiration"));
7879
Assert.That(config.Regions[0].Properties["expiration"], Is.EqualTo("500"));
80+
81+
Assert.That(config.AppendHashcodeToKey, Is.False);
7982
}
8083
}
8184
}

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CacheConfig.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using Microsoft.Extensions.Caching.Distributed;
34

@@ -15,17 +16,40 @@ public class CacheConfig
1516
/// <see cref="IDistributedCache"/> instances.</param>
1617
/// <param name="properties">The cache configuration properties.</param>
1718
/// <param name="regions">The configured cache regions.</param>
18-
public CacheConfig(string factoryClass, IDictionary<string, string> properties, RegionConfig[] regions)
19+
// Since 5.4
20+
[Obsolete("Use overload with appendHashcodeToKey additional parameter")]
21+
public CacheConfig(string factoryClass, IDictionary<string, string> properties, RegionConfig[] regions) :
22+
this(factoryClass, properties, regions, true)
23+
{
24+
}
25+
26+
/// <summary>
27+
/// Build a cache configuration.
28+
/// </summary>
29+
/// <param name="factoryClass">The <see cref="IDistributedCacheFactory"/> factory class name to use for getting
30+
/// <see cref="IDistributedCache"/> instances.</param>
31+
/// <param name="properties">The cache configuration properties.</param>
32+
/// <param name="regions">The configured cache regions.</param>
33+
/// <param name="appendHashcodeToKey">Should the keys be appended with their hashcode?</param>
34+
public CacheConfig(
35+
string factoryClass, IDictionary<string, string> properties, RegionConfig[] regions, bool appendHashcodeToKey)
1936
{
2037
FactoryClass = factoryClass;
2138
Regions = regions;
2239
Properties = properties;
40+
AppendHashcodeToKey = appendHashcodeToKey;
2341
}
2442

2543
/// <summary>The <see cref="IDistributedCacheFactory"/> factory class name to use for getting
2644
/// <see cref="IDistributedCache"/> instances.</summary>
2745
public string FactoryClass { get; }
2846

47+
/// <summary>Should the keys be appended with their hashcode?</summary>
48+
/// <remarks>This option is a workaround for distinguishing composite-id missing an
49+
/// <see cref="object.ToString"/> override. It may causes trouble if the cache is shared
50+
/// between processes running different runtimes.</remarks>
51+
public bool AppendHashcodeToKey { get; }
52+
2953
/// <summary>The configured cache regions.</summary>
3054
public RegionConfig[] Regions { get; }
3155

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CoreDistributedCache.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public CoreDistributedCache(
9696
/// <value><see langword="true" /> for resetting a cached item expiration each time it is accessed.</value>
9797
public bool UseSlidingExpiration { get; private set; }
9898

99+
/// <summary>Should the keys be appended with their hashcode?</summary>
100+
/// <remarks>This option is a workaround for distinguishing composite-id missing an
101+
/// <see cref="object.ToString"/> override. It may causes trouble if the cache is shared
102+
/// between processes running different runtimes. Configure it through
103+
/// <see cref="CoreDistributedCacheProvider.AppendHashcodeToKey"/>.</remarks>
104+
public bool AppendHashcodeToKey { get; internal set; }
105+
99106
private void Configure(IDictionary<string, string> props)
100107
{
101108
var regionPrefix = DefaultRegionPrefix;
@@ -168,7 +175,9 @@ private static bool GetUseSlidingExpiration(IDictionary<string, string> props)
168175

169176
private string GetCacheKey(object key)
170177
{
171-
var keyAsString = string.Concat(_cacheKeyPrefix, _fullRegion, ":", key.ToString(), "@", key.GetHashCode());
178+
var keyAsString = AppendHashcodeToKey
179+
? string.Concat(_cacheKeyPrefix, _fullRegion, ":", key.ToString(), "@", key.GetHashCode())
180+
: string.Concat(_cacheKeyPrefix, _fullRegion, ":", key.ToString());
172181

173182
if (_maxKeySize < keyAsString.Length)
174183
{

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CoreDistributedCacheProvider.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ public class CoreDistributedCacheProvider : ICacheProvider
5050
[CLSCompliant(false)]
5151
public static IDistributedCacheFactory CacheFactory { get; set; }
5252

53+
/// <summary>Should the keys be appended with their hashcode?</summary>
54+
/// <value><see langword="true" /> by default.</value>
55+
/// <remarks>
56+
/// <para>This option is a workaround for distinguishing composite-id missing an
57+
/// <see cref="object.ToString"/> override. It may causes trouble if the cache is shared
58+
/// between processes running different runtimes.
59+
/// </para>
60+
/// <para>
61+
/// Changes to this property affect only caches built after the change.
62+
/// </para>
63+
/// <para>
64+
/// The value of this property can be set with the attribute <c>append-hashcode</c> of the
65+
/// <c>coredistributedcache</c> configuration section.
66+
/// </para>
67+
/// </remarks>
68+
public static bool AppendHashcodeToKey { get; set; }
69+
5370
static CoreDistributedCacheProvider()
5471
{
5572
Log = NHibernateLogger.For(typeof(CoreDistributedCacheProvider));
@@ -83,6 +100,8 @@ static CoreDistributedCacheProvider()
83100
{
84101
ConfiguredCachesProperties.Add(cache.Region, cache.Properties);
85102
}
103+
104+
AppendHashcodeToKey = config.AppendHashcodeToKey;
86105
}
87106

88107
#region ICacheProvider Members
@@ -139,7 +158,11 @@ public ICache BuildCache(string regionName, IDictionary<string, string> properti
139158

140159
Log.Debug("building cache with region: {0}, properties: {1}, factory: {2}" , regionName, sb.ToString(), CacheFactory.GetType().FullName);
141160
}
142-
return new CoreDistributedCache(CacheFactory.BuildCache(), CacheFactory.Constraints, regionName, properties);
161+
return
162+
new CoreDistributedCache(CacheFactory.BuildCache(), CacheFactory.Constraints, regionName, properties)
163+
{
164+
AppendHashcodeToKey = AppendHashcodeToKey
165+
};
143166
}
144167

145168
/// <inheritdoc />

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CoreDistributedCacheSectionHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Configuration;
34
using System.Xml;
@@ -52,8 +53,10 @@ public object Create(object parent, object configContext, XmlNode section)
5253
node.OuterXml);
5354
}
5455
}
56+
var appendHashcodeToKey = !StringComparer.OrdinalIgnoreCase.Equals(
57+
section.Attributes?["append-hashcode"]?.Value, "false");
5558

56-
return new CacheConfig(factoryClass, properties, caches.ToArray());
59+
return new CacheConfig(factoryClass, properties, caches.ToArray(), appendHashcodeToKey);
5760
}
5861

5962
#endregion

0 commit comments

Comments
 (0)