forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileListCacheController.cs
61 lines (53 loc) · 1.93 KB
/
FileListCacheController.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
using Microsoft.Extensions.Caching.Memory;
using System.Threading;
using System.Threading.Tasks;
namespace Files.Helpers.FileListCache
{
internal class FileListCacheController : IFileListCache
{
private static FileListCacheController instance;
public static FileListCacheController GetInstance()
{
return instance ??= new FileListCacheController();
}
private readonly IFileListCache persistentAdapter;
private FileListCacheController()
{
persistentAdapter = new PersistentSQLiteCacheAdapter();
}
private readonly IMemoryCache fileNamesCache = new MemoryCache(new MemoryCacheOptions
{
SizeLimit = 1_000_000
});
public async Task<string> ReadFileDisplayNameFromCache(string path, CancellationToken cancellationToken)
{
var displayName = fileNamesCache.Get<string>(path);
if (displayName == null)
{
displayName = await persistentAdapter.ReadFileDisplayNameFromCache(path, cancellationToken);
if (displayName != null)
{
fileNamesCache.Set(path, displayName, new MemoryCacheEntryOptions
{
Size = 1
});
}
}
return displayName;
}
public Task SaveFileDisplayNameToCache(string path, string displayName)
{
if (displayName == null)
{
fileNamesCache.Remove(path);
return persistentAdapter.SaveFileDisplayNameToCache(path, displayName);
}
fileNamesCache.Set(path, displayName, new MemoryCacheEntryOptions
{
Size = 1
});
// save entry to persistent cache in background
return persistentAdapter.SaveFileDisplayNameToCache(path, displayName);
}
}
}