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 ¶
- type Cache
- func (c *Cache) Close() error
- func (c *Cache) Delete(ctx context.Context, keys ...string) (n int, err error)
- func (c *Cache) Exists(ctx context.Context, key string) (_ bool, err error)
- func (c *Cache) Get(ctx context.Context, key string) (_ []byte, err error)
- func (c *Cache) GetMulti(ctx context.Context, keys ...string) (_ map[string][]byte, err error)
- func (c *Cache) Ping(ctx context.Context) (err error)
- func (c *Cache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) (err error)
- func (c *Cache) SetMulti(ctx context.Context, items map[string][]byte, ttl time.Duration) (err error)
- func (c *Cache) TTL(ctx context.Context, key string) (_ time.Duration, err error)
- type Options
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 ¶
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) GetMulti ¶
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) Set ¶
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.
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.