memcache

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

Memcache

Runs a Memcached container for integration testing and returns a memcache.Memcache typed client. The wrapper starts the container, waits for it to answer a ping, and exposes the endpoint address. You connect to it with github.com/bradfitz/gomemcache/memcache.

The client interface provides GetEndpointAddress(), and Close(ctx).

Tested versions

There are no versioned integration tests yet — applications/memcache/versions/ does not exist. Only the default image below is covered by the package's integration test (applications/memcache/memcache_test.go).

Default image

When you call memcache.New(ctx) without an explicit image, the default (images.Memcache) is used:

index.docker.io/library/memcached:1.6.29-alpine3.20

Use memcache.NewWithImage(ctx, image) to select a different version.

How to use

package main

import (
 "context"
 "fmt"
 "time"

 memcacheCli "github.com/bradfitz/gomemcache/memcache"

 "github.com/teran/go-docker-testsuite/applications/memcache"
)

func main() {
 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
 defer cancel()

 app, err := memcache.New(ctx)
 if err != nil {
  panic(err)
 }
 defer func() { _ = app.Close(ctx) }()

 addr, err := app.GetEndpointAddress()
 if err != nil {
  panic(err)
 }
 fmt.Println("memcache endpoint:", addr)

 cli := memcacheCli.New(addr)
 defer func() { _ = cli.Close() }()

 if err := cli.Set(&memcacheCli.Item{
  Key:   "greeting",
  Value: []byte("Hello, Memcache!"),
 }); err != nil {
  panic(err)
 }

 item, err := cli.Get("greeting")
 if err != nil {
  panic(err)
 }
 fmt.Printf("key value: %s\n", string(item.Value))
}

Running the tests

There are no versioned tests for memcache yet. Run the package integration test (it requires a running Docker daemon):

go test ./applications/memcache/

Documentation

Overview

Example

This example demonstrates starting a Memcache container, connecting via gomemcache, and performing SET/GET operations.

package main

import (
	"context"
	"fmt"
	"time"

	memcacheCli "github.com/bradfitz/gomemcache/memcache"

	"github.com/teran/go-docker-testsuite/applications/memcache"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
	defer cancel()

	app, err := memcache.New(ctx)
	if err != nil {
		fmt.Printf("error: %v (is Docker running?)\n", err)
		return
	}
	defer func() { _ = app.Close(ctx) }()

	addr, err := app.GetEndpointAddress()
	if err != nil {
		fmt.Printf("error getting address: %v\n", err)
		return
	}
	fmt.Println("memcache endpoint:", addr)

	cli := memcacheCli.New(addr)
	defer func() { _ = cli.Close() }()

	if err := cli.Set(&memcacheCli.Item{
		Key:   "greeting",
		Value: []byte("Hello, Memcache!"),
	}); err != nil {
		fmt.Printf("error setting key: %v\n", err)
		return
	}
	fmt.Println("key set")

	item, err := cli.Get("greeting")
	if err != nil {
		fmt.Printf("error getting key: %v\n", err)
		return
	}
	fmt.Printf("key value: %s\n", string(item.Value))
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Memcache

type Memcache interface {
	Close(context.Context) error
	GetEndpointAddress() (string, error)
}

func New

func New(ctx context.Context) (Memcache, error)

func NewWithImage

func NewWithImage(ctx context.Context, image string) (Memcache, error)

func NewWithImageT added in v1.5.0

func NewWithImageT(t *testing.T, ctx context.Context, image string) (Memcache, error)

NewWithImageT is NewWithImage bound to a *testing.T: the container's lifecycle is tied to the test and cleaned up automatically via t.Cleanup.

func NewWithT added in v1.5.0

func NewWithT(t *testing.T, ctx context.Context) (Memcache, error)

NewWithT is New bound to a *testing.T: the container's lifecycle is tied to the test and cleaned up automatically via t.Cleanup.

Jump to

Keyboard shortcuts

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