memory

package
v3.11.2 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: MIT Imports: 7 Imported by: 144

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewStore

func NewStore() limiter.Store

NewStore creates a new instance of memory store with defaults.

func NewStoreWithOptions

func NewStoreWithOptions(options limiter.StoreOptions) limiter.Store

NewStoreWithOptions creates a new instance of memory store with options.

Types

type Cache

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

Cache contains a collection of counters.

func (*Cache) Clean

func (cache *Cache) Clean()

Clean will deleted any expired keys.

func (*Cache) Delete added in v3.8.0

func (cache *Cache) Delete(key string)

Delete deletes the value for a key.

func (*Cache) Get

func (cache *Cache) Get(key string, duration time.Duration) (int64, time.Time)

Get returns key's value and expiration.

func (*Cache) Increment

func (cache *Cache) Increment(key string, value int64, duration time.Duration) (int64, time.Time)

Increment increments given value on key. If key is undefined or expired, it will create it.

func (*Cache) Load added in v3.8.0

func (cache *Cache) Load(key string) (*Counter, bool)

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

func (*Cache) LoadOrStore added in v3.8.0

func (cache *Cache) LoadOrStore(key string, counter *Counter) (*Counter, bool)

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

func (*Cache) Range added in v3.8.0

func (cache *Cache) Range(handler func(key string, counter *Counter))

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

func (*Cache) Reset added in v3.3.0

func (cache *Cache) Reset(key string, duration time.Duration) (int64, time.Time)

Reset changes the key's value and resets the expiration.

func (*Cache) Store added in v3.8.0

func (cache *Cache) Store(key string, counter *Counter)

Store sets the counter for a key.

type CacheWrapper

type CacheWrapper struct {
	*Cache
}

CacheWrapper is used to ensure that the underlying cleaner goroutine used to clean expired keys will not prevent Cache from being garbage collected.

func NewCache

func NewCache(cleanInterval time.Duration) *CacheWrapper

NewCache returns a new cache.

type Counter

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

Counter is a simple counter with an expiration.

func (*Counter) Expiration

func (counter *Counter) Expiration() int64

Expiration returns the counter expiration.

func (*Counter) Expired

func (counter *Counter) Expired() bool

Expired returns true if the counter has expired.

func (*Counter) Increment added in v3.8.0

func (counter *Counter) Increment(value int64, expiration int64) (int64, int64)

Increment increments given value on this counter. If the counter is expired, it will use the given expiration. It returns its current value and expiration.

func (*Counter) Load added in v3.8.0

func (counter *Counter) Load(expiration int64) (int64, int64)

Load returns the value and the expiration of this counter. If the counter is expired, it will use the given expiration.

func (*Counter) Value

func (counter *Counter) Value() int64

Value returns the counter current value.

type Store

type Store struct {
	// Prefix used for the key.
	Prefix string
	// contains filtered or unexported fields
}

Store is the in-memory store.

func (*Store) Get

func (store *Store) Get(ctx context.Context, key string, rate limiter.Rate) (limiter.Context, error)

Get returns the limit for given identifier.

func (*Store) Increment added in v3.9.0

func (store *Store) Increment(ctx context.Context, key string, count int64, rate limiter.Rate) (limiter.Context, error)

Increment increments the limit by given count & returns the new limit value for given identifier.

func (*Store) Peek

func (store *Store) Peek(ctx context.Context, key string, rate limiter.Rate) (limiter.Context, error)

Peek returns the limit for given identifier, without modification on current values.

func (*Store) Reset added in v3.3.0

func (store *Store) Reset(ctx context.Context, key string, rate limiter.Rate) (limiter.Context, error)

Reset returns the limit for given identifier.

Jump to

Keyboard shortcuts

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