concurrent

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const DEFAULT_SHARD_COUNT = 32

默认分片数量

Variables

This section is empty.

Functions

func HashInt

func HashInt(key int) uint32

HashInt 针对 int 类型的快速哈希 使用了 Knuth's Multiplicative Hash 的变种,确保简单的序列数也能均匀打散

func HashInt64

func HashInt64(key int64) uint32

HashInt64 针对 int64 类型的哈希 混合高 32 位和低 32 位,确保高位变化也能影响分片结果

func HashString

func HashString(s string) uint32

HashString 针对 string 类型的标准 FNV-1a 哈希算法 FNV 算法分布极其均匀,是处理字符串的标准选择

func HashUint64

func HashUint64(key uint64) uint32

HashUint64 针对 uint64 类型的哈希

Types

type ConcurrentMapShard

type ConcurrentMapShard[K comparable, V any] struct {
	sync.RWMutex // 读写锁,读写分离提高性能
	// contains filtered or unexported fields
}

ConcurrentMapShard 是内部的分片结构 每个分片拥有自己的锁和原生 Map

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map 是我们暴露给外部的主结构体 K: 键的类型 (必须是可比较的) V: 值的类型 (任意)

func NewMap

func NewMap[K comparable, V any](hashFunc func(K) uint32, opts ...Option[K, V]) *Map[K, V]

NewMap 创建一个新的并发 Map hashFunc: 需要用户传入一个函数,将 Key 转换为 uint32 整数

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

Clear 清空 Map 中的所有数据 策略:直接用一个新的空 Map 替换旧 Map,而不是逐个删除 Key

func (*Map[K, V]) Count

func (m *Map[K, V]) Count() int

Count 统计所有元素的数量(大概率是准的,但在极高并发下是近似值)

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (V, bool)

Get 读取键值对

func (*Map[K, V]) IterCb

func (m *Map[K, V]) IterCb(fn func(key K, v V) bool)

IterCb 接受一个回调函数 fn fn 的参数是 key 和 value fn 的返回值是一个 bool:如果返回 true,继续遍历;如果返回 false,停止遍历。

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys() []K

Keys 获取所有的 Key

func (*Map[K, V]) MSet

func (m *Map[K, V]) MSet(data map[K]V)

MSet 批量写入多个键值对 策略:预先将数据按分片归类,减少锁的竞争次数

func (*Map[K, V]) MarshalJSON

func (m *Map[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON 实现 json.Marshaler 接口 当你调用 json.Marshal(cMap) 时,会自动调用此方法

func (*Map[K, V]) MarshalYAML

func (m *Map[K, V]) MarshalYAML() (interface{}, error)

MarshalYAML 实现 yaml.Marshaler 接口 当调用 yaml.Marshal(cMap) 时会自动触发

func (*Map[K, V]) Pop

func (m *Map[K, V]) Pop(key K) (V, bool)

Pop 从 Map 中删除一个 Key,并返回它被删除之前的值 如果 Key 不存在,返回零值和 false

func (*Map[K, V]) PrettyPrint

func (m *Map[K, V]) PrettyPrint() string

PrettyPrint 以格式化的 JSON 样式打印 (适合调试复杂结构) 需要引入 "encoding/json"

func (*Map[K, V]) Print

func (m *Map[K, V]) Print(w io.Writer)

Print 逐行打印所有键值对 (适合数据量较大时查看) 格式: [Key] val [Key] val

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(key K)

Remove 删除键值对

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(key K, value V)

Set 写入键值对

func (*Map[K, V]) SetIfAbsent

func (m *Map[K, V]) SetIfAbsent(key K, value V) (V, bool)

SetIfAbsent 如果 Key 不存在,则写入值;如果存在,则什么都不做 返回值: (实际存储的值, 是否是新写入的) 如果返回 true,说明写入成功;如果返回 false,说明 Key 已存在,返回旧值

func (*Map[K, V]) String

func (m *Map[K, V]) String() string

String 实现 fmt.Stringer 接口 这允许你直接使用 fmt.Println(cMap) 输出格式示例: {key1: val1, key2: val2}

func (*Map[K, V]) UnmarshalJSON

func (m *Map[K, V]) UnmarshalJSON(b []byte) error

UnmarshalJSON 实现 json.Unmarshaler 接口 当你调用 json.Unmarshal(data, cMap) 时,会自动调用此方法

func (*Map[K, V]) UnmarshalYAML

func (m *Map[K, V]) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML 实现 yaml.Unmarshaler 接口 当调用 yaml.Unmarshal(data, cMap) 时会自动触发

func (*Map[K, V]) Upsert

func (m *Map[K, V]) Upsert(key K, cb func(exist bool, valueInMap V) V) V

Upsert 提供原子性的“读取-修改-回写”操作 cb: 回调函数,参数是 (是否存在, 旧值)。返回值将作为新值写入 Map。 返回值: 最终写入 Map 的新值

type Option

type Option[K comparable, V any] func(*Map[K, V])

Option 定义配置函数的类型

func WithShardCount

func WithShardCount[K comparable, V any](count uint32) Option[K, V]

WithShardCount 允许用户自定义分片数量 count: 建议设置为 2 的幂 (如 16, 32, 64, 128)

Jump to

Keyboard shortcuts

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