-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache the processing of EditorConfigFile. (#23)
Co-authored-by: Martijn Laarman <[email protected]>
- Loading branch information
Showing
12 changed files
with
134 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.IO; | ||
|
||
namespace EditorConfig.Core; | ||
|
||
/// <summary> | ||
/// Cache unchanged parsed EditorConfigFiles. | ||
/// </summary> | ||
public static class EditorConfigFileCache | ||
{ | ||
private static string GetFileHash(string filename) | ||
{ | ||
using var sha256 = System.Security.Cryptography.SHA256.Create(); | ||
using var stream = File.OpenRead(filename); | ||
var hash = sha256.ComputeHash(stream); | ||
return BitConverter.ToString(hash).Replace("-", ""); | ||
} | ||
|
||
private static readonly ConcurrentDictionary<string, EditorConfigFile> FileCache = new(); | ||
|
||
/// <summary> | ||
/// Retrieves a cached EditorConfigFile based on the file name and file hash. | ||
/// The cache will be populated when the file was not present. | ||
/// </summary> | ||
/// <remarks>This function is thread safe. The cache will not be hit when the file does not exist.</remarks> | ||
/// <param name="file"></param> | ||
/// <returns></returns> | ||
public static EditorConfigFile GetOrCreate(string file) | ||
{ | ||
if (!File.Exists(file)) return new EditorConfigFile(file); | ||
|
||
var key = $"{file}_{GetFileHash(file)}"; | ||
return FileCache.GetOrAdd(key, _ => new EditorConfigFile(file)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Reflection; | ||
using EditorConfig.Core; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace EditorConfig.Tests.Caching | ||
{ | ||
[TestFixture] | ||
public class CachingTests : EditorConfigTestBase | ||
{ | ||
[Test] | ||
public void FileShouldCached() | ||
{ | ||
var fileName = GetFileFromMethod(MethodBase.GetCurrentMethod(), ".editorconfig"); | ||
|
||
var parser = new EditorConfigParser(EditorConfigFileCache.GetOrCreate); | ||
var config1 = parser.Parse(fileName); | ||
config1.EditorConfigFiles.Should().NotBeNullOrEmpty(); | ||
config1.EditorConfigFiles.Should().OnlyContain(f => !string.IsNullOrEmpty(f.CacheKey)); | ||
var config2 = parser.Parse(fileName); | ||
config2.EditorConfigFiles.Should().NotBeNullOrEmpty(); | ||
config2.EditorConfigFiles.Should().OnlyContain(f => !string.IsNullOrEmpty(f.CacheKey)); | ||
} | ||
} | ||
} |