Skip to content

Commit a780f42

Browse files
committed
feat: enable to use legacy FNV hashes
1 parent ae553d1 commit a780f42

File tree

8 files changed

+269
-20
lines changed

8 files changed

+269
-20
lines changed

sample/SampleWebApp/appsettings.example.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"SuppressException": false,
1818
"UseSslStream": false,
1919
"UseIPv6": false,
20+
"UseLegacyNodeLocator": false,
2021
"Transcoder": "MessagePackTranscoder",
2122
"KeyTransformer": "Enyim.Caching.Memcached.SHA1KeyTransformer",
2223
"Authentication": {

sample/SampleWebApp/appsettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"queueTimeout": "00:00:00.150"
1616
},
1717
"suppressException": false,
18+
"UseLegacyNodeLocator": false,
1819
"Transcoder": "MessagePackTranscoder"
1920
},
2021

src/Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Net;
41
using Enyim.Caching.Memcached;
5-
using Enyim.Reflection;
62
using Enyim.Caching.Memcached.Protocol.Binary;
3+
using Enyim.Caching.Memcached.Transcoders;
4+
using Enyim.Reflection;
5+
using Microsoft.Extensions.Configuration;
76
using Microsoft.Extensions.Logging;
87
using Microsoft.Extensions.Options;
9-
using Microsoft.Extensions.Configuration;
8+
using System;
9+
using System.Collections.Generic;
1010
using System.Linq;
11+
using System.Net;
1112
using System.Net.Security;
1213
using System.Net.Sockets;
13-
using Enyim.Caching.Memcached.Transcoders;
1414

1515
namespace Enyim.Caching.Configuration
1616
{
@@ -150,7 +150,14 @@ public MemcachedClientConfiguration(
150150

151151
if (NodeLocator == null)
152152
{
153-
NodeLocator = options.Servers.Count > 1 ? typeof(DefaultNodeLocator) : typeof(SingleNodeLocator);
153+
if (options.Servers.Count > 1)
154+
{
155+
NodeLocator = options.UseLegacyNodeLocator ? typeof(LegacyNodeLocator) : typeof(DefaultNodeLocator);
156+
}
157+
else
158+
{
159+
NodeLocator = typeof(SingleNodeLocator);
160+
}
154161
}
155162

156163
if (!string.IsNullOrEmpty(options.Transcoder))

src/Enyim.Caching/Configuration/MemcachedClientOptions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using Microsoft.Extensions.Options;
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65
using System.Net.Security;
7-
using System.Threading.Tasks;
86

97
namespace Enyim.Caching.Configuration
108
{
@@ -16,6 +14,8 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>
1614

1715
public List<Server> Servers { get; set; } = new List<Server>();
1816

17+
public bool UseLegacyNodeLocator { get; set; }
18+
1919
public Authentication Authentication { get; set; }
2020

2121
public string KeyTransformer { get; set; }

src/Enyim.Caching/FnvHash.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Enyim
1010
/// Calculation found at http://lists.danga.com/pipermail/memcached/2007-April/003846.html, but
1111
/// it is pretty much available everywhere
1212
/// </remarks>
13-
public class FNV64 : System.Security.Cryptography.HashAlgorithm, IUIntHashAlgorithm
13+
public class FNV64 : HashAlgorithm, IUIntHashAlgorithm
1414
{
1515
protected const ulong Init = 0xcbf29ce484222325L;
1616
protected const ulong Prime = 0x100000001b3L;
@@ -20,10 +20,10 @@ public class FNV64 : System.Security.Cryptography.HashAlgorithm, IUIntHashAlgori
2020
/// <summary>
2121
/// Initializes a new instance of the <see cref="T:FNV64"/> class.
2222
/// </summary>
23-
public FNV64()
23+
public FNV64(bool initialize)
2424
{
2525
//base.HashSize = 64;
26-
Initialize();
26+
if (initialize) Initialize();
2727
}
2828

2929
/// <summary>
@@ -76,6 +76,8 @@ uint IUIntHashAlgorithm.ComputeHash(byte[] data)
7676
/// </summary>
7777
public sealed class FNV64a : FNV64
7878
{
79+
public FNV64a(bool initialize) : base(initialize) { }
80+
7981
/// <summary>Routes data written to the object into the <see cref="T:FNV64" /> hash algorithm for computing the hash.</summary>
8082
/// <param name="array">The input data. </param>
8183
/// <param name="ibStart">The offset into the byte array from which to begin using data. </param>
@@ -108,9 +110,9 @@ public class FNV1 : HashAlgorithm, IUIntHashAlgorithm
108110
/// <summary>
109111
/// Initializes a new instance of the <see cref="T:FNV1a"/> class.
110112
/// </summary>
111-
public FNV1()
113+
public FNV1(bool initialize)
112114
{
113-
Initialize();
115+
if (initialize) Initialize();
114116
}
115117

116118
/// <summary>
@@ -163,6 +165,8 @@ uint IUIntHashAlgorithm.ComputeHash(byte[] data)
163165
/// </summary>
164166
public class FNV1a : FNV1
165167
{
168+
public FNV1a(bool initialize) : base(initialize) { }
169+
166170
/// <summary>Routes data written to the object into the <see cref="T:FNV1a" /> hash algorithm for computing the hash.</summary>
167171
/// <param name="array">The input data. </param>
168172
/// <param name="ibStart">The offset into the byte array from which to begin using data. </param>
@@ -185,6 +189,8 @@ protected override void HashCore(byte[] array, int ibStart, int cbSize)
185189
/// <remarks>Algorithm found at http://bretm.home.comcast.net/hash/6.html</remarks>
186190
public class ModifiedFNV : FNV1a
187191
{
192+
public ModifiedFNV(bool initialize) : base(initialize) { }
193+
188194
/// <summary>
189195
/// Returns the computed <see cref="T:ModifiedFNV" /> hash value after all data has been written to the object.
190196
/// </summary>

src/Enyim.Caching/Memcached/Locators/DefaultNodeLocator.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2-
using System.Linq;
32
using System.Collections.Generic;
3+
using System.Linq;
44
using System.Text;
55
using System.Threading;
66

@@ -42,7 +42,7 @@ private void BuildIndex(List<IMemcachedNode> nodes)
4242

4343
foreach (IMemcachedNode node in nodes)
4444
{
45-
var tmpKeys = DefaultNodeLocator.GenerateKeys(node, _serverAddressMutations);
45+
var tmpKeys = GenerateKeys(node, _serverAddressMutations);
4646

4747
for (var i = 0; i < tmpKeys.Length; i++)
4848
{
@@ -126,7 +126,7 @@ private IMemcachedNode FindNode(string key)
126126
{
127127
if (_keys.Length == 0) return null;
128128

129-
uint itemKeyHash = BitConverter.ToUInt32(new FNV1a().ComputeHash(Encoding.UTF8.GetBytes(key)), 0);
129+
uint itemKeyHash = BitConverter.ToUInt32(new FNV1a(true).ComputeHash(Encoding.UTF8.GetBytes(key)), 0);
130130
// get the index of the server assigned to this hash
131131
int foundIndex = Array.BinarySearch<uint>(_keys, itemKeyHash);
132132

@@ -168,11 +168,11 @@ private static uint[] GenerateKeys(IMemcachedNode node, int numberOfKeys)
168168
// server will be stored with keys 0x0000aabb & 0x0000ccdd
169169
// (or a bit differently based on the little/big indianness of the host)
170170
string address = node.EndPoint.ToString();
171-
var fnv = new FNV1a();
171+
var fnv = new FNV1a(true);
172172

173173
for (int i = 0; i < numberOfKeys; i++)
174174
{
175-
byte[] data = fnv.ComputeHash(Encoding.UTF8.GetBytes(String.Concat(i, "-", address)));
175+
byte[] data = fnv.ComputeHash(Encoding.UTF8.GetBytes(string.Concat(i, "-", address)));
176176

177177
for (int h = 0; h < PartCount; h++)
178178
{

0 commit comments

Comments
 (0)