store

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package store provides storage backends for rate limiting data.

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyTooLong = errors.New("ratelimiter: key too long")

ErrKeyTooLong is returned when a key exceeds the maximum allowed length.

View Source
var ErrStoreFull = errors.New("ratelimiter: store capacity exceeded")

ErrStoreFull is returned when the storage capacity is reached.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Value     interface{}
	ExpiresAt time.Time
}

Entry represents a stored value with its expiration time.

func (Entry) IsExpired

func (e Entry) IsExpired() bool

IsExpired checks if the entry has expired.

type MemoryStore

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

MemoryStore is an in-memory implementation of the Store interface. It provides automatic cleanup of expired entries.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new in-memory store with default configuration.

func NewMemoryStoreWithConfig

func NewMemoryStoreWithConfig(config MemoryStoreConfig) *MemoryStore

NewMemoryStoreWithConfig creates a new in-memory store with custom configuration.

func (*MemoryStore) Close

func (s *MemoryStore) Close() error

Close stops the cleanup routine and releases resources.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(key string) error

Delete removes a value from the store.

func (*MemoryStore) DeleteWithNamespace

func (s *MemoryStore) DeleteWithNamespace(namespace, key string) error

DeleteWithNamespace removes a value from the store using a namespace and key.

func (*MemoryStore) Get

func (s *MemoryStore) Get(key string) (interface{}, bool)

Get retrieves a value from the store.

func (*MemoryStore) GetWithNamespace

func (s *MemoryStore) GetWithNamespace(namespace, key string) (interface{}, bool)

GetWithNamespace retrieves a value from the store using a namespace and key.

func (*MemoryStore) Len

func (s *MemoryStore) Len() int

Len returns the number of entries in the store (including expired ones).

func (*MemoryStore) Set

func (s *MemoryStore) Set(key string, value interface{}, ttl time.Duration) error

Set stores a value with an optional TTL.

func (*MemoryStore) SetWithNamespace

func (s *MemoryStore) SetWithNamespace(namespace, key string, value interface{}, ttl time.Duration) error

SetWithNamespace stores a value with namespace using an optional TTL.

func (*MemoryStore) UpdateTTL added in v0.0.3

func (s *MemoryStore) UpdateTTL(key string, ttl time.Duration) error

UpdateTTL updates the expiration of a key without changing its value.

func (*MemoryStore) UpdateTTLWithNamespace added in v0.0.3

func (s *MemoryStore) UpdateTTLWithNamespace(namespace, key string, ttl time.Duration) error

UpdateTTLWithNamespace updates the expiration of a namespaced key without changing its value.

type MemoryStoreConfig

type MemoryStoreConfig struct {
	// CleanupInterval is how often to run the cleanup routine.
	// Default is 1 minute.
	CleanupInterval time.Duration
	// MaxEntries is the maximum number of keys to store.
	// Default is 1,000,000.
	MaxEntries int
	// MaxKeySize is the maximum length of a key in bytes.
	// Default is 4096.
	MaxKeySize int
}

MemoryStoreConfig holds configuration for MemoryStore.

func DefaultMemoryStoreConfig

func DefaultMemoryStoreConfig() MemoryStoreConfig

DefaultMemoryStoreConfig returns sensible defaults for MemoryStore.

type NamespacedStore

type NamespacedStore interface {
	Store

	// GetWithNamespace retrieves a value from the store using a namespace and key.
	GetWithNamespace(namespace, key string) (interface{}, bool)

	// SetWithNamespace stores a value with namespace using an optional TTL.
	SetWithNamespace(namespace, key string, value interface{}, ttl time.Duration) error

	// DeleteWithNamespace removes a value from the store using a namespace and key.
	DeleteWithNamespace(namespace, key string) error
}

NamespacedStore extends Store with namespace support to avoid string concatenation allocations.

type NamespacedTTLStore added in v0.0.3

type NamespacedTTLStore interface {
	NamespacedStore
	// UpdateTTLWithNamespace updates the expiration of a namespaced key.
	UpdateTTLWithNamespace(namespace, key string, ttl time.Duration) error
}

NamespacedTTLStore extends NamespacedStore with UpdateTTL support.

type Store

type Store interface {
	// Get retrieves a value from the store.
	// Returns the value and true if found, nil and false otherwise.
	Get(key string) (interface{}, bool)

	// Set stores a value with an optional TTL.
	// If ttl is 0, the value never expires.
	Set(key string, value interface{}, ttl time.Duration) error

	// Delete removes a value from the store.
	Delete(key string) error

	// Close releases any resources held by the store.
	Close() error
}

Store defines the storage interface for rate limiting data. Implementations must be safe for concurrent use.

type TTLStore added in v0.0.3

type TTLStore interface {
	Store
	// UpdateTTL updates the expiration of a key without changing its value.
	UpdateTTL(key string, ttl time.Duration) error
}

TTLStore extends Store with the ability to update TTL without changing value.

Jump to

Keyboard shortcuts

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