memcached

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package memcached is a zero-dependency cache.Cache backend for Memcached. It speaks the classic ASCII protocol (get / set / delete, with native multi-key get) over a small bounded connection pool, and uses the meta-get command (mg, Memcached 1.6+) to report remaining TTL.

Like drops/cache/redis it imports only the standard library: the wire protocol and pool are implemented here rather than pulled from a third-party client.

c, _ := memcached.New(memcached.Options{Addr: "127.0.0.1:11211"})
defer c.Close()
_ = c.Set(ctx, "k", []byte("v"), time.Minute)
v, _ := c.Get(ctx, "k")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is the Memcached implementation of cache.Cache. It also satisfies cache.MultiCache (native multi-key get; SetMulti is a per-item loop, as Memcached has no atomic multi-set).

func New

func New(opts Options) (*Cache, error)

New constructs a Memcached cache. It does not dial eagerly; the first operation opens a connection. An error is returned only for an invalid configuration (there is none today, so it is always nil — the signature mirrors redis.New for symmetry).

func (*Cache) Close

func (c *Cache) Close() error

Close closes every pooled connection and rejects further use.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, keys ...string) (n int, err error)

Delete removes the listed keys, returning the count that existed.

func (*Cache) Exists

func (c *Cache) Exists(ctx context.Context, key string) (_ bool, err error)

Exists reports whether the key has a live entry.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string) (_ []byte, err error)

Get returns the value for key, or cache.ErrNotFound.

func (*Cache) GetMulti

func (c *Cache) GetMulti(ctx context.Context, keys ...string) (_ map[string][]byte, err error)

GetMulti fetches many keys in a single get command. Missing keys are absent from the result. Keys in the returned map carry the KeyPrefix (they are the wire keys), consistent with how a batched get echoes them.

func (*Cache) Ping

func (c *Cache) Ping(ctx context.Context) (err error)

Ping verifies the server is reachable via the version command.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) (err error)

Set stores value under key. ttl=0 means no expiry. Memcached treats an exptime above 30 days as an absolute unix timestamp; TTLs longer than that are not supported and return ErrInvalidKey-free but capped behaviour — keep cache TTLs under 30 days.

func (*Cache) SetMulti

func (c *Cache) SetMulti(ctx context.Context, items map[string][]byte, ttl time.Duration) (err error)

SetMulti stores each item with the same TTL. Memcached has no atomic multi-set, so this pipelines individual set commands on one connection.

func (*Cache) TTL

func (c *Cache) TTL(ctx context.Context, key string) (_ time.Duration, err error)

TTL returns the remaining lifetime via the meta-get command (Memcached 1.6+): -1 for "no expiry", or cache.ErrNotFound for an absent key.

type Options

type Options struct {
	// Addr is the host:port of the server. Default "127.0.0.1:11211".
	Addr string

	// MaxConns caps the simultaneous pooled connections. Default 10.
	MaxConns int

	// DialTimeout caps a single TCP dial. Default 5s.
	DialTimeout time.Duration

	// ReadTimeout / WriteTimeout cap per-command I/O when the caller's
	// context has no deadline. Default 3s each. Negative disables.
	ReadTimeout  time.Duration
	WriteTimeout time.Duration

	// KeyPrefix is prepended to every key. Useful for namespacing a
	// shared server.
	KeyPrefix string

	// Hook fires after every operation with kind = "cache.get",
	// "cache.set", "cache.del", "cache.exists", "cache.ttl",
	// "cache.ping", "cache.mget", "cache.mset".
	Hook drops.Hook

	// Dialer overrides how new connections are made (for tests / custom
	// transports). Default net.Dialer.
	Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
}

Options configures a Memcached cache. Zero-valued fields take the defaults documented below.

Jump to

Keyboard shortcuts

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