Documentation
¶
Overview ¶
Package client is the Go client for cache-boy (and any RESP server).
c, err := client.Dial("127.0.0.1:6380", client.Password("secret"))
defer c.Close()
c.Set(ctx, "greeting", "hello", client.SetOptions{TTL: time.Minute})
v, err := c.Get(ctx, "greeting")
A Client is safe for concurrent use; it keeps a pool of connections. Do runs any command; typed helpers cover common and cache-boy commands.
Index ¶
- Constants
- Variables
- func Args(args ...any) ([]string, error)
- type CacheGetOptions
- type CacheResult
- type Client
- func (c *Client) CacheGet(ctx context.Context, key string, opts ...CacheGetOptions) (CacheResult, error)
- func (c *Client) CacheRelease(ctx context.Context, key string) (bool, error)
- func (c *Client) Close() error
- func (c *Client) Del(ctx context.Context, keys ...string) (int64, error)
- func (c *Client) Do(ctx context.Context, args ...any) (Value, error)
- func (c *Client) Eval(ctx context.Context, script string, keys []string, args ...any) (Value, error)
- func (c *Client) Exists(ctx context.Context, keys ...string) (int64, error)
- func (c *Client) Expire(ctx context.Context, key string, ttl time.Duration) (bool, error)
- func (c *Client) Get(ctx context.Context, key string) (string, error)
- func (c *Client) HGetAll(ctx context.Context, key string) (map[string]string, error)
- func (c *Client) HSet(ctx context.Context, key string, fields map[string]any) (int64, error)
- func (c *Client) Incr(ctx context.Context, key string) (int64, error)
- func (c *Client) KeyWatch(ctx context.Context, patterns ...string) (*Subscription, error)
- func (c *Client) LPush(ctx context.Context, key string, values ...any) (int64, error)
- func (c *Client) Lock(ctx context.Context, name string, ttl time.Duration, opts ...LockOptions) (*Lock, error)
- func (c *Client) PSubscribe(ctx context.Context, patterns ...string) (*Subscription, error)
- func (c *Client) Pipeline() *Pipeline
- func (c *Client) Publish(ctx context.Context, channel string, message any) (int64, error)
- func (c *Client) QAck(ctx context.Context, queue, id string) (bool, error)
- func (c *Client) QNack(ctx context.Context, queue, id string, delay time.Duration) (bool, error)
- func (c *Client) QPop(ctx context.Context, queue string, opts ...QPopOptions) (*Job, error)
- func (c *Client) QPush(ctx context.Context, queue string, payload any, opts ...QPushOptions) (string, error)
- func (c *Client) RPop(ctx context.Context, key string) (string, error)
- func (c *Client) RateLimit(ctx context.Context, key string, limit int64, period time.Duration, ...) (RateLimitResult, error)
- func (c *Client) SAdd(ctx context.Context, key string, members ...any) (int64, error)
- func (c *Client) SMembers(ctx context.Context, key string) ([]string, error)
- func (c *Client) Set(ctx context.Context, key string, value any, opts ...SetOptions) (bool, error)
- func (c *Client) Subscribe(ctx context.Context, channels ...string) (*Subscription, error)
- func (c *Client) TTL(ctx context.Context, key string) (time.Duration, error)
- func (c *Client) TagAdd(ctx context.Context, key string, tags ...string) (int64, error)
- func (c *Client) TagInvalidate(ctx context.Context, tags ...string) (int64, error)
- func (c *Client) Wrap(ctx context.Context, key string, ttl time.Duration, ...) (string, error)
- func (c *Client) ZAdd(ctx context.Context, key string, members ...Z) (int64, error)
- func (c *Client) ZRange(ctx context.Context, key string, start, stop int64) ([]string, error)
- func (c *Client) ZRangeWithScores(ctx context.Context, key string, start, stop int64) ([]Z, error)
- type Error
- type Job
- type Lock
- type LockOptions
- type Message
- type Option
- type Pipeline
- type QPopOptions
- type QPushOptions
- type RateLimitResult
- type SetOptions
- type Subscription
- type Value
- type Z
Constants ¶
const ( CacheHit = "hit" CacheCompute = "compute" CacheTimeout = "timeout" )
CacheGet statuses.
const NoExpiry time.Duration = -1
NoExpiry is returned by TTL for a key without a TTL.
Variables ¶
var ErrClosed = errors.New("cache-boy: client closed")
ErrClosed is returned after Close.
var ErrNil = errors.New("cache-boy: nil")
ErrNil is returned by typed helpers when the server replies null (missing key...).
var ErrNotAcquired = errors.New("cache-boy: lock not acquired")
ErrNotAcquired is returned by Lock when the lock is held by someone else.
Functions ¶
Types ¶
type CacheGetOptions ¶
type CacheGetOptions struct {
Lock time.Duration // compute lock duration (server default 10s)
Wait time.Duration // how long to wait for another computer (server default 5s)
}
CacheGetOptions are the options of CacheGet.
type CacheResult ¶
type CacheResult struct {
Status string // CacheHit, CacheCompute or CacheTimeout
Value string // set when Status is CacheHit
}
CacheResult is the reply of CACHE.GET.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a pool of connections to one server.
func Dial ¶
Dial connects to addr ("host:port") and checks the connection (and credentials) by opening the first pooled connection.
func (*Client) CacheGet ¶
func (c *Client) CacheGet(ctx context.Context, key string, opts ...CacheGetOptions) (CacheResult, error)
CacheGet runs CACHE.GET. Prefer Wrap, which implements the whole protocol.
func (*Client) CacheRelease ¶
CacheRelease gives up the compute lock taken by CACHE.GET.
func (*Client) Do ¶
Do runs one command and returns its reply. An error reply is returned as both the Value and an Error. A null reply is returned as a null Value with a nil error (typed helpers turn it into ErrNil).
func (*Client) Eval ¶
func (c *Client) Eval(ctx context.Context, script string, keys []string, args ...any) (Value, error)
Eval runs a server-side JavaScript script (see docs/features.md).
func (*Client) KeyWatch ¶
KeyWatch streams changes of keys matching the patterns: each Message has Channel = key and Payload = operation ("set", "del", "expired"...).
func (*Client) Lock ¶
func (c *Client) Lock(ctx context.Context, name string, ttl time.Duration, opts ...LockOptions) (*Lock, error)
Lock acquires name for ttl, or returns ErrNotAcquired.
func (*Client) PSubscribe ¶
PSubscribe listens to channels matching glob patterns.
func (*Client) QPop ¶
QPop takes the next job, or returns ErrNil when none is ready (after blocking, if asked). Blocking honours ctx cancellation.
func (*Client) QPush ¶
func (c *Client) QPush(ctx context.Context, queue string, payload any, opts ...QPushOptions) (string, error)
QPush enqueues a job and returns its id.
func (*Client) RateLimit ¶
func (c *Client) RateLimit(ctx context.Context, key string, limit int64, period time.Duration, cost ...int64) (RateLimitResult, error)
RateLimit consumes cost (default 1) from a GCRA limit of `limit` per `period` for key.
func (*Client) Set ¶
Set stores value at key. It returns false (and no error) when NX/XX prevented the write.
func (*Client) TTL ¶
TTL returns the remaining time to live, NoExpiry, or ErrNil if the key doesn't exist.
func (*Client) TagInvalidate ¶
TagInvalidate deletes every key carrying any of the tags; returns the count.
func (*Client) Wrap ¶
func (c *Client) Wrap(ctx context.Context, key string, ttl time.Duration, fn func(ctx context.Context) (string, error)) (string, error)
Wrap returns the cached value of key, or computes it with fn exactly once across all clients (others wait for the result):
- hit: the cached value is returned.
- compute: fn runs; its result is stored with SET key value PX ttl. If fn fails, the compute lock is released (CACHE.RELEASE) and the error returned.
- timeout (another client took too long): fn runs and its result is returned without being stored.
type Error ¶
type Error string
Error is an error reply from the server, such as "WRONGTYPE ...".
type Job ¶
type Job struct {
Queue string
ID string
Payload string
Attempt int64
// contains filtered or unexported fields
}
Job is a job taken with QPop. Ack or Nack it when done.
type Lock ¶
Lock is a held distributed lock. Token is the fencing token: pass it along with writes so stale holders can be rejected.
type LockOptions ¶
type LockOptions struct {
Wait time.Duration // how long to wait for the lock (0 = don't wait)
Owner string // shown in LOCK.INFO
}
LockOptions are the options of Lock.
type Message ¶
type Message struct {
Kind string // "message", "pmessage", "keyevent", or another push kind
Pattern string // for pmessage
Channel string // channel, or the key for keyevent
Payload string // message, or the operation (set, del, expired...) for keyevent
Raw Value
}
Message is one push received by a Subscription.
type Option ¶
type Option func(*options)
Option configures Dial.
func DialTimeout ¶
DialTimeout bounds connecting and the handshake (default 5s).
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline queues commands and sends them in one round-trip.
p := c.Pipeline()
p.Do("SET", "a", 1)
p.Do("INCR", "a")
replies, err := p.Exec(ctx)
type QPopOptions ¶
type QPopOptions struct {
Visibility time.Duration // re-deliver if not acked within this time (server default 30s)
Block time.Duration // wait up to this long for a job
Forever bool // wait until a job arrives or ctx ends (BLOCK 0)
}
QPopOptions are the options of QPop.
type QPushOptions ¶
type QPushOptions struct {
Delay time.Duration // deliver later
Retries int // max retries before the dead-letter list (0 = server default, 3)
ID string // idempotency id; a duplicate is ignored
}
QPushOptions are the options of QPush.
type RateLimitResult ¶
type RateLimitResult struct {
Allowed bool
Remaining int64
RetryAfter time.Duration // 0 when allowed
ResetAfter time.Duration
}
RateLimitResult is the reply of RATELIMIT.
type SetOptions ¶
type SetOptions struct {
TTL time.Duration // EX/PX; 0 = no expiry
NX bool // only set if the key doesn't exist
XX bool // only set if the key exists
Tags []string // cache-boy tags (see TagInvalidate)
}
SetOptions are the options of Set.
type Subscription ¶
type Subscription struct {
C <-chan Message
// contains filtered or unexported fields
}
Subscription receives pushes on its own connection. Read C until it is closed (after Close, ctx cancellation, or a connection error; see Err).
func (*Subscription) Close ¶
func (s *Subscription) Close() error
Close ends the subscription and closes its connection.
func (*Subscription) Err ¶
func (s *Subscription) Err() error
Err returns the connection error that ended the subscription, if any.