-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathLibraryTypeCache.cs
53 lines (44 loc) · 1.42 KB
/
LibraryTypeCache.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
using System;
using System.Collections.Concurrent;
namespace Rubberduck.Parsing.ComReflection.TypeLibReflection
{
internal interface ILibraryTypeCache
{
string Key { get; }
bool TryGetType(string progId, out Type type);
bool AddType(string progId, Type type);
Type GetOrAdd(string progId, Type type);
bool Remove(string progId);
}
internal sealed class LibraryTypeCache : ILibraryTypeCache
{
private readonly ConcurrentDictionary<string, Type> _cache;
public LibraryTypeCache(string key)
{
Key = key;
_cache = new ConcurrentDictionary<string, Type>();
}
public string Key { get; }
public bool TryGetType(string progId, out Type type)
{
return _cache.TryGetValue(progId.ToLowerInvariant(), out type);
}
public bool AddType(string progId, Type type)
{
if (_cache.ContainsKey(progId.ToLowerInvariant()))
{
return false;
}
_cache.AddOrUpdate(progId.ToLowerInvariant(), p => type, (p, t) => type);
return true;
}
public Type GetOrAdd(string progId, Type type)
{
return _cache.GetOrAdd(progId.ToLowerInvariant(), s => type);
}
public bool Remove(string progId)
{
return _cache.TryRemove(progId, out _);
}
}
}