-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BEDS-536): add cache to raw store (#966)
- Loading branch information
1 parent
71253dd
commit e0b6374
Showing
5 changed files
with
130 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package db2 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ttl = 200 * time.Millisecond | ||
|
||
type MinimalBlock struct { | ||
Result struct { | ||
Hash string `json:"hash"` | ||
} `json:"result"` | ||
} | ||
|
||
type CachedRawStore struct { | ||
db RawStoreReader | ||
// sync.Map with manual delete have better perf than freecache because we can handle this way a ttl < 1s | ||
cache sync.Map | ||
} | ||
|
||
func WithCache(reader RawStoreReader) *CachedRawStore { | ||
return &CachedRawStore{ | ||
db: reader, | ||
} | ||
} | ||
|
||
func (c *CachedRawStore) ReadBlockByNumber(chainID uint64, number int64) (*FullBlockRawData, error) { | ||
key := blockKey(chainID, number) | ||
v, ok := c.cache.Load(key) | ||
if ok { | ||
return v.(*FullBlockRawData), nil | ||
} | ||
|
||
block, err := c.db.ReadBlockByNumber(chainID, number) | ||
if block != nil { | ||
c.cache.Store(key, block) | ||
|
||
// retrieve the block hash for caching purpose | ||
var mini MinimalBlock | ||
if err := json.Unmarshal(block.Block, &mini); err != nil { | ||
return nil, fmt.Errorf("cannot unmarshal block: %w", err) | ||
} | ||
c.cache.Store(mini.Result.Hash, number) | ||
go func() { | ||
time.Sleep(ttl) | ||
c.cache.Delete(key) | ||
c.cache.Delete(mini.Result.Hash) | ||
}() | ||
} | ||
return block, err | ||
} | ||
|
||
func (c *CachedRawStore) ReadBlockByHash(chainID uint64, hash string) (*FullBlockRawData, error) { | ||
v, ok := c.cache.Load(hash) | ||
if !ok { | ||
return c.db.ReadBlockByHash(chainID, hash) | ||
} | ||
|
||
v, ok = c.cache.Load(blockKey(chainID, v.(int64))) | ||
if !ok { | ||
return c.db.ReadBlockByHash(chainID, hash) | ||
} | ||
|
||
return v.(*FullBlockRawData), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters