Documentation
¶
Index ¶
- Variables
- type Lock
- type LockOption
- type Locker
- type MemoryStore
- func (m *MemoryStore) Delete(ctx context.Context, key string) error
- func (m *MemoryStore) Flush(ctx context.Context) error
- func (m *MemoryStore) Get(ctx context.Context, key string) (string, error)
- func (m *MemoryStore) Has(ctx context.Context, key string) (bool, error)
- func (m *MemoryStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- type RedisLock
- type RedisLocker
- type RedisStore
- func (s *RedisStore) Delete(ctx context.Context, key string) error
- func (s *RedisStore) Flush(ctx context.Context) error
- func (s *RedisStore) Get(ctx context.Context, key string) (string, error)
- func (s *RedisStore) GetMany(ctx context.Context, keys []string) (map[string]string, error)
- func (s *RedisStore) Has(ctx context.Context, key string) (bool, error)
- func (s *RedisStore) Remember(ctx context.Context, key string, ttl time.Duration, fn func() (string, error)) (string, error)
- func (s *RedisStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (s *RedisStore) SetMany(ctx context.Context, items map[string]any, ttl time.Duration) error
- type Store
Constants ¶
This section is empty.
Variables ¶
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") )
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.
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.
type RedisLock ¶
type RedisLock struct {
// contains filtered or unexported fields
}
RedisLock is a Redis-backed distributed lock.
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.
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) 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.
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 ¶
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 ¶
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")