collection

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 12 Imported by: 262

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed   = errors.New("TimingWheel is closed already")
	ErrArgument = errors.New("incorrect task argument")
)

Functions

This section is empty.

Types

type Bucket

type Bucket[T Numerical] struct {
	Sum   T
	Count int64
}

Bucket defines the bucket that holds sum and num of additions.

func (*Bucket[T]) Add added in v1.6.5

func (b *Bucket[T]) Add(v T)

func (*Bucket[T]) Reset added in v1.6.5

func (b *Bucket[T]) Reset()

type BucketInterface added in v1.6.5

type BucketInterface[T Numerical] interface {
	Add(v T)
	Reset()
}

BucketInterface is the interface that defines the buckets.

type Cache

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

A Cache object is an in-memory cache.

func NewCache

func NewCache(expire time.Duration, opts ...CacheOption) (*Cache, error)

NewCache returns a Cache with given expire.

func (*Cache) Del

func (c *Cache) Del(key string)

Del deletes the item with the given key from c.

func (*Cache) Get

func (c *Cache) Get(key string) (any, bool)

Get returns the item with the given key from c.

func (*Cache) Set

func (c *Cache) Set(key string, value any)

Set sets value into c with key.

func (*Cache) SetWithExpire added in v1.3.3

func (c *Cache) SetWithExpire(key string, value any, expire time.Duration)

SetWithExpire sets value into c with key and expire with the given value.

func (*Cache) Take

func (c *Cache) Take(key string, fetch func() (any, error)) (any, error)

Take returns the item with the given key. If the item is in c, return it directly. If not, use fetch method to get the item, set into c and return it.

type CacheOption

type CacheOption func(cache *Cache)

CacheOption defines the method to customize a Cache.

func WithLimit

func WithLimit(limit int) CacheOption

WithLimit customizes a Cache with items up to limit.

func WithName

func WithName(name string) CacheOption

WithName customizes a Cache with the given name.

type Execute

type Execute func(key, value any)

Execute defines the method to execute the task.

type Numerical added in v1.6.5

type Numerical = mathx.Numerical

Numerical is the interface that restricts the numerical type.

type Queue

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

A Queue is a FIFO queue.

func NewQueue

func NewQueue(size int) *Queue

NewQueue returns a Queue object.

func (*Queue) Empty

func (q *Queue) Empty() bool

Empty checks if q is empty.

func (*Queue) Put

func (q *Queue) Put(element any)

Put puts element into q at the last position.

func (*Queue) Take

func (q *Queue) Take() (any, bool)

Take takes the first element out of q if not empty.

type Ring

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

A Ring can be used as fixed size ring.

func NewRing

func NewRing(n int) *Ring

NewRing returns a Ring object with the given size n.

func (*Ring) Add

func (r *Ring) Add(v any)

Add adds v into r.

func (*Ring) Take

func (r *Ring) Take() []any

Take takes all items from r.

type RollingWindow

type RollingWindow[T Numerical, B BucketInterface[T]] struct {
	// contains filtered or unexported fields
}

RollingWindow defines a rolling window to calculate the events in buckets with the time interval.

func NewRollingWindow

func NewRollingWindow[T Numerical, B BucketInterface[T]](newBucket func() B, size int,
	interval time.Duration, opts ...RollingWindowOption[T, B]) *RollingWindow[T, B]

NewRollingWindow returns a RollingWindow that with size buckets and time interval, use opts to customize the RollingWindow.

func (*RollingWindow[T, B]) Add

func (rw *RollingWindow[T, B]) Add(v T)

Add adds value to current bucket.

func (*RollingWindow[T, B]) Reduce

func (rw *RollingWindow[T, B]) Reduce(fn func(b B))

Reduce runs fn on all buckets, ignore current bucket if ignoreCurrent was set.

type RollingWindowOption

type RollingWindowOption[T Numerical, B BucketInterface[T]] func(rollingWindow *RollingWindow[T, B])

RollingWindowOption let callers customize the RollingWindow.

func IgnoreCurrentBucket

func IgnoreCurrentBucket[T Numerical, B BucketInterface[T]]() RollingWindowOption[T, B]

IgnoreCurrentBucket lets the Reduce call ignore current bucket.

type SafeMap

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

SafeMap provides a map alternative to avoid memory leak. This implementation is not needed until issue below fixed. https://github.com/golang/go/issues/20135

func NewSafeMap

func NewSafeMap() *SafeMap

NewSafeMap returns a SafeMap.

func (*SafeMap) Del

func (m *SafeMap) Del(key any)

Del deletes the value with the given key from m.

func (*SafeMap) Get

func (m *SafeMap) Get(key any) (any, bool)

Get gets the value with the given key from m.

func (*SafeMap) Range added in v1.4.1

func (m *SafeMap) Range(f func(key, val any) bool)

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

func (*SafeMap) Set

func (m *SafeMap) Set(key, value any)

Set sets the value into m with the given key.

func (*SafeMap) Size

func (m *SafeMap) Size() int

Size returns the size of m.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a type-safe generic set collection. It's not thread-safe, use with synchronization for concurrent access.

func NewSet

func NewSet[T comparable]() *Set[T]

NewSet returns a new type-safe set.

func (*Set[T]) Add

func (s *Set[T]) Add(items ...T)

Add adds items to the set. Duplicates are automatically ignored.

func (*Set[T]) Clear added in v1.9.0

func (s *Set[T]) Clear()

Clear removes all items from the set.

func (*Set[T]) Contains

func (s *Set[T]) Contains(item T) bool

Contains checks if an item exists in the set.

func (*Set[T]) Count

func (s *Set[T]) Count() int

Count returns the number of items in the set.

func (*Set[T]) Keys

func (s *Set[T]) Keys() []T

Keys returns all elements in the set as a slice.

func (*Set[T]) Remove

func (s *Set[T]) Remove(item T)

Remove removes an item from the set.

type TimingWheel

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

A TimingWheel is a timing wheel object to schedule tasks.

func NewTimingWheel

func NewTimingWheel(interval time.Duration, numSlots int, execute Execute) (*TimingWheel, error)

NewTimingWheel returns a TimingWheel.

func NewTimingWheelWithTicker added in v1.4.4

func NewTimingWheelWithTicker(interval time.Duration, numSlots int, execute Execute,
	ticker timex.Ticker) (*TimingWheel, error)

NewTimingWheelWithTicker returns a TimingWheel with the given ticker.

func (*TimingWheel) Drain

func (tw *TimingWheel) Drain(fn func(key, value any)) error

Drain drains all items and executes them.

func (*TimingWheel) MoveTimer

func (tw *TimingWheel) MoveTimer(key any, delay time.Duration) error

MoveTimer moves the task with the given key to the given delay.

func (*TimingWheel) RemoveTimer

func (tw *TimingWheel) RemoveTimer(key any) error

RemoveTimer removes the task with the given key.

func (*TimingWheel) SetTimer

func (tw *TimingWheel) SetTimer(key, value any, delay time.Duration) error

SetTimer sets the task value with the given key to the delay.

func (*TimingWheel) Stop

func (tw *TimingWheel) Stop()

Stop stops tw. No more actions after stopping a TimingWheel.

Jump to

Keyboard shortcuts

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