Documentation
¶
Index ¶
- type Array16Hasher
- type BytesHasher
- type CacheConfig
- type FloatHasher
- type Hasher
- type IntegerHasher
- type Map
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) DeleteAll()
- func (m *Map[K, V]) DeleteAndGetCount(keys ...K) int
- func (m *Map[K, V]) DeletePrefix(prefix string)
- func (m *Map[K, V]) DeleteSuffix(suffix string)
- func (m *Map[K, V]) GetHeartbeat(key K) (time.Time, bool)
- func (m *Map[K, V]) GetTTL(key K) (time.Duration, bool)
- func (m *Map[K, V]) IsExpired(key K) bool
- func (m *Map[K, V]) Load(key K) (V, bool)
- func (m *Map[K, V]) LoadAndDelete(key K) (V, bool)
- func (m *Map[K, V]) LoadAndDeleteIf(key K, condition func(value V) bool) (V, bool)
- func (m *Map[K, V]) LoadAndRefresh(key K, duration ...time.Duration) (V, bool)
- func (m *Map[K, V]) LoadOrStore(key K, val V, duration ...time.Duration) (actual V, loaded bool)
- func (m *Map[K, V]) LoadOrStoreFunc(key K, valueFunc func(k K) (V, error), duration ...time.Duration) (V, bool, error)
- func (m *Map[K, V]) LoadWithExpiry(key K) (V, time.Time, bool)
- func (m *Map[K, V]) Range(f func(key K, value V) bool)
- func (m *Map[K, V]) Refresh(key K, duration ...time.Duration) bool
- func (m *Map[K, V]) SetOnExpired(onExpired OnExpired[K])
- func (m *Map[K, V]) Store(key K, val V, duration ...time.Duration)
- type OnExpired
- type Resource
- type ResourceMutex
- type ResourceRWMutex
- type StringHasher
- type SyncMap
- func (m *SyncMap[K, V]) CompareAndDelete(key K, oldval V) bool
- func (m *SyncMap[K, V]) CompareAndSwap(key K, oldval, newval V) bool
- func (m *SyncMap[K, V]) Delete(key K)
- func (m *SyncMap[K, V]) DeleteAll()
- func (m *SyncMap[K, V]) DeletePrefix(prefix string)
- func (m *SyncMap[K, V]) DeleteSuffix(suffix string)
- func (m *SyncMap[K, V]) Load(key K) (V, bool)
- func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *SyncMap[K, V]) LoadOrStoreFunc(key K, valueFunc func(k K) (V, bool)) (actual V, loaded bool)
- func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)
- func (m *SyncMap[K, V]) Store(key K, value V)
- func (m *SyncMap[K, V]) Swap(key K, newval V) (oldval V, swapped bool)
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 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]) 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 (*Map[K, V]) LoadAndDelete ¶
func (m *Map[K, V]) LoadAndDelete(key K) (V, bool)
LoadAndDelete 获取键的值并删除键值对。
func (*Map[K, V]) LoadAndDeleteIf ¶
LoadAndDeleteIf 获取键的值并删除键值对,条件性删除 返回值第二个参数 如果 == true,表示删除成功
func (*Map[K, V]) LoadAndRefresh ¶
func (*Map[K, V]) LoadOrStore ¶
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 ¶
LoadWithExpiry 获取键的值和过期时间
func (*Map[K, V]) SetOnExpired ¶
func (m *Map[K, V]) SetOnExpired(onExpired OnExpired[K])
SetOnExpired 设置过期回调函数。
type Resource ¶
type Resource[T any] struct { // contains filtered or unexported fields }
func NewResource ¶
NewResource 创建一个原子操作的资源 注意这个资源不是线程安全的 虽然原子操作不会出现数据竞争,但是数据读取出来后的比较不是线程安全的。可能真实值已经被其他线程修改了
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 ¶
CompareAndDelete 比较键的值与提供的旧值, 如果相等,则删除该项。 它返回是否成功进行了删除。
func (*SyncMap[K, V]) CompareAndSwap ¶
CompareAndSwap 比较键的值与提供的旧值, 如果相等,则将其替换为新值。 它返回是否成功进行了替换。
func (*SyncMap[K, V]) DeletePrefix ¶
func (*SyncMap[K, V]) DeleteSuffix ¶
func (*SyncMap[K, V]) LoadAndDelete ¶
LoadAndDelete 删除键的值,返回之前的值(如果有)。 loaded 结果为 true 表示键存在。
func (*SyncMap[K, V]) LoadOrStore ¶
LoadOrStore 返回存在的键的值(如果存在)。 否则,它存储并返回给定的值。 loaded 结果为 true 表示找到了该值。