-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCachedCoreApi.cs
114 lines (87 loc) · 2.99 KB
/
CachedCoreApi.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Ipfs.CoreApi;
using OwlCore.ComponentModel;
using OwlCore.Storage;
namespace OwlCore.Kubo.Cache;
/// <summary>
/// A cached api layer for <see cref="ICoreApi"/>.
/// </summary>
public class CachedCoreApi : ICoreApi, IDelegable<ICoreApi>, IFlushable, IAsyncInit
{
/// <summary>
/// Creates a new instance of <see cref="CachedCoreApi"/>.
/// </summary>
/// <param name="cacheFolder">The folder to store cached data in.</param>
/// <param name="inner">The inner <see cref="ICoreApi"/> to wrap around.</param>
public CachedCoreApi(IModifiableFolder cacheFolder, ICoreApi inner)
{
Name = new CachedNameApi(cacheFolder) { Inner = inner.Name, KeyApi = inner.Key };
Key = new CachedKeyApi(cacheFolder) { Inner = inner.Key };
Inner = inner;
}
/// <summary>
/// The inner, unwrapped core api to delegate to.
/// </summary>
public ICoreApi Inner { get; }
/// <inheritdoc/>
public IBitswapApi Bitswap => Inner.Bitswap;
/// <inheritdoc/>
public IBlockApi Block => Inner.Block;
/// <inheritdoc/>
public IBlockRepositoryApi BlockRepository => Inner.BlockRepository;
/// <inheritdoc/>
public IBootstrapApi Bootstrap => Inner.Bootstrap;
/// <inheritdoc/>
public IConfigApi Config => Inner.Config;
/// <inheritdoc/>
public IDagApi Dag => Inner.Dag;
/// <inheritdoc/>
public IDhtApi Dht => Inner.Dht;
/// <inheritdoc/>
public IDnsApi Dns => Inner.Dns;
/// <inheritdoc/>
public IFileSystemApi FileSystem => Inner.FileSystem;
/// <inheritdoc/>
public IMfsApi Mfs => Inner.Mfs;
/// <inheritdoc/>
public IGenericApi Generic => Inner.Generic;
/// <inheritdoc/>
public IKeyApi Key { get; }
/// <inheritdoc/>
public INameApi Name { get; }
/// <inheritdoc/>
public IObjectApi Object => Inner.Object;
/// <inheritdoc/>
public IPinApi Pin => Inner.Pin;
/// <inheritdoc/>
public IPubSubApi PubSub => Inner.PubSub;
/// <inheritdoc/>
public IStatsApi Stats => Inner.Stats;
/// <inheritdoc/>
public ISwarmApi Swarm => Inner.Swarm;
/// <inheritdoc/>
public bool IsInitialized { get; set; }
/// <inheritdoc/>
public async Task FlushAsync(CancellationToken cancellationToken)
{
await ((CachedNameApi)Name).FlushAsync(cancellationToken);
await ((CachedNameApi)Name).SaveAsync(cancellationToken);
await ((CachedKeyApi)Key).SaveAsync(cancellationToken);
}
/// <inheritdoc/>
public async Task InitAsync(CancellationToken cancellationToken = default)
{
try
{
// Try loading data from API
await ((CachedKeyApi)Key).InitAsync(cancellationToken);
}
catch
{
// Load data from disk as fallback
await ((CachedKeyApi)Key).LoadAsync(cancellationToken);
}
await ((CachedNameApi)Name).LoadAsync(cancellationToken);
// Allow multiple initialization
IsInitialized = true;
}
}