github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/istructsmem/internal/plogcache/cache.go (about)

     1  /*
     2   * Copyright (c) 2021-present Sigma-Soft, Ltd.
     3   * @author: Nikolay Nikitin
     4   */
     5  
     6  package plogcache
     7  
     8  import (
     9  	"github.com/voedger/voedger/pkg/istructs"
    10  	"github.com/voedger/voedger/pkg/objcache"
    11  )
    12  
    13  // plog events cache
    14  //
    15  // Get() returns plog event by handling partition and offset.
    16  //
    17  // Put() puts plog event into cache.
    18  type Cache struct {
    19  	cache objcache.ICache[plogEvKey, istructs.IPLogEvent]
    20  }
    21  
    22  func New(size int) *Cache {
    23  	cache := &Cache{}
    24  	if size > 0 {
    25  		cache.cache = objcache.New[plogEvKey, istructs.IPLogEvent](size)
    26  	}
    27  	return cache
    28  }
    29  
    30  // Gets PLOG event on the key from the specified partition and offset
    31  func (c *Cache) Get(partition istructs.PartitionID, offset istructs.Offset) (e istructs.IPLogEvent, ok bool) {
    32  	if c.cache != nil {
    33  		return c.cache.Get(plogEvKey{partition, offset})
    34  	}
    35  	return nil, false
    36  }
    37  
    38  // Puts the specified PLOG event on the key from the specified partition and offset
    39  func (c *Cache) Put(partition istructs.PartitionID, offset istructs.Offset, event istructs.IPLogEvent) {
    40  	if c.cache != nil {
    41  		c.cache.Put(plogEvKey{partition, offset}, event)
    42  	}
    43  }
    44  
    45  type plogEvKey struct {
    46  	partition istructs.PartitionID
    47  	offset    istructs.Offset
    48  }