-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGenericPool.cs
More file actions
42 lines (39 loc) · 964 Bytes
/
GenericPool.cs
File metadata and controls
42 lines (39 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Author: Rick
* Create: 2017/6/30 16:12:13
* Email: rickjiangshu@gmail.com
* Follow: https://github.com/RickJiangShu
*/
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 最高效的对象池
/// </summary>
public class GenericPool<T> where T : class
{
private Dictionary<object, List<T>> cache = new Dictionary<object, List<T>>();
public void Add(object key, T value)
{
List<T> list;
if (cache.TryGetValue(key, out list))
{
list.Add(value);
}
else
{
list = new List<T>() { value };
cache.Add(key, list);
}
}
public T Get(object key)
{
List<T> list;
if (cache.TryGetValue(key, out list) && list.Count > 0)
{
T value = list[0];
list.RemoveAt(0);
return value;
}
return null;
}
}