cache

package
v2.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package cache package

Index

Constants

View Source
const (
	// CacheProviderSLRU cache with slur algorithm.
	CacheProviderSLRU = "slru"
	// CacheProviderBC bigcache
	CacheProviderBC = "bigcache"
)

Variables

This section is empty.

Functions

func NewCacheWrapToDBHandle added in v2.2.0

func NewCacheWrapToDBHandle(c Cache, innerDb protocol.DBHandle, l protocol.Logger) protocol.DBHandle

NewCacheWrapToDBHandle return a cacheWrapToDB which cache , db and log

@Description:
@param c
@param innerDb
@param l
@return protocol.DBHandle

func QuickSort added in v2.2.0

func QuickSort(arr []uint64) []uint64

QuickSort 快排,获得从小到大排序后的结果,返回

@Description:
@param arr
@return []uint64

func SystemQuickSort added in v2.3.2

func SystemQuickSort(arr []uint64)

SystemQuickSort 快排,获得从大到小排序后的结果,返回

@Description:
@param arr
@return []uint64

Types

type BlockBatch added in v2.4.0

type BlockBatch struct {
	Height  uint64
	Batcher protocol.StoreBatcher
}

BlockBatch is a kv batcher with block height.

type Cache added in v2.2.0

type Cache interface {

	// Get get key from cache or db
	//  @Description:
	//  @param key
	//  @return []byte
	//  @return error
	Get(key string) ([]byte, error)

	// Set set k/v
	//  @Description:
	//  @param key
	//  @param entry
	//  @return error
	Set(key string, entry []byte) error

	// Delete del k
	//  @Description:
	//  @param key
	//  @return error
	Delete(key string) error

	// Reset set cache to initial state
	//  @Description:
	//  @return error
	Reset() error

	// Close  close cache
	//  @Description:
	//  @return error
	Close() error
}

Cache interface

@Description:

func NewCache added in v2.4.0

func NewCache(cacheConf *conf.CacheConfig, logger protocol.Logger) (Cache, error)

NewCache construct Cache

type CacheWrapToDBHandle added in v2.2.0

type CacheWrapToDBHandle struct {
	// contains filtered or unexported fields
}

CacheWrapToDBHandle struct

@Description:

func (*CacheWrapToDBHandle) Close added in v2.2.0

func (c *CacheWrapToDBHandle) Close() error

Close close cache and db

@Description:
@receiver c
@return error

func (*CacheWrapToDBHandle) CompactRange added in v2.2.0

func (c *CacheWrapToDBHandle) CompactRange(start, limit []byte) error

CompactRange compacts the underlying DB for the given key range

@Description:
@receiver c
@param start
@param limit
@return error

func (*CacheWrapToDBHandle) Delete added in v2.2.0

func (c *CacheWrapToDBHandle) Delete(key []byte) error

Delete delete key/value from cache and db

@Description:
@receiver c
@param key
@return error

func (*CacheWrapToDBHandle) Get added in v2.2.0

func (c *CacheWrapToDBHandle) Get(key []byte) ([]byte, error)

Get get value by key from cache and db

@Description:
@receiver c
@param key
@return []byte
@return error

func (*CacheWrapToDBHandle) GetDbType added in v2.2.0

func (c *CacheWrapToDBHandle) GetDbType() string

GetDbType get db type from db

@Description:
@receiver c
@return string

func (*CacheWrapToDBHandle) GetKeys added in v2.3.0

func (c *CacheWrapToDBHandle) GetKeys(keys [][]byte) ([][]byte, error)

GetKeys batch get key's value; if not found , batch get from db then refill them in cache @Description: @receiver c @param keys @return [][]byte @return error

func (*CacheWrapToDBHandle) GetWriteBatchSize added in v2.2.0

func (c *CacheWrapToDBHandle) GetWriteBatchSize() uint64

GetWriteBatchSize not implement , will panic

@Description:
@receiver c
@return uint64

func (*CacheWrapToDBHandle) Has added in v2.2.0

func (c *CacheWrapToDBHandle) Has(key []byte) (bool, error)

Has check key whether exist in cache and db

@Description:
@receiver c
@param key
@return bool
@return error

func (*CacheWrapToDBHandle) NewIteratorWithPrefix added in v2.2.0

func (c *CacheWrapToDBHandle) NewIteratorWithPrefix(prefix []byte) (protocol.Iterator, error)

NewIteratorWithPrefix returns an iterator that contains all the key-values with given prefix

@Description:
@receiver c
@param prefix
@return protocol.Iterator
@return error

func (*CacheWrapToDBHandle) NewIteratorWithRange added in v2.2.0

func (c *CacheWrapToDBHandle) NewIteratorWithRange(start []byte, limit []byte) (protocol.Iterator, error)

NewIteratorWithRange return iterator from db that contains all the key-values between given key ranges start is included in the results and limit is excluded.

@Description:
@receiver c
@param start
@param limit
@return protocol.Iterator
@return error

func (*CacheWrapToDBHandle) Put added in v2.2.0

func (c *CacheWrapToDBHandle) Put(key []byte, value []byte) error

Put set key/value include cache and db @Description: @receiver c @param key @param value @return error

func (*CacheWrapToDBHandle) WriteBatch added in v2.2.0

func (c *CacheWrapToDBHandle) WriteBatch(batch protocol.StoreBatcher, sync bool) error

WriteBatch write a batch to cache and db

@Description:
@receiver c
@param batch
@param sync
@return error

type FIFOBlockCache added in v2.4.0

type FIFOBlockCache struct {
	// contains filtered or unexported fields
}

FIFOBlockCache is a thread-safe FIFO cache for continuous blocks.

func NewAutoScaleFIFOBlockCache added in v2.4.0

func NewAutoScaleFIFOBlockCache(initCapacity int, logger protocol.Logger) *FIFOBlockCache

NewAutoScaleFIFOBlockCache create a a new FIFOBlockCache instance which can scale it's capacity automatically.

func NewFIFOBlockCache added in v2.4.0

func NewFIFOBlockCache(blockWriteBufferSize int, logger protocol.Logger) *FIFOBlockCache

NewFIFOBlockCache creates a new FIFOBlockCache instance with fixed capacity.

func (*FIFOBlockCache) Clear added in v2.4.0

func (c *FIFOBlockCache) Clear()

Clear remove all data.

func (*FIFOBlockCache) Get added in v2.4.0

func (c *FIFOBlockCache) Get(key []byte) ([]byte, bool)

Get returns the latest value associated with the key. bool is false if the key is not found.

func (*FIFOBlockCache) Has added in v2.4.0

func (c *FIFOBlockCache) Has(key []byte) (deleted bool, ok bool)

Has checks if the key exists in the cache. ok is true if the key exists. deleted is true if the key exists but has been deleted.

func (*FIFOBlockCache) KVRange added in v2.4.0

func (c *FIFOBlockCache) KVRange(startKey []byte, endKey []byte) (map[string][]byte, error)

KVRange returns a map of key-value pairs in the range [startKey, endKey).

func (*FIFOBlockCache) PeekMinBlock added in v2.4.0

func (c *FIFOBlockCache) PeekMinBlock() (*BlockBatch, bool)

PeekMinBlock returns the min height block from the cache. bool is false if the cache is empty.

func (*FIFOBlockCache) PopBlock added in v2.4.0

func (c *FIFOBlockCache) PopBlock() (*BlockBatch, bool)

PopBlock removes and returns the min height block from the cache. bool is false if the cache is empty.

func (*FIFOBlockCache) PopBlockIf added in v2.4.0

func (c *FIFOBlockCache) PopBlockIf(fn func(*BlockBatch) bool) (*BlockBatch, bool)

PopBlockIf the block at the head of the cache will be popped out when it meets the condition of the given function. It is usually used to maintain the consistency of peek and pop data.

func (*FIFOBlockCache) PushBlock added in v2.4.0

func (c *FIFOBlockCache) PushBlock(bt *BlockBatch)

PushBlock push a block to head of the cache. Note:block height must be continuous.

func (*FIFOBlockCache) Size added in v2.4.0

func (c *FIFOBlockCache) Size() int

Size returns the count of blocks in the cache.

type Factory added in v2.4.0

type Factory struct {
	// contains filtered or unexported fields
}

Factory cache factory

func (*Factory) CreateCache added in v2.4.0

func (f *Factory) CreateCache(cacheConf *conf.CacheConfig) (Cache, error)

CreateCache construct Cache by cacheConf. According to cacheConf.Provider, create different Cache.

type SLRUCache added in v2.4.0

type SLRUCache struct {
	// contains filtered or unexported fields
}

SLRUCache the cache with slru.

func NewSlruCache added in v2.4.0

func NewSlruCache(c *SLRUConfig, logger protocol.Logger) *SLRUCache

NewSlruCache new slru cache. Make Capacity divisible by 4 and calculate the number of bytes of memory.

func (*SLRUCache) Close added in v2.4.0

func (c *SLRUCache) Close() error

Close no op.

func (*SLRUCache) Delete added in v2.4.0

func (c *SLRUCache) Delete(key string) error

Delete delete value by key

func (*SLRUCache) Get added in v2.4.0

func (c *SLRUCache) Get(key string) ([]byte, error)

Get gets value by key Reading will cause data to migrate between different levels, so concurrent reading is not possible.

func (*SLRUCache) Reset added in v2.4.0

func (c *SLRUCache) Reset() error

Reset recreate a new slru.

func (*SLRUCache) Set added in v2.4.0

func (c *SLRUCache) Set(key string, value []byte) error

Set add kv to slru. If the key already exists, it will be updated.

type SLRUConfig added in v2.4.0

type SLRUConfig struct {
	Memory   int64 `mapstructure:"memory"`   //Maximum memory usage in MB
	Capacity int   `mapstructure:"capacity"` //Maximum number of cache entries
	//function to calculate the memory size consumed by value.
	Cost func(interface{}) int64 `mapstructure:"-"`
}

SLRUConfig config for slru cache.

type StoreCacheMgr

type StoreCacheMgr struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

StoreCacheMgr provide handle to cache instances

@Description:

func NewStoreCacheMgr

func NewStoreCacheMgr(chainId string, blockWriteBufferSize int, logger protocol.Logger) *StoreCacheMgr

NewStoreCacheMgr construct a new `StoreCacheMgr` with given chainId

@Description:
@param chainId
@param blockWriteBufferSize
@param logger
@return *StoreCacheMgr

func (*StoreCacheMgr) AddBlock

func (mgr *StoreCacheMgr) AddBlock(blockHeight uint64, updateBatch protocol.StoreBatcher)

AddBlock cache a block with given block height and update batch

@Description:
@receiver mgr
@param blockHeight
@param updateBatch

func (*StoreCacheMgr) Clear

func (mgr *StoreCacheMgr) Clear()

Clear 清除缓存,目前未做任何清除操作

@Description:
@receiver mgr

func (*StoreCacheMgr) DelBlock

func (mgr *StoreCacheMgr) DelBlock(blockHeight uint64)

DelBlock delete block for the given block height

@Description:
@receiver mgr
@param blockHeight

func (*StoreCacheMgr) Get

func (mgr *StoreCacheMgr) Get(key string) ([]byte, bool)

Get returns value if the key in cache, or returns nil if none exists.

@Description:
@receiver mgr
@param key
@return []byte
@return bool

func (*StoreCacheMgr) GetBatch added in v2.2.0

func (mgr *StoreCacheMgr) GetBatch(height uint64) (protocol.StoreBatcher, error)

GetBatch 根据块高,返回 块对应的cache

@Description:
@receiver mgr
@param height
@return protocol.StoreBatcher
@return error

func (*StoreCacheMgr) GetLength added in v2.3.3

func (mgr *StoreCacheMgr) GetLength() int

GetLength returns the length of the pendingBlockUpdates

func (*StoreCacheMgr) Has

func (mgr *StoreCacheMgr) Has(key string) (bool, bool)

Has returns true if the key in cache, or returns false if none exists. 如果这个key 对应value 是 nil,说明这个key被删除了 所以查找到第一个key,要判断 这个key 是否是被删除的

@Description:
@receiver mgr
@param key
@return bool isDelete
@return bool isExist

func (*StoreCacheMgr) HasFromHeight added in v2.3.2

func (mgr *StoreCacheMgr) HasFromHeight(key string, startHeight uint64, endHeight uint64) (bool, bool)

HasFromHeight returns true if the key in cache, or returns false if none exists by given startHeight, endHeight. 如果这个key 对应value 是 nil,说明这个key被删除了 所以查找到第一个key,要判断 这个key 是否是被删除的

@Description:
@receiver mgr
@param key, startHeight, endHeight
@return bool isDelete
@return bool isExist

func (*StoreCacheMgr) KVRange added in v2.2.0

func (mgr *StoreCacheMgr) KVRange(startKey []byte, endKey []byte) (map[string][]byte, error)

KVRange get data from mgr , [startKey,endKey)

@Description:
@receiver mgr
@param startKey
@param endKey
@return map[string][]byte
@return error

func (*StoreCacheMgr) LockForFlush

func (mgr *StoreCacheMgr) LockForFlush()

LockForFlush used to lock cache until all cache item be flushed to db

@Description:
@receiver mgr

func (*StoreCacheMgr) UnLockFlush

func (mgr *StoreCacheMgr) UnLockFlush()

UnLockFlush used to unlock cache by release all semaphore

@Description:
@receiver mgr

Source Files

  • blockstore_cache.go
  • cache_wrap.go
  • factory.go
  • fifo_cache.go
  • ringbuffer.go
  • slru_cache.go

Directories

Path Synopsis
Package slru is a LRU with 4 segments.
Package slru is a LRU with 4 segments.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL