Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ packages/*/
project.lock.json
/publish/*.bat
.vs/
.idea
2 changes: 1 addition & 1 deletion Enyim.Caching/Enyim.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>EnyimMemcachedCore is a Memcached client library for .NET Core. Usage: Add services.AddEnyimMemcached(...) and app.UseEnyimMemcached() in Startup. Add IMemcachedClient into constructor.</Description>
<VersionPrefix>2.2.0-beta1</VersionPrefix>
<VersionPrefix>2.1.14</VersionPrefix>
<Authors>cnblogs.com</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
80 changes: 43 additions & 37 deletions Enyim.Caching/FastActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,54 @@

namespace Enyim.Reflection
{
/// <summary>
/// <para>Implements a very fast object factory for dynamic object creation. Dynamically generates a factory class which will use the new() constructor of the requested type.</para>
/// <para>Much faster than using Activator at the price of the first invocation being significantly slower than subsequent calls.</para>
/// </summary>
public static class FastActivator
{
private static Dictionary<Type, Func<object>> factoryCache = new Dictionary<Type, Func<object>>();
/// <summary>
/// <para>Implements a very fast object factory for dynamic object creation. Dynamically generates a factory class which will use the new() constructor of the requested type.</para>
/// <para>Much faster than using Activator at the price of the first invocation being significantly slower than subsequent calls.</para>
/// </summary>
public static class FastActivator
{
private static Dictionary<Type, Func<object>> factoryCache = new Dictionary<Type, Func<object>>();

/// <summary>
/// Creates an instance of the specified type using a generated factory to avoid using Reflection.
/// </summary>
/// <typeparam name="T">The type to be created.</typeparam>
/// <returns>The newly created instance.</returns>
public static T Create<T>()
{
return TypeFactory<T>.Create();
}
/// <summary>
/// Creates an instance of the specified type using a generated factory to avoid using Reflection.
/// </summary>
/// <typeparam name="T">The type to be created.</typeparam>
/// <returns>The newly created instance.</returns>
public static T Create<T>()
{
return TypeFactory<T>.Create();
}

/// <summary>
/// Creates an instance of the specified type using a generated factory to avoid using Reflection.
/// </summary>
/// <param name="type">The type to be created.</param>
/// <returns>The newly created instance.</returns>
public static object Create(Type type)
{
Func<object> f;
/// <summary>
/// Creates an instance of the specified type using a generated factory to avoid using Reflection.
/// </summary>
/// <param name="type">The type to be created.</param>
/// <returns>The newly created instance.</returns>
public static object Create(Type type)
{
Func<object> f;

if (!factoryCache.TryGetValue(type, out f))
lock (factoryCache)
if (!factoryCache.TryGetValue(type, out f))
{
factoryCache[type] = f = Expression.Lambda<Func<object>>(Expression.New(type)).Compile();
}
if (!factoryCache.TryGetValue(type, out f))
{
lock (factoryCache)
if (!factoryCache.TryGetValue(type, out f))
{
f = Expression.Lambda<Func<object>>(Expression.New(type)).Compile();
if (f != null)
{
factoryCache[type] = f;
}
}
}

return f();
}
return f();
}

private static class TypeFactory<T>
{
public static readonly Func<T> Create = Expression.Lambda<Func<T>>(Expression.New(typeof(T))).Compile();
}
}
private static class TypeFactory<T>
{
public static readonly Func<T> Create = Expression.Lambda<Func<T>>(Expression.New(typeof(T))).Compile();
}
}
}

#region [ License information ]
Expand Down
6 changes: 3 additions & 3 deletions Enyim.Caching/Memcached/AsyncSocketHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ private void BeginReceive()
{
this.readInProgressEvent.Reset();

if (this.socket.socket.ReceiveAsync(this.readEvent))
if (this.socket._socket.ReceiveAsync(this.readEvent))
{
// wait until the timeout elapses, then abort this reading process
// EndREceive will be triggered sooner or later but its timeout
// may be higher than our read timeout, so it's not reliable
if (!readInProgressEvent.WaitOne(this.socket.socket.ReceiveTimeout))
if (!readInProgressEvent.WaitOne(this.socket._socket.ReceiveTimeout))
this.AbortReadAndTryPublishError(false);

return;
Expand All @@ -293,7 +293,7 @@ void AsyncReadCompleted(object sender, SocketAsyncEventArgs e)
private void AbortReadAndTryPublishError(bool markAsDead)
{
if (markAsDead)
this.socket.isAlive = false;
this.socket._isAlive = false;

// we've been already aborted, so quit
// both the EndReceive and the wait on the event can abort the read
Expand Down
Loading