Skip to content

Commit d18034a

Browse files
authored
Merge pull request #58 from initia-labs/chore/improve-cache
improve cache kvstore's performance
2 parents 2e4f1e3 + eeba6e9 commit d18034a

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

store/cache_kvstore.go

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ package store
33
import (
44
"context"
55

6+
corestoretypes "cosmossdk.io/core/store"
67
"cosmossdk.io/errors"
78
cachekv "cosmossdk.io/store/cachekv"
8-
"cosmossdk.io/store/types"
9+
storetypes "cosmossdk.io/store/types"
910
bigcache "github.com/allegro/bigcache/v3"
1011
)
1112

13+
var _ corestoretypes.KVStore = (*CacheStore)(nil)
14+
1215
type CacheStore struct {
13-
store types.CacheKVStore
16+
store storetypes.CacheKVStore
1417
cache *bigcache.BigCache
1518
}
1619

17-
func NewCacheStore(store types.KVStore, capacity int) *CacheStore {
20+
func NewCacheStore(store storetypes.KVStore, capacity int) *CacheStore {
1821
// default with no eviction and custom hard max cache capacity
1922
cacheCfg := bigcache.DefaultConfig(0)
2023
cacheCfg.Verbose = false
@@ -31,33 +34,48 @@ func NewCacheStore(store types.KVStore, capacity int) *CacheStore {
3134
}
3235
}
3336

37+
// Get returns nil iff key doesn't exist. Errors on nil key.
3438
func (c CacheStore) Get(key []byte) ([]byte, error) {
35-
types.AssertValidKey(key)
39+
storetypes.AssertValidKey(key)
3640

37-
v, err := c.cache.Get(string(key))
38-
// cache hit
39-
if err == nil {
40-
return v, nil
41+
if value, err := c.cache.Get(string(key)); err == nil {
42+
return value, nil
4143
}
4244

4345
// get from store and write to cache
4446
value := c.store.Get(key)
45-
err = c.cache.Set(string(key), value)
46-
if err != nil {
47-
return nil, errors.Wrap(err, "failed to set cache")
47+
if value == nil {
48+
return nil, nil
4849
}
4950

51+
// ignore cache error
52+
_ = c.cache.Set(string(key), value)
53+
5054
return value, nil
5155
}
5256

57+
// Has checks if a key exists. Errors on nil key.
5358
func (c CacheStore) Has(key []byte) (bool, error) {
5459
_, err := c.cache.Get(string(key))
55-
return err == nil, err
60+
if err == nil {
61+
return true, nil
62+
}
63+
64+
value := c.store.Get(key)
65+
if value == nil {
66+
return false, nil
67+
}
68+
69+
// ignore cache error
70+
_ = c.cache.Set(string(key), value)
71+
72+
return true, nil
5673
}
5774

75+
// Set sets the key. Errors on nil key or value.
5876
func (c CacheStore) Set(key, value []byte) error {
59-
types.AssertValidKey(key)
60-
types.AssertValidValue(value)
77+
storetypes.AssertValidKey(key)
78+
storetypes.AssertValidValue(value)
6179

6280
err := c.cache.Set(string(key), value)
6381
if err != nil {
@@ -68,20 +86,35 @@ func (c CacheStore) Set(key, value []byte) error {
6886
return nil
6987
}
7088

89+
// Delete deletes the key. Errors on nil key.
7190
func (c CacheStore) Delete(key []byte) error {
72-
types.AssertValidKey(key)
91+
storetypes.AssertValidKey(key)
7392

74-
_ = c.cache.Delete(string(key))
93+
err := c.cache.Delete(string(key))
94+
if err != nil && errors.IsOf(err, bigcache.ErrEntryNotFound) {
95+
return errors.Wrap(err, "failed to delete cache")
96+
}
7597
c.store.Delete(key)
7698

7799
return nil
78100
}
79101

80-
func (c CacheStore) Iterator(start, end []byte) (types.Iterator, error) {
102+
// Iterator iterates over a domain of keys in ascending order. End is exclusive.
103+
// Start must be less than end, or the Iterator is invalid.
104+
// Iterator must be closed by caller.
105+
// To iterate over entire domain, use store.Iterator(nil, nil)
106+
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
107+
// Exceptionally allowed for cachekv.Store, safe to write in the modules.
108+
func (c CacheStore) Iterator(start, end []byte) (storetypes.Iterator, error) {
81109
return c.store.Iterator(start, end), nil
82110
}
83111

84-
func (c CacheStore) ReverseIterator(start, end []byte) (types.Iterator, error) {
112+
// ReverseIterator iterates over a domain of keys in descending order. End is exclusive.
113+
// Start must be less than end, or the Iterator is invalid.
114+
// Iterator must be closed by caller.
115+
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
116+
// Exceptionally allowed for cachekv.Store, safe to write in the modules.
117+
func (c CacheStore) ReverseIterator(start, end []byte) (storetypes.Iterator, error) {
85118
return c.store.ReverseIterator(start, end), nil
86119
}
87120

0 commit comments

Comments
 (0)