-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathHttpSourceCacheContext.cs
More file actions
78 lines (68 loc) · 2.67 KB
/
HttpSourceCacheContext.cs
File metadata and controls
78 lines (68 loc) · 2.67 KB
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.Diagnostics;
namespace NuGet.Protocol.Core.Types
{
public class HttpSourceCacheContext
{
private HttpSourceCacheContext(string? rootTempFolder, TimeSpan maxAge, bool directDownload, SourceCacheContext cacheContext)
{
if (maxAge <= TimeSpan.Zero)
{
if (rootTempFolder == null)
{
throw new ArgumentNullException(nameof(rootTempFolder));
}
}
else
{
Debug.Assert(
rootTempFolder == null,
$"{nameof(rootTempFolder)} should be null when {nameof(maxAge)} is not zero.");
}
RootTempFolder = rootTempFolder;
MaxAge = maxAge;
DirectDownload = directDownload;
SourceCacheContext = cacheContext ?? throw new ArgumentNullException(nameof(cacheContext));
}
public TimeSpan MaxAge { get; }
public bool DirectDownload { get; }
/// <summary>
/// A suggested root folder to drop temporary files under, it will get cleared by the
/// disposal of the <see cref="SourceCacheContext"/> that was used to create this instance.
/// </summary>
public string? RootTempFolder { get; }
/// <summary>
/// Inner cache context.
/// </summary>
public SourceCacheContext SourceCacheContext { get; }
public static HttpSourceCacheContext Create(SourceCacheContext cacheContext, int retryCount)
{
return Create(cacheContext, retryCount == 0);
}
public static HttpSourceCacheContext Create(SourceCacheContext cacheContext, bool isFirstAttempt)
{
if (cacheContext == null)
{
throw new ArgumentNullException(nameof(cacheContext));
}
if (isFirstAttempt && cacheContext.MaxAgeTimeSpan > TimeSpan.Zero)
{
return new HttpSourceCacheContext(
rootTempFolder: null,
maxAge: cacheContext.MaxAgeTimeSpan,
directDownload: cacheContext.DirectDownload,
cacheContext: cacheContext);
}
else
{
return new HttpSourceCacheContext(
cacheContext.GeneratedTempFolder,
TimeSpan.Zero,
cacheContext.DirectDownload,
cacheContext: cacheContext);
}
}
}
}