cachev2

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package cachev2 provides a high-performance concurrent map implementation optimized for cache operations. The implementation uses sharding to minimize lock contention by dividing the map into multiple independent shards, each protected by its own read-write mutex.

The concurrent map stores string keys mapped to *cache.Item values and is designed to be thread-safe for concurrent read and write operations across multiple goroutines.

Key features:

  • Sharded design with 32 shards to reduce lock contention
  • FNV-1a hash function for efficient key distribution
  • Thread-safe operations with optimized read/write locking
  • Buffered iteration support for safe concurrent traversal
  • Standard map operations: Set, Get, Has, Remove, Pop, Clear, Count

Example usage:

cm := cachev2.New()
cm.Set("key", &cache.Item{...})
if item, ok := cm.Get("key"); ok {
    // Process item
}

Index

Constants

View Source
const (
	// ShardCount is the number of shards used by the map.
	ShardCount = 32
	// ShardCount32 is the number of shards used by the map pre-casted to uint32 to avoid performance issues.
	ShardCount32 uint32 = uint32(ShardCount)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrentMap

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

ConcurrentMap is a "thread" safe map of type string:*cache.Item. To avoid lock bottlenecks this map is divided into several (ShardCount) map shards.

func New

func New() ConcurrentMap

New creates a new concurrent map.

func (*ConcurrentMap) Clear

func (cm *ConcurrentMap) Clear()

Clear removes all items from map.

func (*ConcurrentMap) Count

func (cm *ConcurrentMap) Count() int

Count returns the number of items in the map.

func (*ConcurrentMap) Get

func (cm *ConcurrentMap) Get(key string) (*cache.Item, bool)

Get retrieves an element from map under given key.

func (*ConcurrentMap) GetShard

func (cm *ConcurrentMap) GetShard(key string) *ConcurrentMapShard

GetShard returns shard under given key.

func (*ConcurrentMap) Has

func (cm *ConcurrentMap) Has(key string) bool

Has checks if key is present in the map.

func (*ConcurrentMap) IterBuffered

func (cm *ConcurrentMap) IterBuffered() <-chan Tuple

IterBuffered returns a buffered iterator which could be used in a for range loop.

func (*ConcurrentMap) Pop

func (cm *ConcurrentMap) Pop(key string) (*cache.Item, bool)

Pop removes an element from the map and returns it.

func (*ConcurrentMap) Remove

func (cm *ConcurrentMap) Remove(key string)

Remove removes the value under the specified key.

func (*ConcurrentMap) Set

func (cm *ConcurrentMap) Set(key string, value *cache.Item)

Set sets the given value under the specified key.

type ConcurrentMapShard

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

ConcurrentMapShard is a "thread" safe string to `*cache.Item` map shard.

type Tuple

type Tuple struct {
	Key string
	Val cache.Item
}

Tuple is used by the IterBuffered functions to wrap two variables together over a channel,.

Jump to

Keyboard shortcuts

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