7 "github.com/golang/groupcache/lru"
8 "github.com/golang/groupcache/singleflight"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/bc/types"
14 const maxCachedBlocks = 30
16 func newBlockCache(fillFn func(hash *bc.Hash) *types.Block) blockCache {
18 lru: lru.New(maxCachedBlocks),
23 type blockCache struct {
26 fillFn func(hash *bc.Hash) *types.Block
27 single singleflight.Group
30 func (c *blockCache) lookup(hash *bc.Hash) (*types.Block, error) {
31 if b, ok := c.get(hash); ok {
35 block, err := c.single.Do(hash.String(), func() (interface{}, error) {
38 return nil, fmt.Errorf("There are no block with given hash %s", hash.String())
47 return block.(*types.Block), nil
50 func (c *blockCache) get(hash *bc.Hash) (*types.Block, bool) {
52 block, ok := c.lru.Get(*hash)
57 return block.(*types.Block), ok
60 func (c *blockCache) add(block *types.Block) {
62 c.lru.Add(block.Hash(), block)