-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* refactor: Updating LRU cache to version2 #514 * chore: rename cache variable * docs: len returns the maximum number of items cache
- Loading branch information
1 parent
db341c3
commit dcf2242
Showing
2 changed files
with
27 additions
and
44 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 |
---|---|---|
@@ -1,84 +1,74 @@ | ||
package cache | ||
|
||
import ( | ||
lru "github.com/hashicorp/golang-lru" | ||
lru "github.com/hashicorp/golang-lru/v2" | ||
"github.com/pactus-project/pactus/types/block" | ||
"github.com/pactus-project/pactus/util" | ||
) | ||
|
||
const ( | ||
blockPrefix = 0x01 | ||
certificatePrefix = 0x02 | ||
) | ||
|
||
type key [32]byte | ||
|
||
func blockKey(height uint32) key { | ||
var k key | ||
k[0] = blockPrefix | ||
copy(k[1:], util.Uint32ToSlice(height)) | ||
return k | ||
} | ||
|
||
func certificateKey(height uint32) key { | ||
var k key | ||
k[0] = certificatePrefix | ||
copy(k[1:], util.Uint32ToSlice(height)) | ||
return k | ||
} | ||
|
||
type Cache struct { | ||
cache *lru.Cache // it's thread safe | ||
blocks *lru.Cache[uint32, *block.Block] // it's thread safe | ||
certs *lru.Cache[uint32, *block.Certificate] | ||
} | ||
|
||
func NewCache(size int) (*Cache, error) { | ||
c, err := lru.New(size) | ||
b, err := lru.New[uint32, *block.Block](size) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c, err := lru.New[uint32, *block.Certificate](size) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Cache{ | ||
cache: c, | ||
blocks: b, | ||
certs: c, | ||
}, nil | ||
} | ||
|
||
func (c *Cache) HasBlockInCache(height uint32) bool { | ||
_, ok := c.cache.Get(blockKey(height)) | ||
return ok | ||
return c.blocks.Contains(height) | ||
} | ||
|
||
func (c *Cache) GetBlock(height uint32) *block.Block { | ||
i, ok := c.cache.Get(blockKey(height)) | ||
block, ok := c.blocks.Get(height) | ||
if ok { | ||
return i.(*block.Block) | ||
return block | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Cache) AddBlock(height uint32, block *block.Block) { | ||
c.cache.Add(blockKey(height), block) | ||
c.blocks.Add(height, block) | ||
c.AddCertificate(height-1, block.PrevCertificate()) | ||
} | ||
|
||
func (c *Cache) GetCertificate(height uint32) *block.Certificate { | ||
i, ok := c.cache.Get(certificateKey(height)) | ||
certificate, ok := c.certs.Get(height) | ||
if ok { | ||
return i.(*block.Certificate) | ||
return certificate | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Cache) AddCertificate(height uint32, cert *block.Certificate) { | ||
if cert != nil { | ||
c.cache.Add(certificateKey(height), cert) | ||
c.certs.Add(height, cert) | ||
} | ||
} | ||
|
||
// Len returns the maximum number of items in the blocks and certificates cache. | ||
func (c *Cache) Len() int { | ||
return c.cache.Len() | ||
if c.blocks.Len() > c.certs.Len() { | ||
return c.blocks.Len() | ||
} | ||
return c.certs.Len() | ||
} | ||
|
||
func (c *Cache) Clear() { | ||
c.cache.Purge() | ||
c.blocks.Purge() | ||
c.certs.Purge() | ||
} |
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