forked from hyperledger/fabric-sdk-go
/
cache.go
80 lines (65 loc) · 1.77 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
80
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package membership
import (
"crypto/sha256"
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache"
"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazyref"
"github.com/pkg/errors"
)
// CacheKey membership reference cache key
type CacheKey interface {
lazycache.Key
Context() Context
ChannelID() string
ChConfigRef() *lazyref.Reference
}
// CacheKey holds a key for the cache
type cacheKey struct {
key string
context Context
channelID string
chConfigRef *lazyref.Reference
}
// NewCacheKey returns a new CacheKey
func NewCacheKey(context Context, chConfigRef *lazyref.Reference, channelID string) (CacheKey, error) {
h := sha256.New()
hash := h.Sum([]byte(channelID))
return &cacheKey{
key: string(hash),
context: context,
chConfigRef: chConfigRef,
channelID: channelID,
}, nil
}
// NewRefCache a cache of membership 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.Context(), ck.ChConfigRef()), nil
}
return lazycache.New("Membership_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() Context {
return k.context
}
// ChannelID returns the channelID
func (k *cacheKey) ChannelID() string {
return k.channelID
}
// ChConfigRef returns the channel config reference
func (k *cacheKey) ChConfigRef() *lazyref.Reference {
return k.chConfigRef
}