conc

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

concurrent 主要提供 3 个函数 GoRun,异步执行函数 Wait,等待所有任务执行完毕 UnsafeWaitWithContext,包含超时机制的等待所有任务执行完毕

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheNotFound = errors.New("cache not found")

ErrCacheNotFound 缓存未找到错误

Functions

func DefaultTimer

func DefaultTimer(ctx context.Context, every time.Duration, fn func())

DefaultTimer 首次是 3 秒后执行,每隔 every 执行一次

func GoSafe added in v1.2.2

func GoSafe(fn func())

func Timer

func Timer(ctx context.Context, first, every time.Duration, fn func())

Timer 轮询任务

Types

type Cacher added in v1.2.5

type Cacher interface {
	Set(context.Context, string, any)
	Del(context.Context, string)
	Get(context.Context, string, any) error
	SetNX(context.Context, string, any)
}

Cacher 缓存接口

type DefaultTracer

type DefaultTracer struct{}

func (DefaultTracer) Error

func (DefaultTracer) Error(msg string, args ...any)

type G

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

func New

func New(l Tracer) *G

func (*G) GoRun

func (g *G) GoRun(fn func())

GoRun 异步执行任务 会记录数量,并且可以 wait 等待结束

func (*G) UnsafeWaitWithContext

func (g *G) UnsafeWaitWithContext(ctx context.Context) error

UnsafeWaitWithContext wait 会一直等,此函数会有个超时限制。

func (*G) Wait

func (g *G) Wait()

type Map

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

func NewMap added in v1.2.9

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

NewMap 创建一个新的泛型 Map

func (*Map[K, V]) Clear

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

Clear 清空所有键值对

func (*Map[K, V]) CompareAndDelete

func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool)

CompareAndDelete 比较并删除键值对

func (*Map[K, V]) CompareAndSwap

func (m *Map[K, V]) CompareAndSwap(key K, old, ne V) bool

CompareAndSwap 比较并交换值

func (*Map[K, V]) Delete

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

Delete 删除键值对

func (*Map[K, V]) Keys added in v1.2.9

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

Keys 返回所有键的切片

func (*Map[K, V]) Len

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

Len 返回 map 中键值对的数量 (注意: 这是一个近似值)

func (*Map[K, V]) Load

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

Load 根据键获取值

func (*Map[K, V]) LoadAndDelete

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

LoadAndDelete 获取并删除键值对

func (*Map[K, V]) LoadOrStore

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

LoadOrStore 获取或存储键值对

func (*Map[K, V]) Range

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

Range 遍历所有键值对

func (*Map[K, V]) Store

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

Store 存储键值对

func (*Map[K, V]) Swap

func (m *Map[K, V]) Swap(key K, value V) (previous V, loaded bool)

Swap 交换键对应的值

func (*Map[K, V]) Values added in v1.2.9

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

Values 返回所有值的切片

type TTLCache added in v1.2.5

type TTLCache struct {
	*TTLMap[string, any]
	// contains filtered or unexported fields
}

TTLCache 不使用泛型,是考虑到使用 redis 时,也没办法用泛型 为了统一和代码简洁性,故而使用 any,而非泛型

func NewTTLCache added in v1.2.5

func NewTTLCache(ttl time.Duration) *TTLCache

func (*TTLCache) Del added in v1.2.5

func (t *TTLCache) Del(_ context.Context, key string)

Del 删除缓存值

func (*TTLCache) Get added in v1.2.5

func (t *TTLCache) Get(_ context.Context, key string, dest any) error

Get 获取缓存值,将结果反序列化到 dest 中

func (*TTLCache) Set added in v1.2.5

func (t *TTLCache) Set(_ context.Context, key string, value any)

Set 设置缓存值

func (*TTLCache) SetNX added in v1.2.5

func (t *TTLCache) SetNX(_ context.Context, key string, value any)

SetNX 仅当键不存在时设置值

type TTLMap

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

TTLMap 带有过期时间的 map

func NewTTLMap

func NewTTLMap[K comparable, V any]() *TTLMap[K, V]

NewTTLMap 提供默认的过期删除 也可以使用 SwichFixedTimeCleanup 开启定时清空

func (*TTLMap[K, V]) Clear

func (c *TTLMap[K, V]) Clear()

Clear 清空数据

func (*TTLMap[K, V]) Delete

func (c *TTLMap[K, V]) Delete(key K)

Delete 删除 k/v

func (*TTLMap[K, V]) Dispose added in v1.2.5

func (c *TTLMap[K, V]) Dispose()

Dispose 销毁协程

func (*TTLMap[K, V]) Len

func (c *TTLMap[K, V]) Len() int

Len map 长度

func (*TTLMap[K, V]) Load

func (c *TTLMap[K, V]) Load(key K) (V, bool)

Load 获取未过期的 k/v

func (*TTLMap[K, V]) LoadOrStore

func (c *TTLMap[K, V]) LoadOrStore(key K, value V, ttl time.Duration) (V, bool)

LoadOrStore 第二个参数,true:获取 load 的数据; false:刚存储的数据

func (*TTLMap[K, V]) Range

func (c *TTLMap[K, V]) Range(fn func(key K, value V) bool)

Range 遍历 map

func (*TTLMap[K, V]) SetTickerCleanup added in v1.3.11

func (c *TTLMap[K, V]) SetTickerCleanup(interval time.Duration) *TTLMap[K, V]

SetTickerCleanup 设置定时清除,传 0 表示每 1 秒检测一次,interval 表示检测间隔时间

func (*TTLMap[K, V]) Store

func (c *TTLMap[K, V]) Store(key K, value V, ttl time.Duration)

Store 将在 ttl 后自动删除 k/v

func (*TTLMap[K, V]) SwichFixedTimeClear

func (c *TTLMap[K, V]) SwichFixedTimeClear(afterFn func() time.Duration) *TTLMap[K, V]

SwichFixedTimeClear 固定时间清除全部数据 参数 afterFn 用于获取间隔多久以后执行

type Tracer

type Tracer interface {
	Error(msg string, args ...any)
}

Jump to

Keyboard shortcuts

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