-
Notifications
You must be signed in to change notification settings - Fork 39
/
cache.go
48 lines (37 loc) · 1.05 KB
/
cache.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
// (c) 2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package api
import (
"context"
"fmt"
"strconv"
"time"
"github.com/ava-labs/ortelius/services/cache"
)
// CacheableFn is a function whose output can safely be cached
type CacheableFn func(context.Context) (interface{}, error)
// Cacheable is a keyed CacheableFn
type Cacheable struct {
Key []string
CacheableFn CacheableFn
TTL time.Duration
}
type cacher interface {
Get(context.Context, string) ([]byte, error)
Set(context.Context, string, []byte, time.Duration) error
}
func cacheKey(networkID uint32, parts ...string) string {
k := make([]string, 1, len(parts)+1)
k[0] = strconv.Itoa(int(networkID))
return cache.KeyFromParts(append(k, parts...)...)
}
type nullCache struct{}
func NewNullCache() cache.Cache {
return &nullCache{}
}
func (nullCache) Get(_ context.Context, _ string) ([]byte, error) {
return nil, fmt.Errorf("invalid")
}
func (nullCache) Set(_ context.Context, _ string, _ []byte, _ time.Duration) error {
return nil
}