forked from hyperledger/fabric-sdk-go
/
cache.go
79 lines (64 loc) · 1.76 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
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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chconfig
import (
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache"
"github.com/pkg/errors"
)
// Provider provides ChannelConfig
type Provider func(channelID string) (fab.ChannelConfig, error)
// CacheKey channel config reference cache key
type CacheKey interface {
lazycache.Key
Context() fab.ClientContext
ChannelID() string
Provider() Provider
}
// CacheKey holds a key for the provider cache
type cacheKey struct {
key string
channelID string
context fab.ClientContext
pvdr Provider
}
// NewCacheKey returns a new CacheKey
func NewCacheKey(ctx fab.ClientContext, pvdr Provider, channelID string) (CacheKey, error) {
return &cacheKey{
key: channelID,
channelID: channelID,
context: ctx,
pvdr: pvdr,
}, nil
}
// NewRefCache a cache of channel config references that refreshed with the
// given interval
func NewRefCache(refresh time.Duration) *lazycache.Cache {
initializer := func(key lazycache.Key) (interface{}, error) {
ck, ok := key.(CacheKey)
if !ok {
return nil, errors.New("unexpected cache key")
}
return NewRef(refresh, ck.Provider(), ck.ChannelID(), ck.Context()), nil
}
return lazycache.New("Channel_Cfg_Cache", initializer)
}
// String returns the key as a string
func (k *cacheKey) String() string {
return k.key
}
// Context returns the Context
func (k *cacheKey) Context() fab.ClientContext {
return k.context
}
// ChannelID returns the channelID
func (k *cacheKey) ChannelID() string {
return k.channelID
}
// Provider channel configuration provider
func (k *cacheKey) Provider() Provider {
return k.pvdr
}