Skip to content

Commit 988a07b

Browse files
committed
增加ConcurrentCache类型
1 parent 57e8624 commit 988a07b

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Concurrent;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace WebApiClient
11+
{
12+
/// <summary>
13+
/// 表示线程安全的内存缓存
14+
/// </summary>
15+
/// <typeparam name="TKey">键</typeparam>
16+
/// <typeparam name="TValue">值</typeparam>
17+
public class ConcurrentCache<TKey, TValue>
18+
{
19+
/// <summary>
20+
/// 线程安全字典
21+
/// </summary>
22+
private readonly ConcurrentDictionary<TKey, Lazy<TValue>> dictionary;
23+
24+
/// <summary>
25+
/// 线程安全的内存缓存
26+
/// </summary>
27+
public ConcurrentCache()
28+
{
29+
this.dictionary = new ConcurrentDictionary<TKey, Lazy<TValue>>();
30+
}
31+
32+
/// <summary>
33+
/// 线程安全的内存缓存
34+
/// </summary>
35+
/// <param name="comparer">键的比较器</param>
36+
public ConcurrentCache(IEqualityComparer<TKey> comparer)
37+
{
38+
this.dictionary = new ConcurrentDictionary<TKey, Lazy<TValue>>(comparer);
39+
}
40+
41+
/// <summary>
42+
/// 获取或添加缓存
43+
/// </summary>
44+
/// <param name="key">键</param>
45+
/// <param name="valueFactory">生成缓存内容的委托</param>
46+
/// <returns></returns>
47+
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
48+
{
49+
return this.dictionary
50+
.GetOrAdd(key, k => new Lazy<TValue>(() => valueFactory(k), LazyThreadSafetyMode.ExecutionAndPublication))
51+
.Value;
52+
}
53+
}
54+
}

WebApiClient/Internal/HttpApiClientProxy.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static class HttpApiClientProxy
3737
/// <summary>
3838
/// 接口类型与代理类型的构造器缓存
3939
/// </summary>
40-
private static readonly ConcurrentDictionary<Type, Lazy<ConstructorInfo>> proxyTypeCtorCache = new ConcurrentDictionary<Type, Lazy<ConstructorInfo>>();
40+
private static readonly ConcurrentCache<Type, ConstructorInfo> proxyTypeCtorCache = new ConcurrentCache<Type, ConstructorInfo>();
4141

4242
/// <summary>
4343
/// 创建HttpApiClient代理类
@@ -53,9 +53,9 @@ public static object CreateProxyWithInterface(Type interfaceType, IApiIntercepto
5353
var apiMethods = interfaceType.GetAllApiMethods();
5454
var proxyTypeCtor = proxyTypeCtorCache.GetOrAdd(
5555
interfaceType,
56-
@interface => new Lazy<ConstructorInfo>(() => @interface.ImplementAsHttpApiClient(apiMethods)));
56+
@interface => @interface.ImplementAsHttpApiClient(apiMethods));
5757

58-
return proxyTypeCtor.Value.Invoke(new object[] { interceptor, apiMethods });
58+
return proxyTypeCtor.Invoke(new object[] { interceptor, apiMethods });
5959
}
6060

6161
/// <summary>

WebApiClient/Internal/TypeExtend.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ static class TypeExtend
1616
/// <summary>
1717
/// 接口的方法缓存
1818
/// </summary>
19-
private static readonly ConcurrentDictionary<Type, MethodInfo[]> interfaceMethodsCache = new ConcurrentDictionary<Type, MethodInfo[]>();
19+
private static readonly ConcurrentCache<Type, MethodInfo[]> interfaceMethodsCache = new ConcurrentCache<Type, MethodInfo[]>();
2020

2121
/// <summary>
2222
/// 类型是否AllowMultiple的缓存
2323
/// </summary>
2424
private static readonly ConcurrentDictionary<Type, bool> typeAllowMultipleCache = new ConcurrentDictionary<Type, bool>();
2525

26-
2726
/// <summary>
2827
/// 获取接口类型及其继承的接口的所有方法
2928
/// 忽略HttpApiClient类型的所有接口的方法

0 commit comments

Comments
 (0)