cache

package
v0.1.73 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: MIT Imports: 7 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[Key, Value any] struct {
	// contains filtered or unexported fields
}

Cache is cache struct.

func New

func New[Key, Value any](autoRenew bool) *Cache[Key, Value]

New creates a new cache with auto clean or not.

func (*Cache[Key, Value]) Clear

func (c *Cache[Key, Value]) Clear()

Clear deletes all values in cache.

func (*Cache[Key, Value]) Delete

func (c *Cache[Key, Value]) Delete(key Key)

Delete deletes the value for a key.

func (*Cache[Key, Value]) Get

func (c *Cache[Key, Value]) Get(key Key) (value Value, ok bool)

Get gets cache value by key and whether value was found.

func (*Cache[Key, Value]) Set

func (c *Cache[Key, Value]) Set(key Key, value Value, lifecycle time.Duration, fn func() (Value, error))

Set sets cache value for a key, if fn is presented, this value will regenerate when expired.

func (*Cache[Key, Value]) Swap

func (c *Cache[Key, Value]) Swap(key Key, value Value) (previous Value, 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 Element

type Element[T any] struct {
	// contains filtered or unexported fields
}

Element is an element of a linked list.

func (*Element[T]) Next

func (e *Element[T]) Next() *Element[T]

Next returns the next list element or nil.

func (*Element[T]) Prev

func (e *Element[T]) Prev() *Element[T]

Prev returns the previous list element or nil.

func (*Element[T]) Value

func (e *Element[T]) Value() T

Value returns the value stored with this element.

type List

type List[T any] struct {
	// contains filtered or unexported fields
}

List represents a doubly linked list. The zero value for List is an empty list ready to use.

func NewList

func NewList[T any]() *List[T]

New returns an initialized list.

func (*List[T]) Back

func (l *List[T]) Back() *Element[T]

Back returns the last element of list l or nil if the list is empty.

func (*List[T]) Front

func (l *List[T]) Front() *Element[T]

Front returns the first element of list l or nil if the list is empty.

func (*List[T]) Init

func (l *List[T]) Init() *List[T]

Init initializes or clears list l.

func (*List[T]) InsertAfter

func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T]

InsertAfter inserts a new element e with value v immediately after mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.

func (*List[T]) InsertBefore

func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T]

InsertBefore inserts a new element e with value v immediately before mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.

func (*List[T]) Len

func (l *List[T]) Len() int

Len returns the number of elements of list l. The complexity is O(1).

func (*List[T]) MoveAfter

func (l *List[T]) MoveAfter(e, mark *Element[T])

MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.

func (*List[T]) MoveBefore

func (l *List[T]) MoveBefore(e, mark *Element[T])

MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.

func (*List[T]) MoveToBack

func (l *List[T]) MoveToBack(e *Element[T])

MoveToBack moves element e to the back of list l. If e is not an element of l, the list is not modified. The element must not be nil.

func (*List[T]) MoveToFront

func (l *List[T]) MoveToFront(e *Element[T])

MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.

func (*List[T]) PushBack

func (l *List[T]) PushBack(v T) *Element[T]

PushBack inserts a new element e with value v at the back of list l and returns e.

func (*List[T]) PushBackList

func (l *List[T]) PushBackList(other *List[T])

PushBackList inserts a copy of another list at the back of list l. The lists l and other may be the same. They must not be nil.

func (*List[T]) PushFront

func (l *List[T]) PushFront(v T) *Element[T]

PushFront inserts a new element e with value v at the front of list l and returns e.

func (*List[T]) PushFrontList

func (l *List[T]) PushFrontList(other *List[T])

PushFrontList inserts a copy of another list at the front of list l. The lists l and other may be the same. They must not be nil.

func (*List[T]) Remove

func (l *List[T]) Remove(e *Element[T]) T

Remove removes e from l if e is an element of list l. It returns the element value e.Value. The element must not be nil.

type Map

type Map[Key, Value any] struct {
	// contains filtered or unexported fields
}

func NewMap

func NewMap[Key, Value any]() *Map[Key, Value]

func (*Map[Key, Value]) Clear

func (m *Map[Key, Value]) Clear()

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

func (*Map[Key, Value]) CompareAndDelete

func (m *Map[Key, Value]) CompareAndDelete(key Key, old Value) (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[Key, Value]) CompareAndSwap

func (m *Map[Key, Value]) CompareAndSwap(key Key, old Value, new Value) 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[Key, Value]) Delete

func (m *Map[Key, Value]) Delete(key Key)

Delete deletes the value for a key.

func (*Map[Key, Value]) Load

func (m *Map[Key, Value]) Load(key Key) (value Value, 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[Key, Value]) LoadAndDelete

func (m *Map[Key, Value]) LoadAndDelete(key Key) (value Value, 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[Key, Value]) LoadOrStore

func (m *Map[Key, Value]) LoadOrStore(key Key, value Value) (actual Value, 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[Key, Value]) Range

func (m *Map[Key, Value]) Range(f func(Key, Value) 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[Key, Value]) Store

func (m *Map[Key, Value]) Store(key Key, value Value)

Store sets the value for a key.

func (*Map[Key, Value]) Swap

func (m *Map[Key, Value]) Swap(key Key, value Value) (previous Value, 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 Ring

type Ring[T any] struct {
	// contains filtered or unexported fields
}

A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value.

func NewRing

func NewRing[T any](n int) *Ring[T]

NewRing creates a ring of n elements.

func (*Ring[T]) Do

func (r *Ring[T]) Do(f func(*T))

Do calls function f on each element of the ring, in forward order. The behavior of Do is undefined if f changes *r.

func (*Ring[T]) Len

func (r *Ring[T]) Len() int

Len computes the number of elements in ring r. It executes in time proportional to the number of elements.

func (r *Ring[T]) Link(s *Ring[T]) *Ring[T]

Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.

If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).

If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion.

func (*Ring[T]) Move

func (r *Ring[T]) Move(n int) *Ring[T]

Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty.

func (*Ring[T]) Next

func (r *Ring[T]) Next() *Ring[T]

Next returns the next ring element. r must not be empty.

func (*Ring[T]) Prev

func (r *Ring[T]) Prev() *Ring[T]

Prev returns the previous ring element. r must not be empty.

func (*Ring[T]) Set

func (r *Ring[T]) Set(v T)
func (r *Ring[T]) Unlink(n int) *Ring[T]

Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty.

func (*Ring[T]) Value

func (r *Ring[T]) Value() *T

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

A Value provides an atomic load and store of a specified typed value. Once Value.Store has been called, a Value must not be copied.

A Value must not be copied after first use.

func NewValue

func NewValue[T any]() *Value[T]

NewValue creates a new instance of Value.

func (*Value[T]) CompareAndSwap

func (v *Value[T]) CompareAndSwap(old, new T) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for the Value.

func (*Value[T]) Load

func (v *Value[T]) Load() (val T, stored bool)

Load returns the value set by the most recent Store and a boolean indicating whether a value was stored. If there has been no call to Store for this Value, it returns the zero value of T and false.

func (*Value[T]) MustLoad

func (v *Value[T]) MustLoad() (val T)

MustLoad returns the value set by the most recent Store. It panics if there has been no call to Store for this Value.

func (*Value[T]) Store

func (v *Value[T]) Store(val T)

Store sets the value of the Value v to val.

func (*Value[T]) Swap

func (v *Value[T]) Swap(new T) (old T, stored bool)

Swap stores the new value into the Value and returns the previous value. If no value was previously stored, it returns the zero value of T and false.

Jump to

Keyboard shortcuts

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