Documentation
¶
Index ¶
- Constants
- func HashInt(key int) uint32
- func HashInt64(key int64) uint32
- func HashString(s string) uint32
- func HashUint64(key uint64) uint32
- type ConcurrentMapShard
- type Map
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Count() int
- func (m *Map[K, V]) Get(key K) (V, bool)
- func (m *Map[K, V]) IterCb(fn func(key K, v V) bool)
- func (m *Map[K, V]) Keys() []K
- func (m *Map[K, V]) MSet(data map[K]V)
- func (m *Map[K, V]) MarshalJSON() ([]byte, error)
- func (m *Map[K, V]) MarshalYAML() (interface{}, error)
- func (m *Map[K, V]) Pop(key K) (V, bool)
- func (m *Map[K, V]) PrettyPrint() string
- func (m *Map[K, V]) Print(w io.Writer)
- func (m *Map[K, V]) Remove(key K)
- func (m *Map[K, V]) Set(key K, value V)
- func (m *Map[K, V]) SetIfAbsent(key K, value V) (V, bool)
- func (m *Map[K, V]) String() string
- func (m *Map[K, V]) UnmarshalJSON(b []byte) error
- func (m *Map[K, V]) UnmarshalYAML(value *yaml.Node) error
- func (m *Map[K, V]) Upsert(key K, cb func(exist bool, valueInMap V) V) V
- type Option
Constants ¶
const DEFAULT_SHARD_COUNT = 32
默认分片数量
Variables ¶
This section is empty.
Functions ¶
func HashString ¶
HashString 针对 string 类型的标准 FNV-1a 哈希算法 FNV 算法分布极其均匀,是处理字符串的标准选择
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]) IterCb ¶
IterCb 接受一个回调函数 fn fn 的参数是 key 和 value fn 的返回值是一个 bool:如果返回 true,继续遍历;如果返回 false,停止遍历。
func (*Map[K, V]) MSet ¶
func (m *Map[K, V]) MSet(data map[K]V)
MSet 批量写入多个键值对 策略:预先将数据按分片归类,减少锁的竞争次数
func (*Map[K, V]) MarshalJSON ¶
MarshalJSON 实现 json.Marshaler 接口 当你调用 json.Marshal(cMap) 时,会自动调用此方法
func (*Map[K, V]) MarshalYAML ¶
MarshalYAML 实现 yaml.Marshaler 接口 当调用 yaml.Marshal(cMap) 时会自动触发
func (*Map[K, V]) PrettyPrint ¶
PrettyPrint 以格式化的 JSON 样式打印 (适合调试复杂结构) 需要引入 "encoding/json"
func (*Map[K, V]) SetIfAbsent ¶
SetIfAbsent 如果 Key 不存在,则写入值;如果存在,则什么都不做 返回值: (实际存储的值, 是否是新写入的) 如果返回 true,说明写入成功;如果返回 false,说明 Key 已存在,返回旧值
func (*Map[K, V]) String ¶
String 实现 fmt.Stringer 接口 这允许你直接使用 fmt.Println(cMap) 输出格式示例: {key1: val1, key2: val2}
func (*Map[K, V]) UnmarshalJSON ¶
UnmarshalJSON 实现 json.Unmarshaler 接口 当你调用 json.Unmarshal(data, cMap) 时,会自动调用此方法
func (*Map[K, V]) UnmarshalYAML ¶
UnmarshalYAML 实现 yaml.Unmarshaler 接口 当调用 yaml.Unmarshal(data, cMap) 时会自动触发
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)