Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Updating LRU cache to version2 #514 #602

Merged
merged 4 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 26 additions & 36 deletions sync/cache/cache.go
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 {
sadaghiani marked this conversation as resolved.
Show resolved Hide resolved
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()
}
9 changes: 1 addition & 8 deletions sync/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestKeys(t *testing.T) {
assert.Equal(t, blockKey(1234),
key{0x1, 0xd2, 0x4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
assert.Equal(t, certificateKey(1234),
key{0x2, 0xd2, 0x4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
}

func TestCacheBlocks(t *testing.T) {
ts := testsuite.NewTestSuite(t)

Expand Down Expand Up @@ -51,7 +44,7 @@ func TestClearCache(t *testing.T) {

cache.AddBlock(2, b)

assert.Equal(t, cache.Len(), 2) // block + certificate
assert.Equal(t, cache.Len(), 1)
cache.Clear()
assert.Equal(t, cache.Len(), 0)
assert.Nil(t, cache.GetBlock(2))
Expand Down