Skip to content

Commit

Permalink
refactor: Updating LRU cache to version2 #514 (#602)
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
sadaghiani authored Jul 28, 2023
1 parent db341c3 commit dcf2242
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
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 {
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

0 comments on commit dcf2242

Please sign in to comment.