-
Notifications
You must be signed in to change notification settings - Fork 417
/
CachingScriptMetadataResolver.cs
60 lines (50 loc) · 2.29 KB
/
CachingScriptMetadataResolver.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
56
57
58
59
60
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Scripting;
namespace OmniSharp.Cake
{
internal class CachingScriptMetadataResolver : MetadataReferenceResolver
{
private static readonly ConcurrentDictionary<string, ImmutableArray<PortableExecutableReference>> DirectReferenceCache = new ConcurrentDictionary<string, ImmutableArray<PortableExecutableReference>>();
private static readonly ConcurrentDictionary<string, PortableExecutableReference> MissingReferenceCache = new ConcurrentDictionary<string, PortableExecutableReference>();
private static readonly MetadataReferenceResolver DefaultRuntimeResolver = ScriptMetadataResolver.Default;
public override bool Equals(object other)
{
return DefaultRuntimeResolver.Equals(other);
}
public override int GetHashCode()
{
return DefaultRuntimeResolver.GetHashCode();
}
public override bool ResolveMissingAssemblies => DefaultRuntimeResolver.ResolveMissingAssemblies;
public override PortableExecutableReference ResolveMissingAssembly(MetadataReference definition, AssemblyIdentity referenceIdentity)
{
if (MissingReferenceCache.TryGetValue(referenceIdentity.Name, out var result))
{
return result;
}
result = DefaultRuntimeResolver.ResolveMissingAssembly(definition, referenceIdentity);
if (result != null)
{
MissingReferenceCache[referenceIdentity.Name] = result;
}
return result;
}
public override ImmutableArray<PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
{
var key = $"{reference}-{baseFilePath}";
if (DirectReferenceCache.TryGetValue(key, out var result))
{
return result;
}
result = DefaultRuntimeResolver.ResolveReference(reference, baseFilePath, properties);
if (result.Length > 0)
{
DirectReferenceCache[key] = result;
}
return result;
}
}
}