-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
70 lines (62 loc) · 1.39 KB
/
store.go
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
package opplasmairys
import (
"context"
"io"
"log/slog"
)
var _ KVStore = (*DAStore)(nil)
type KVStore interface {
// Get retrieves the given key if it's present in the key-value data store.
Get(ctx context.Context, key []byte) ([]byte, error)
// Put inserts the given value into the key-value data store.
Put(ctx context.Context, key []byte, value []byte) error
}
type DAStore struct {
client *IrysClient
cache KVStore
}
func NewDAStore(c Config, store KVStore) (*DAStore, error) {
client, err := NewIrysClient(c.NetworkName, c.NetWorkRpc, c.PrivateKey, c.FreeUpload)
if err != nil {
return nil, err
}
return &DAStore{
client: client,
cache: store,
}, nil
}
func (d *DAStore) Get(ctx context.Context, key []byte) (v []byte, err error) {
if d.cache != nil {
v, err = d.cache.Get(ctx, key)
if err != nil {
return nil, err
}
}
if v != nil {
return v, nil
}
r, err := d.client.Download(ctx, key)
if err != nil {
return nil, err
}
defer r.Close()
v, err = io.ReadAll(r)
if err != nil {
return nil, err
}
if d.cache != nil {
err1 := d.cache.Put(ctx, key, v)
if err1 != nil {
slog.Warn("failed to cache value, ", "err", err1)
}
}
return v, nil
}
func (d *DAStore) Put(ctx context.Context, key []byte, value []byte) error {
if d.cache != nil {
if err := d.cache.Put(ctx, key, value); err != nil {
return err
}
}
return d.client.Upload(ctx, key, value)
}