memcache

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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)

Jump to

Keyboard shortcuts

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