-
Notifications
You must be signed in to change notification settings - Fork 696
/
Copy pathCachingSourceProvider.cs
78 lines (66 loc) · 2.73 KB
/
CachingSourceProvider.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using NuGet.Configuration;
using NuGet.Protocol.Core.Types;
namespace NuGet.Protocol
{
/// <summary>
/// A caching source repository provider intended to be used as a singleton.
/// </summary>
public class CachingSourceProvider : ISourceRepositoryProvider
{
private readonly IPackageSourceProvider _packageSourceProvider;
private readonly List<Lazy<INuGetResourceProvider>> _resourceProviders
= new List<Lazy<INuGetResourceProvider>>();
private readonly List<SourceRepository> _repositories = new List<SourceRepository>();
// There should only be one instance of the source repository for each package source.
private readonly ConcurrentDictionary<string, SourceRepository> _cachedSources
= new ConcurrentDictionary<string, SourceRepository>(StringComparer.Ordinal);
public CachingSourceProvider(IPackageSourceProvider packageSourceProvider)
{
_packageSourceProvider = packageSourceProvider;
_resourceProviders.AddRange(Repository.Provider.GetCoreV3());
_repositories = _packageSourceProvider.LoadPackageSources()
.Where(s => s.IsEnabled)
.Select(CreateRepository)
.ToList();
}
/// <summary>
/// Retrieve repositories that have been cached.
/// </summary>
public IEnumerable<SourceRepository> GetRepositories()
{
return _repositories;
}
/// <summary>
/// Create a repository and add it to the cache.
/// </summary>
public SourceRepository CreateRepository(string source)
{
return CreateRepository(new PackageSource(source));
}
/// <summary>
/// Create a repository and add it to the cache.
/// </summary>
public SourceRepository CreateRepository(PackageSource source)
{
return CreateRepository(source, FeedType.Undefined);
}
public SourceRepository CreateRepository(PackageSource source, FeedType type)
{
return _cachedSources.GetOrAdd(source.Source, new SourceRepository(source, _resourceProviders, type));
}
public void AddSourceRepository(SourceRepository source)
{
_cachedSources.TryAdd(source.PackageSource.Source, source);
}
public IPackageSourceProvider PackageSourceProvider
{
get { return _packageSourceProvider; }
}
}
}