conc

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: MIT Imports: 12 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
}

Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.

The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.

The zero Map is empty and ready for use. A Map must not be copied after first use.

In the terminology of the Go memory model, Map arranges that a write operation “synchronizes before” any read operation that observes the effect of the write, where read and write operations are defined as follows. Load, LoadAndDelete, LoadOrStore, Swap, CompareAndSwap, and CompareAndDelete are read operations; Delete, LoadAndDelete, Store, and Swap are write operations; LoadOrStore is a write operation when it returns loaded set to false; CompareAndSwap is a write operation when it returns swapped set to true; and CompareAndDelete is a write operation when it returns deleted set to true.

func (*Map[K, V]) Clear

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

Clear deletes all the entries, resulting in an empty Map.

func (*Map[K, V]) CompareAndDelete

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

CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.

If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).

func (*Map[K, V]) CompareAndSwap

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

CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.

func (*Map[K, V]) Delete

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

Delete deletes the value for a key.

func (*Map[K, V]) Len

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

func (*Map[K, V]) Load

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

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*Map[K, V]) LoadAndDelete

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

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*Map[K, V]) LoadOrStore

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

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map[K, V]) Range

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

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*Map[K, V]) Store

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

Store sets the value for a key.

func (*Map[K, V]) Swap

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

Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.

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]) 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]

SwichFixedTimeCleanup 固定时间清除全部数据 参数 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