collection

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 11 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	Sum   float64
	Count int64
}

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

type Cache

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

A Cache object is a 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) (interface{}, bool)

Get returns the item with the given key from c.

func (*Cache) Set

func (c *Cache) Set(key string, value interface{})

Set sets value into c with key.

func (*Cache) Take

func (c *Cache) Take(key string, fetch func() (interface{}, error)) (interface{}, 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 interface{})

Execute defines the method to execute the task.

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 interface{})

Put puts element into q at the last position.

func (*Queue) Take

func (q *Queue) Take() (interface{}, 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 interface{})

Add adds v into r.

func (*Ring) Take

func (r *Ring) Take() []interface{}

Take takes all items from r.

type RollingWindow

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

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

func NewRollingWindow

func NewRollingWindow(size int, interval time.Duration, opts ...RollingWindowOption) *RollingWindow

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

func (*RollingWindow) Add

func (rw *RollingWindow) Add(v float64)

Add adds value to current bucket.

func (*RollingWindow) Reduce

func (rw *RollingWindow) Reduce(fn func(b *Bucket))

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

type RollingWindowOption

type RollingWindowOption func(rollingWindow *RollingWindow)

RollingWindowOption let callers customize the RollingWindow.

func IgnoreCurrentBucket

func IgnoreCurrentBucket() RollingWindowOption

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 interface{})

Del deletes the value with the given key from m.

func (*SafeMap) Get

func (m *SafeMap) Get(key interface{}) (interface{}, bool)

Get gets the value with the given key from m.

func (*SafeMap) Set

func (m *SafeMap) Set(key, value interface{})

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 struct {
	// contains filtered or unexported fields
}

Set is not thread-safe, for concurrent use, make sure to use it with synchronization.

func NewSet

func NewSet() *Set

NewSet returns a managed Set, can only put the values with the same type.

func NewUnmanagedSet

func NewUnmanagedSet() *Set

NewUnmanagedSet returns a unmanaged Set, which can put values with different types.

func (*Set) Add

func (s *Set) Add(i ...interface{})

Add adds i into s.

func (*Set) AddInt

func (s *Set) AddInt(ii ...int)

AddInt adds int values ii into s.

func (*Set) AddInt64

func (s *Set) AddInt64(ii ...int64)

AddInt64 adds int64 values ii into s.

func (*Set) AddStr

func (s *Set) AddStr(ss ...string)

AddStr adds string values ss into s.

func (*Set) AddUint

func (s *Set) AddUint(ii ...uint)

AddUint adds uint values ii into s.

func (*Set) AddUint64

func (s *Set) AddUint64(ii ...uint64)

AddUint64 adds uint64 values ii into s.

func (*Set) Contains

func (s *Set) Contains(i interface{}) bool

Contains checks if i is in s.

func (*Set) Count

func (s *Set) Count() int

Count returns the number of items in s.

func (*Set) Keys

func (s *Set) Keys() []interface{}

Keys returns the keys in s.

func (*Set) KeysInt

func (s *Set) KeysInt() []int

KeysInt returns the int keys in s.

func (*Set) KeysInt64

func (s *Set) KeysInt64() []int64

KeysInt64 returns int64 keys in s.

func (*Set) KeysStr

func (s *Set) KeysStr() []string

KeysStr returns string keys in s.

func (*Set) KeysUint

func (s *Set) KeysUint() []uint

KeysUint returns uint keys in s.

func (*Set) KeysUint64

func (s *Set) KeysUint64() []uint64

KeysUint64 returns uint64 keys in s.

func (*Set) Remove

func (s *Set) Remove(i interface{})

Remove removes i from s.

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 (*TimingWheel) Drain

func (tw *TimingWheel) Drain(fn func(key, value interface{}))

Drain drains all items and executes them.

func (*TimingWheel) MoveTimer

func (tw *TimingWheel) MoveTimer(key interface{}, delay time.Duration)

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

func (*TimingWheel) RemoveTimer

func (tw *TimingWheel) RemoveTimer(key interface{})

RemoveTimer removes the task with the given key.

func (*TimingWheel) SetTimer

func (tw *TimingWheel) SetTimer(key, value interface{}, delay time.Duration)

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

func (*TimingWheel) Stop

func (tw *TimingWheel) Stop()

Stop stops tw.

Jump to

Keyboard shortcuts

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