forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcurrentDictionaryPool{TKey,TValue}.cs
55 lines (44 loc) · 1.41 KB
/
ConcurrentDictionaryPool{TKey,TValue}.cs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace System.Collections.Pooling.Concurrent
{
public static class ConcurrentDictionaryPool<TKey, TValue>
{
private static readonly ConcurrentPool<ConcurrentDictionary<TKey, TValue>> _pool = new ConcurrentPool<ConcurrentDictionary<TKey, TValue>>();
public static ConcurrentDictionary<TKey, TValue> Get()
=> _pool.Get();
public static void Return(ConcurrentDictionary<TKey, TValue> item)
{
if (item == null)
return;
item.Clear();
_pool.Return(item);
}
public static void Return(params ConcurrentDictionary<TKey, TValue>[] items)
{
if (items == null)
return;
foreach (var item in items)
{
if (item == null)
continue;
item.Clear();
_pool.Return(item);
}
}
public static void Return(IEnumerable<ConcurrentDictionary<TKey, TValue>> items)
{
if (items == null)
return;
foreach (var item in items)
{
if (item == null)
continue;
item.Clear();
_pool.Return(item);
}
}
public static void Clear()
=> _pool.Clear();
}
}