wabbajack/Wabbajack.VFS/FallthroughVFSCache.cs

56 lines
1.3 KiB
C#
Raw Normal View History

2022-06-22 01:38:42 +00:00
using System.Threading;
using System.Threading.Tasks;
2022-07-11 20:55:54 +00:00
using Wabbajack.DTOs.Streams;
2022-06-22 01:38:42 +00:00
using Wabbajack.DTOs.Vfs;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.VFS.Interfaces;
namespace Wabbajack.VFS;
public class FallthroughVFSCache : IVfsCache
{
private readonly IVfsCache[] _caches;
public FallthroughVFSCache(IVfsCache[] caches)
{
_caches = caches;
}
2022-07-11 20:55:54 +00:00
public async Task<IndexedVirtualFile?> Get(Hash hash, IStreamFactory sf, CancellationToken token)
2022-06-22 01:38:42 +00:00
{
2022-06-29 13:18:04 +00:00
IndexedVirtualFile? result = null;
2022-06-22 01:38:42 +00:00
foreach (var cache in _caches)
{
2022-06-29 13:18:04 +00:00
if (result == null)
{
2022-07-11 20:55:54 +00:00
result = await cache.Get(hash, sf, token);
if (result == null) continue;
foreach (var upperCache in _caches)
{
if (upperCache != cache)
await upperCache.Put(result, token);
}
return result;
}
2022-06-22 01:38:42 +00:00
}
2022-06-29 13:18:04 +00:00
return result;
2022-06-22 01:38:42 +00:00
}
public async Task Put(IndexedVirtualFile file, CancellationToken token)
{
foreach (var cache in _caches)
{
await cache.Put(file, token);
}
}
2022-08-19 23:59:29 +00:00
public async Task Clean()
{
foreach (var cache in _caches)
{
await cache.Clean();
}
}
2022-06-22 01:38:42 +00:00
}