-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathBuildUpToDateCheck.TimestampCache.cs
More file actions
53 lines (45 loc) · 1.93 KB
/
BuildUpToDateCheck.TimestampCache.cs
File metadata and controls
53 lines (45 loc) · 1.93 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
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
using Microsoft.VisualStudio.IO;
namespace Microsoft.VisualStudio.ProjectSystem.UpToDate
{
internal sealed partial class BuildUpToDateCheck
{
internal readonly struct TimestampCache : ITimestampCache
{
private readonly Dictionary<string, DateTime> _timestampCache = new(StringComparers.Paths);
private readonly IFileSystem _fileSystem;
public TimestampCache(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
/// <summary>
/// Gets the number of unique files added to this cache.
/// </summary>
public int Count => _timestampCache.Count;
/// <summary>
/// Attempts to get the last write time of the specified file.
/// If the value already exists in this cache, return the cached value.
/// Otherwise, query the filesystem, cache the result, then return it.
/// If the file is not found, return <see langword="null"/>.
/// </summary>
public DateTime? GetTimestampUtc(string path)
{
if (!_timestampCache.TryGetValue(path, out DateTime time))
{
if (!_fileSystem.TryGetLastFileWriteTimeUtc(path, out DateTime? newTime))
{
return null;
}
time = newTime.Value;
_timestampCache[path] = time;
}
return time;
}
public void ClearTimestamps(IEnumerable<string> paths)
{
// This should never be called.
throw new NotImplementedException();
}
}
}
}