safe

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array16Hasher

type Array16Hasher struct{}

Array16Hasher 为常用长度创建专门的哈希器

func (Array16Hasher) Hash

func (h Array16Hasher) Hash(key [16]byte) uint64

type BytesHasher

type BytesHasher struct{}

BytesHasher []byte哈希器

func (BytesHasher) Hash

func (h BytesHasher) Hash(key []byte) uint64

type CacheConfig

type CacheConfig struct {
	EnableCleanup bool          `json:"enable_cleanup" yaml:"enable_cleanup" ini:"enable_cleanup"` // 是否启用自动清理功能
	ClearInterval time.Duration `json:"clear_interval" yaml:"clear_interval" ini:"clear_interval"` // 清理间隔,推荐值是设置成 ttl/2 或者 ttl/3
	TTL           time.Duration `json:"ttl" yaml:"ttl" ini:"ttl"`                                  // 默认TTL为0,表示不过期
	ShardSize     uint64        `json:"shard_size" yaml:"shard_size" ini:"shard_size"`             // 分片数量,默认为2的8次方
	UseKey        bool          `json:"use_key" yaml:"use_key" ini:"use_key"`                      // 是否使用 key 作为 hash 值
}

type FloatHasher

type FloatHasher[T constraints.Float] struct{}

FloatHasher 浮点数哈希器

func (FloatHasher[T]) Hash

func (h FloatHasher[T]) Hash(key T) uint64

type Hasher

type Hasher[K comparable] interface {
	Hash(K) uint64
}

Hasher 编译时确定的哈希函数

type IntegerHasher

type IntegerHasher[T constraints.Integer] struct{}

IntegerHasher 整数哈希器

func (IntegerHasher[T]) Hash

func (h IntegerHasher[T]) Hash(key T) uint64

type Map

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

func NewMap

func NewMap[K comparable, V any](ctx context.Context, hasher Hasher[K], configs ...CacheConfig) *Map[K, V]

func (*Map[K, V]) Delete

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

Delete 移除键的值。

func (*Map[K, V]) DeleteAll

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

DeleteAll 删除所有键值对

func (*Map[K, V]) DeleteAndGetCount

func (m *Map[K, V]) DeleteAndGetCount(keys ...K) int

DeleteAndGetCount 删除多个键并返回删除的数量

func (*Map[K, V]) DeletePrefix

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

DeletePrefix 删除指定前缀的键值对

func (*Map[K, V]) DeleteSuffix

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

DeleteSuffix 删除指定后缀的键值对

func (*Map[K, V]) GetHeartbeat

func (m *Map[K, V]) GetHeartbeat(key K) (time.Time, bool)

func (*Map[K, V]) GetTTL

func (m *Map[K, V]) GetTTL(key K) (time.Duration, bool)

GetTTL 获取键的剩余存活时间

func (*Map[K, V]) IsExpired

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

IsExpired 检测键是否已过期

func (*Map[K, V]) Load

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

Load 获取键的值。

func (*Map[K, V]) LoadAndDelete

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

LoadAndDelete 获取键的值并删除键值对。

func (*Map[K, V]) LoadAndDeleteIf

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

LoadAndDeleteIf 获取键的值并删除键值对,条件性删除 返回值第二个参数 如果 == true,表示删除成功

func (*Map[K, V]) LoadAndRefresh

func (m *Map[K, V]) LoadAndRefresh(key K, duration ...time.Duration) (V, bool)

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, val V, duration ...time.Duration) (actual V, loaded bool)

LoadOrStore 获取键的值,如果没有则存储键的值。

func (*Map[K, V]) LoadOrStoreFunc

func (m *Map[K, V]) LoadOrStoreFunc(key K, valueFunc func(k K) (V, error), duration ...time.Duration) (V, bool, error)

LoadOrStoreFunc 获取键的值,如果没有则存储键的值。

func (*Map[K, V]) LoadWithExpiry

func (m *Map[K, V]) LoadWithExpiry(key K) (V, time.Time, bool)

LoadWithExpiry 获取键的值和过期时间

func (*Map[K, V]) Range

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

func (*Map[K, V]) Refresh

func (m *Map[K, V]) Refresh(key K, duration ...time.Duration) bool

Refresh 更新键的过期时间

func (*Map[K, V]) SetOnExpired

func (m *Map[K, V]) SetOnExpired(onExpired OnExpired[K])

SetOnExpired 设置过期回调函数。

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, val V, duration ...time.Duration)

Store 存储键的值。

type OnExpired

type OnExpired[K comparable] func(key []K) // 过期回调

OnExpired 过期回调

type Resource

type Resource[T any] struct {
	// contains filtered or unexported fields
}

func NewResource

func NewResource[T any](resource *T) *Resource[T]

NewResource 创建一个原子操作的资源 注意这个资源不是线程安全的 虽然原子操作不会出现数据竞争,但是数据读取出来后的比较不是线程安全的。可能真实值已经被其他线程修改了

func (*Resource[T]) Read

func (s *Resource[T]) Read() *T

func (*Resource[T]) Write

func (s *Resource[T]) Write(newResource *T)

type ResourceMutex

type ResourceMutex[T any] struct {
	// contains filtered or unexported fields
}

ResourceMutex 使用 Mutex 的线程安全资源

func NewResourceMutex

func NewResourceMutex[T any](resource T) *ResourceMutex[T]

NewResourceMutex 创建 Mutex 版本的安全资源

func (*ResourceMutex[T]) Read

func (sr *ResourceMutex[T]) Read() T

func (*ResourceMutex[T]) ReadWith

func (sr *ResourceMutex[T]) ReadWith(fn func(T))

func (*ResourceMutex[T]) Update

func (sr *ResourceMutex[T]) Update(fn func(T) T)

func (*ResourceMutex[T]) Write

func (sr *ResourceMutex[T]) Write(newResource T)

type ResourceRWMutex

type ResourceRWMutex[T any] struct {
	// contains filtered or unexported fields
}

ResourceRWMutex 使用 RWMutex 的线程安全资源

func NewResourceRWMutex

func NewResourceRWMutex[T any](resource T) *ResourceRWMutex[T]

NewResourceRWMutex 创建 RWMutex 版本的安全资源

func (*ResourceRWMutex[T]) Read

func (sr *ResourceRWMutex[T]) Read() T

func (*ResourceRWMutex[T]) ReadWith

func (sr *ResourceRWMutex[T]) ReadWith(fn func(T))

func (*ResourceRWMutex[T]) ReadWithResult

func (sr *ResourceRWMutex[T]) ReadWithResult(fn func(T) error) error

func (*ResourceRWMutex[T]) Update

func (sr *ResourceRWMutex[T]) Update(fn func(T) T)

func (*ResourceRWMutex[T]) Write

func (sr *ResourceRWMutex[T]) Write(newResource T)

type StringHasher

type StringHasher struct{}

StringHasher 字符串哈希器

func (StringHasher) Hash

func (h StringHasher) Hash(key string) uint64

type SyncMap

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

SyncMap 是 sync.Map 的泛型包装器。 Deprecated: 弃用,最新采用 safe.Map

func (*SyncMap[K, V]) CompareAndDelete

func (m *SyncMap[K, V]) CompareAndDelete(key K, oldval V) bool

CompareAndDelete 比较键的值与提供的旧值, 如果相等,则删除该项。 它返回是否成功进行了删除。

func (*SyncMap[K, V]) CompareAndSwap

func (m *SyncMap[K, V]) CompareAndSwap(key K, oldval, newval V) bool

CompareAndSwap 比较键的值与提供的旧值, 如果相等,则将其替换为新值。 它返回是否成功进行了替换。

func (*SyncMap[K, V]) Delete

func (m *SyncMap[K, V]) Delete(key K)

Delete 移除键的值。

func (*SyncMap[K, V]) DeleteAll

func (m *SyncMap[K, V]) DeleteAll()

func (*SyncMap[K, V]) DeletePrefix

func (m *SyncMap[K, V]) DeletePrefix(prefix string)

func (*SyncMap[K, V]) DeleteSuffix

func (m *SyncMap[K, V]) DeleteSuffix(suffix string)

func (*SyncMap[K, V]) Load

func (m *SyncMap[K, V]) Load(key K) (V, bool)

Load 返回存储在 map 中给定键的值。

func (*SyncMap[K, V]) LoadAndDelete

func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete 删除键的值,返回之前的值(如果有)。 loaded 结果为 true 表示键存在。

func (*SyncMap[K, V]) LoadOrStore

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore 返回存在的键的值(如果存在)。 否则,它存储并返回给定的值。 loaded 结果为 true 表示找到了该值。

func (*SyncMap[K, V]) LoadOrStoreFunc

func (m *SyncMap[K, V]) LoadOrStoreFunc(key K, valueFunc func(k K) (V, bool)) (actual V, loaded bool)

func (*SyncMap[K, V]) Range

func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)

Range 依次调用 f 函数,针对 map 中的每个键和值。 如果 f 返回 false,则停止迭代。

func (*SyncMap[K, V]) Store

func (m *SyncMap[K, V]) Store(key K, value V)

Store 设置键的值。

func (*SyncMap[K, V]) Swap

func (m *SyncMap[K, V]) Swap(key K, newval V) (oldval V, swapped bool)

Swap 将键的值与提供的新值交换,返回旧值。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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