/
tx_signature_cache.go
99 lines (84 loc) · 1.59 KB
/
tx_signature_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package types
import (
"github.com/VictoriaMetrics/fastcache"
"sync/atomic"
"github.com/spf13/viper"
"github.com/tendermint/go-amino"
)
var (
signatureCache *Cache
)
const (
TxHashLen = 32
AddressStringLen = 2 + 20*2
)
const FlagSigCacheSize = "signature-cache-size"
func init() {
// used for ut
defaultCache := &Cache{
data: nil,
readCount: 0,
hitCount: 0,
}
signatureCache = defaultCache
}
func InitSignatureCache() {
fastCache := fastcache.New((TxHashLen + AddressStringLen) * viper.GetInt(FlagSigCacheSize))
signatureCache = &Cache{
data: fastCache,
}
}
func SignatureCache() *Cache {
return signatureCache
}
type Cache struct {
data *fastcache.Cache
readCount int64
hitCount int64
}
func (c *Cache) Get(key []byte) (string, bool) {
// validate
if !c.validate(key) {
return "", false
}
atomic.AddInt64(&c.readCount, 1)
// get cache
value, ok := c.data.HasGet(nil, key)
if ok {
atomic.AddInt64(&c.hitCount, 1)
return amino.BytesToStr(value), true
}
return "", false
}
func (c *Cache) Add(key []byte, value string) {
// validate
if !c.validate(key) {
return
}
// add cache
c.data.Set(key, amino.StrToBytes(value))
}
func (c *Cache) Remove(key []byte) {
// validate
if !c.validate(key) {
return
}
c.data.Del(key)
}
func (c *Cache) ReadCount() int64 {
return atomic.LoadInt64(&c.readCount)
}
func (c *Cache) HitCount() int64 {
return atomic.LoadInt64(&c.hitCount)
}
func (c *Cache) validate(key []byte) bool {
// validate key
if len(key) == 0 {
return false
}
// validate lru cache
if c.data == nil {
return false
}
return true
}