cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLockNotAcquired is returned when a lock is already held elsewhere.
	ErrLockNotAcquired = errors.New("astra/lock: lock is already held")
	// ErrLockNotOwned is returned when a lock operation is attempted by a non-owner.
	ErrLockNotOwned = errors.New("astra/lock: lock is not owned by this instance")
)
View Source
var ErrCacheMiss = errors.New("astra/cache: key not found")

ErrCacheMiss is returned when a cache key does not exist.

Functions

This section is empty.

Types

type Lock

type Lock interface {
	// Release releases the lock if the caller still owns it.
	Release(ctx context.Context) error
	// Extend refreshes the lock TTL if the caller still owns it.
	Extend(ctx context.Context, ttl time.Duration) error
}

Lock represents an acquired distributed lock.

type LockOption

type LockOption func(*lockOptions)

LockOption configures lock acquisition behavior.

func WithRetry

func WithRetry(attempts int, delay time.Duration) LockOption

WithRetry retries lock acquisition a fixed number of times with a delay.

type Locker

type Locker interface {
	// Acquire attempts to acquire a lock with the provided TTL.
	Acquire(ctx context.Context, key string, ttl time.Duration, opts ...LockOption) (Lock, error)
}

Locker acquires distributed locks.

type MemoryStore

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

MemoryStore implements Store with process-local memory.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new in-memory cache store.

func (*MemoryStore) Delete

func (m *MemoryStore) Delete(ctx context.Context, key string) error

Delete removes a value from memory.

func (*MemoryStore) Flush

func (m *MemoryStore) Flush(ctx context.Context) error

Flush clears all in-memory values.

func (*MemoryStore) Get

func (m *MemoryStore) Get(ctx context.Context, key string) (string, error)

Get retrieves a value from memory.

func (*MemoryStore) Has

func (m *MemoryStore) Has(ctx context.Context, key string) (bool, error)

Has reports whether a value exists in memory.

func (*MemoryStore) Set

func (m *MemoryStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores a value in memory.

type RedisLock

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

RedisLock is a Redis-backed distributed lock.

func (*RedisLock) Extend

func (l *RedisLock) Extend(ctx context.Context, ttl time.Duration) error

Extend refreshes the lock TTL if it is still owned by the caller.

func (*RedisLock) Release

func (l *RedisLock) Release(ctx context.Context) error

Release releases the lock if it is still owned by the caller.

type RedisLocker

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

RedisLocker acquires distributed locks backed by Redis.

func NewRedisLocker

func NewRedisLocker(client goredis.UniversalClient, keyPrefix string) *RedisLocker

NewRedisLocker creates a new Redis-backed locker.

func (*RedisLocker) Acquire

func (l *RedisLocker) Acquire(ctx context.Context, key string, ttl time.Duration, opts ...LockOption) (Lock, error)

Acquire attempts to acquire a distributed lock.

type RedisStore

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

RedisStore is a Redis-backed implementation of Store.

func NewRedisStore

func NewRedisStore(client goredis.UniversalClient, keyPrefix string) *RedisStore

NewRedisStore creates a new Redis-backed cache store.

func (*RedisStore) Delete

func (s *RedisStore) Delete(ctx context.Context, key string) error

Delete removes a value from Redis.

func (*RedisStore) Flush

func (s *RedisStore) Flush(ctx context.Context) error

Flush deletes only keys owned by this store prefix.

func (*RedisStore) Get

func (s *RedisStore) Get(ctx context.Context, key string) (string, error)

Get retrieves a value from Redis.

func (*RedisStore) GetMany

func (s *RedisStore) GetMany(ctx context.Context, keys []string) (map[string]string, error)

GetMany fetches many keys in a single pipeline round trip.

func (*RedisStore) Has

func (s *RedisStore) Has(ctx context.Context, key string) (bool, error)

Has reports whether a value exists in Redis.

func (*RedisStore) Remember

func (s *RedisStore) Remember(ctx context.Context, key string, ttl time.Duration, fn func() (string, error)) (string, error)

Remember returns a cached value or computes, stores, and returns it.

func (*RedisStore) Set

func (s *RedisStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores a value in Redis.

func (*RedisStore) SetMany

func (s *RedisStore) SetMany(ctx context.Context, items map[string]any, ttl time.Duration) error

SetMany stores many values in a single pipeline round trip.

type Store

type Store interface {
	// Get retrieves a cached value.
	Get(ctx context.Context, key string) (string, error)
	// Set stores a value for the provided TTL. A zero TTL stores the value forever.
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	// Delete removes a value from the cache.
	Delete(ctx context.Context, key string) error
	// Has reports whether a value exists in the cache.
	Has(ctx context.Context, key string) (bool, error)
	// Flush removes every key owned by the store.
	Flush(ctx context.Context) error
}

Store defines the cache contract used throughout Astra.

func NewMemoryStandalone

func NewMemoryStandalone() Store

NewMemoryStandalone creates an in-memory cache Store with zero external dependencies. Suitable for testing, CLI tools, and services that don't need distributed caching.

Note: data is not persisted across process restarts.

func NewRedisStandalone

func NewRedisStandalone(addr, password string, db int) (Store, error)

NewRedisStandalone creates a Redis-backed cache Store using only a host address, password, and DB index. Requires no engine.App or extra framework setup.

store, err := cache.NewRedisStandalone("localhost:6379", "", 0)

func NewRedisStandaloneURL

func NewRedisStandaloneURL(rawURL string) (Store, error)

NewRedisStandaloneURL creates a Redis-backed cache Store from a Redis URL. Supports redis://, rediss://, and redis+sentinel:// schemes.

store, err := cache.NewRedisStandaloneURL("redis://:password@localhost:6379/0")

Jump to

Keyboard shortcuts

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