working on it ...

Filters

Explore Public Snippets

Sort by

Found 42 snippets matching: enyimmemcached-master

    public by Geometry  126457  0  4  0

    Combine: Combine will attempt to minimize the depth of InnerResults and maintain status codes

    Combine will attempt to minimize the depth of InnerResults and maintain status codes
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    /// <summary>
    /// Combine will attempt to minimize the depth of InnerResults and maintain status codes
    /// </summary>
    /// <param name="target"></param>
    public static void Combine(this IOperationResult source, IOperationResult target)
    {
    	target.Message = source.Message;
    	target.Success = source.Success;
    	target.Exception = source.Exception;
    	target.StatusCode = source.StatusCode ?? target.StatusCode;
    	target.InnerResult = source.InnerResult ?? source;
    }

    public by Geometry  87453  0  4  0

    Allows to DecodeUInt64

    using System;
    using System.Text;
    
    public static unsafe ulong DecodeUInt64(byte[] buffer, int offset)
    {
    	fixed (byte* ptr = buffer)
    	{
    		return DecodeUInt64(ptr, offset);
    	}
    }

    public by Geometry  77049  1  4  0

    Allows to GenerateKeys

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    private static uint[] GenerateKeys(IMemcachedNode node, int numberOfKeys)
    {
    	const int KeyLength = 4;
    	const int PartCount = 1; // (ModifiedFNV.HashSize / 8) / KeyLength; // HashSize is in bits, uint is 4 byte long
    
    	var k = new uint[PartCount * numberOfKeys];
    
    	// every server is registered numberOfKeys times
    	// using UInt32s generated from the different parts of the hash
    	// i.e. hash is 64 bit:
    	// 00 00 aa bb 00 00 cc dd
    	// server will be stored with keys 0x0000aabb & 0x0000ccdd
    	// (or a bit differently based on the little/big indianness of the host)
    	string address = node.EndPoint.ToString();
    	var fnv = new FNV1a();
    
    	for (int i = 0; i < numberOfKeys; i++)
    	{
    		byte[] data = fnv.ComputeHash(Encoding.ASCII.GetBytes(String.Concat(address, "-", i)));
    
    		for (int h = 0; h < PartCount; h++)
    		{
    			k[i * PartCount + h] = BitConverter.ToUInt32(data, h * KeyLength);
    		}
    	}
    
    	return k;
    }

    public by Geometry  53033  0  4  0

    StressTest

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading;
    using Enyim.Caching;
    using Enyim.Caching.Configuration;
    using Enyim.Caching.Memcached;
    
    private static void StressTest(MemcachedClient client, string keyPrefix)
    {
    	var i = 0;
    	var last = true;
    
    	var progress = @"-\|/".ToCharArray();
    	Console.CursorVisible = false;
    	Dictionary<bool, int> counters = new Dictionary<bool, int>() { { true, 0 }, { false, 0 } };
    
    	while (true)
    	{
    		var key = keyPrefix + i;
    		var state = client.Store(StoreMode.Set, key, i) & client.Get<int>(key) == i;
    
    		Action updateTitle = () => Console.Title = "Success: " + counters[true] + " Fail: " + counters[false];
    
    		if (state != last)
    		{
    			Console.ForegroundColor = state ? ConsoleColor.White : ConsoleColor.Red;
    			Console.Write(".");
    
    			counters[state] = 0;
    			last = state;
    
    			updateTitle();
    		}
    		else if (i % 200 == 0)
    		{
    			//Console.ForegroundColor = state ? ConsoleColor.White : ConsoleColor.Red;
    
    			//Console.Write(progress[(i / 200) % 4]);
    			//if (Console.CursorLeft == 0)
    			//{
    			//    Console.CursorLeft = Console.WindowWidth - 1;
    			//    Console.CursorTop -= 1;
    			//}
    			//else
    			//{
    			//    Console.CursorLeft -= 1;
    			//}
    
    			updateTitle();
    		}
    
    		i++;
    		counters[state] = counters[state] + 1;
    	}
    }

    public by Geometry  1368  0  4  0

    GetCommandBuffer: Gets the bytes representing the specified command. returned buffer can be used to streamline multiple writes into one Write on the Socket using the <see cref="M:Enyim.Caching.Memcached.PooledSocket.Write(IList&lt;ArraySegment&lt;b...

    Gets the bytes representing the specified command. returned buffer can be used to streamline multiple writes into one Write on the Socket using the The command to be converted. The buffer containing the bytes representing the command. The command must be terminated by \r\n. The Nagle algorithm is disabled on the socket to speed things up, so it's
    using System;
    using System.IO;
    using System.Text;
    using System.Collections.Generic;
    
    /// <summary>
    /// Gets the bytes representing the specified command. returned buffer can be used to streamline multiple writes into one Write on the Socket
    /// using the <see cref="M:Enyim.Caching.Memcached.PooledSocket.Write(IList&lt;ArraySegment&lt;byte&gt;&gt;)"/>
    /// </summary>
    /// <param name="value">The command to be converted.</param>
    /// <returns>The buffer containing the bytes representing the command. The command must be terminated by \r\n.</returns>
    /// <remarks>The Nagle algorithm is disabled on the socket to speed things up, so it's recommended to convert a command into a buffer
    /// and use the <see cref="M:Enyim.Caching.Memcached.PooledSocket.Write(IList&lt;ArraySegment&lt;byte&gt;&gt;)"/> to send the command and the additional buffers in one transaction.</remarks>
    public unsafe static IList<ArraySegment<byte>> GetCommandBuffer(string value)
    {
    	var data = new ArraySegment<byte>(Encoding.ASCII.GetBytes(value));
    
    	return new ArraySegment<byte>[] { data };
    }

    public by Geometry  1251  1  5  0

    Allows to DecodeUInt16

    using System;
    using System.Text;
    
    public static unsafe ushort DecodeUInt16(byte[] buffer, int offset)
    {
    	return (ushort)((buffer[offset] << 8) + buffer[offset + 1]);
    }

    public by Geometry  1159  0  5  0

    Copy: Copy properties from one IOperationResult to another. Does not use reflection. Ony LCD properties are copied

    Copy properties from one IOperationResult to another. Does not use reflection. Ony LCD properties are copied
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    /// <summary>
    /// Copy properties from one IOperationResult to another.  Does not use reflection.
    /// Ony LCD properties are copied
    /// </summary>
    /// <param name="target"></param>
    public static void Copy(this IOperationResult source, IOperationResult target)
    {
    	target.Message = source.Message;
    	target.Success = source.Success;
    	target.Exception = source.Exception;
    	target.StatusCode = source.StatusCode;
    }

    public by Geometry  1069  0  5  0

    SafeWaitAllAndDispose: Waits for all WaitHandles and works in both STA and MTA mode.

    Waits for all WaitHandles and works in both STA and MTA mode.
    using System;
    using System.Linq;
    using System.Configuration;
    using Enyim.Caching.Configuration;
    using Enyim.Caching.Memcached;
    using System.Collections.Generic;
    using System.Threading;
    using System.Net;
    using System.Diagnostics;
    using Enyim.Caching.Memcached.Results;
    using Enyim.Caching.Memcached.Results.Factories;
    using Enyim.Caching.Memcached.Results.Extensions;
    
    /// <summary>
    /// Waits for all WaitHandles and works in both STA and MTA mode.
    /// </summary>
    /// <param name="waitHandles"></param>
    private static void SafeWaitAllAndDispose(WaitHandle[] waitHandles)
    {
    	try
    	{
    		if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA)
    			WaitHandle.WaitAll(waitHandles);
    		else
    			for (var i = 0; i < waitHandles.Length; i++)
    				waitHandles[i].WaitOne();
    	}
    	finally
    	{
    		for (var i = 0; i < waitHandles.Length; i++)
    			waitHandles[i].Close();
    	}
    }

    public by Geometry  967  1  4  0

    GetLogger: Returns a new logger for the specified Type.

    Returns a new logger for the specified Type.
    using System;
    using System.Configuration;
    
    private static ILogFactory factory;
    private static ILogFactory factory;
    
    static LogManager()
    		{
    			var section = ConfigurationManager.GetSection("enyim.com/log") as Enyim.Caching.Configuration.LoggerSection;
    			ILogFactory f = null;
    
    			if (section != null && section.LogFactory != null)
    			{
    				f = Enyim.Reflection.FastActivator.Create(section.LogFactory) as ILogFactory;
    			}
    #if !log4net
    			// use an empty logger if nothing is specified in the app.config
    			LogManager.factory = f ?? (ILogFactory)new NullLoggerFactory();
    #else
    			// use the log4net logger logger if nothing is specified in the app.config
    			LogManager.factory = f ?? (ILogFactory)new Log4NetLogFactory();
    #endif
    		}
    
    /// <summary>
    /// Returns a new logger for the specified Type.
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static ILog GetLogger(Type type)
    {
    	return factory.GetLogger(type);
    }

    public by Geometry  962  0  4  0

    UnsafeHashCore

    // this could be rewritten to support streams tho // cache the tail of the buffer if its length is not mod4 then merge with the next buffer (this is a perf hit since we cannot do our pointer magics) // then the swicth and the last XORs could be moved into TransformFinal // -- or -- // just cache tail and if we have a cache dvalue and the next b
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Cryptography;
    
    #region [ UnsafeHashCore               ]
    
    // this could be rewritten to support streams tho
    // cache the tail of the buffer if its length is not mod4 then merge with the next buffer (this is a perf hit since we cannot do our pointer magics)
    // then the swicth and the last XORs could be moved into TransformFinal
    // -- or --
    // just cache tail and if we have a cache dvalue and the next block is not mod4 long then throw an exception (thus only allow random length blocks for the last one)
    static unsafe uint UnsafeHashCore(byte[] data, int offset, int length)
    {
    	const uint M = 0x5bd1e995;
    	const int R = 24;
    
    	uint seed = (uint)(0xdeadbeef * length);
    	uint hash = (uint)(seed ^ length);
    
    	int count = length >> 2;
    
    	fixed (byte* start = &(data[offset]))
    	{
    		uint* ptrUInt = (uint*)start;
    
    		while (count > 0)
    		{
    			uint current = *ptrUInt;
    
    			current = (uint)(current * M);
    			current ^= current >> R;
    			current = (uint)(current * M);
    			hash = (uint)(hash * M);
    			hash ^= current;
    
    			count--;
    			ptrUInt++;
    		}
    
    		switch (length & 3)
    		{
    			case 3:
    				// reverse the last 3 bytes and convert it to an uint
    				// so cast the last to into an UInt16 and get the 3rd as a byte
    				// ABC --> CBA; (UInt16)(AB) --> BA
    				//h ^= (uint)(*ptrByte);
    				//h ^= (uint)(ptrByte[1] << 8);
    				hash ^= (*(UInt16*)ptrUInt);
    				hash ^= (uint)(((byte*)ptrUInt)[2] << 16);
    				hash *= M;
    				break;
    
    			case 2:
    				hash ^= (*(UInt16*)ptrUInt);
    				hash *= M;
    				break;
    
    			case 1:
    				hash ^= (*((byte*)ptrUInt));
    				hash *= M;
    				break;
    		}
    	}
    
    	hash ^= hash >> 13;
    	hash *= M;
    	hash ^= hash >> 15;
    
    	return hash;
    }
    • Public Snippets
    • Channels Snippets