memcache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package memcache is a cache.Store on Memcached, in its own Go module so no client library reaches a project that does not import it.

Memcached is simpler than Redis, and the differences are real, so they are stated here instead of hidden behind a symmetric Config:

  • No authentication. The client has no SASL, so Config has no username or password. Use network isolation, or TLS (below), as the security model.
  • TLS only through the client's dialer, for a server started with --enable-ssl. Config.TLSConfig builds that dialer; nothing else is configurable.
  • No in-flight cancellation. The client's calls take no context. The adapter checks the context before an operation begins, but once I/O has started Config.Timeout (default 500ms) is the bound.
  • Several servers are sharded by key with no replication. Changing the server list remaps keys, which shows up as misses: acceptable for a cache, and why one cache must not hold anything you cannot recompute.
  • Expiry has one-second granularity and zero means "never expires", so a TTL is rounded up to whole seconds and is never turned into zero.

A Store passes the cachetest conformance suite against a real server. See https://memcached.org for the server.

Index

Constants

View Source
const DefaultTimeout = 500 * time.Millisecond

DefaultTimeout is the socket read/write timeout used when Config.Timeout is zero.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Servers lists host:port addresses. It must not be empty. With more than
	// one, keys are sharded across them with no replication.
	Servers []string
	// Timeout bounds each socket read and write, and so every operation once
	// it has begun. Zero means DefaultTimeout; negative is invalid.
	Timeout time.Duration
	// MaxIdleConns is the most idle connections kept per server. Zero uses
	// the client's default; negative is invalid.
	MaxIdleConns int
	// TLSConfig, when set, makes every connection use TLS (the server must be
	// started with --enable-ssl). It is applied through the client's dialer.
	TLSConfig *tls.Config
}

Config describes the Memcached servers a Store talks to.

type Store

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

Store is a Memcached cache.Store. Close it when done.

func New

func New(cfg Config) (*Store, error)

New validates cfg and returns a Store. It does not connect: the client connects lazily and has no handshake to verify.

func (*Store) Close

func (s *Store) Close() error

Close closes the idle connections. The Store can still be used afterwards.

func (*Store) Delete

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

Delete implements cache.Store.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string) ([]byte, bool, error)

Get implements cache.Store.

func (*Store) Set

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

Set implements cache.Store. The TTL is rounded up to whole seconds, at least one, so a positive TTL never becomes Memcached's "no expiry".

Jump to

Keyboard shortcuts

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