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
79
80
81
82
83
84
85
86
87
88
89
|
using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Controller.Providers
{
public class DirectoryService : IDirectoryService
{
private readonly IFileSystem _fileSystem;
private readonly Dictionary<string, FileSystemMetadata[]> _cache = new Dictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, FileSystemMetadata> _fileCache = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, List<string>> _filePathCache = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
public DirectoryService(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
public FileSystemMetadata[] GetFileSystemEntries(string path)
{
if (!_cache.TryGetValue(path, out FileSystemMetadata[] entries))
{
entries = _fileSystem.GetFileSystemEntries(path).ToArray();
//_cache.TryAdd(path, entries);
_cache[path] = entries;
}
return entries;
}
public List<FileSystemMetadata> GetFiles(string path)
{
var list = new List<FileSystemMetadata>();
var items = GetFileSystemEntries(path);
foreach (var item in items)
{
if (!item.IsDirectory)
{
list.Add(item);
}
}
return list;
}
public FileSystemMetadata GetFile(string path)
{
if (!_fileCache.TryGetValue(path, out FileSystemMetadata file))
{
file = _fileSystem.GetFileInfo(path);
if (file != null && file.Exists)
{
//_fileCache.TryAdd(path, file);
_fileCache[path] = file;
}
else
{
return null;
}
}
return file;
//return _fileSystem.GetFileInfo(path);
}
public List<string> GetFilePaths(string path)
{
return GetFilePaths(path, false);
}
public List<string> GetFilePaths(string path, bool clearCache)
{
if (clearCache || !_filePathCache.TryGetValue(path, out List<string> result))
{
result = _fileSystem.GetFilePaths(path).ToList();
_filePathCache[path] = result;
}
return result;
}
}
}
|