Skip to content

Commit

Permalink
Merge pull request #2 from shortendarragh-mt/patch
Browse files Browse the repository at this point in the history
Patch
  • Loading branch information
quynh-ng authored Sep 22, 2020
2 parents 685bcc5 + 3ddac63 commit 3ea5bea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Memcached/NodeLocators/KetamaNodeLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography;

namespace Enyim.Caching.Memcached
Expand All @@ -13,6 +14,7 @@ namespace Enyim.Caching.Memcached
public sealed class KetamaNodeLocator : INodeLocator
{
static int ServerAddressMutations { get; } = 160;
static int MemcachedDefaultPort { get; } = 11211;
static Dictionary<string, Func<HashAlgorithm>> Factories { get; } = new Dictionary<string, Func<HashAlgorithm>>(StringComparer.OrdinalIgnoreCase)
{
{ "md5", () => MD5.Create() },
Expand Down Expand Up @@ -84,6 +86,16 @@ void INodeLocator.Initialize(IList<IMemcachedNode> nodes)
// 01 02 03 04 05 06 07
// server will be stored with keys 0x07060504 & 0x03020100
var address = currentNode.EndPoint.ToString();
// Other libketama-comaptible clients (libmemcached, node, pylibmc) ignore the port number
// when calculating the hash if the default port (11211) is used.
// If using a non-standard port, we use the full hostname:port
// Examples:
// libmemcached: https://bazaar.launchpad.net/~tangent-trunk/libmemcached/1.2/view/head:/libmemcached/hosts.cc#L293
// node-hashring: https://github.com/3rd-Eden/node-hashring/blob/master/index.js#L138
if (((IPEndPoint)currentNode.EndPoint).Port == MemcachedDefaultPort)
{
address = ((IPEndPoint)currentNode.EndPoint).Address.ToString();
}
for (var mutation = 0; mutation < KetamaNodeLocator.ServerAddressMutations / partCount; mutation++)
{
var data = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(address + "-" + mutation));
Expand Down
2 changes: 1 addition & 1 deletion Memcached/Protocol/Text/TextOperationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ IStoreOperation IOperationFactory.Store(StoreMode mode, string key, CacheItem va
if (cas == 0)
return new StoreOperation(mode, key, value, expires);

return new CasOperation(key, value, expires, (uint)cas);
return new CasOperation(key, value, expires, cas);
}

IDeleteOperation IOperationFactory.Delete(string key, ulong cas)
Expand Down

0 comments on commit 3ea5bea

Please sign in to comment.