memcached

package
v1.0.1 Latest Latest
Warning

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

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

Documentation

Overview

Package memcached provides functionality for establishing connections to Memcached servers using the gomemcache package "github.com/bradfitz/gomemcache/memcache". It allows you to connect to specific Memcached servers, configure connection parameters, and reuse existing connections. The package also handles configurations such as server addresses derived from the application configuration.

Usage:

  1. Use Connect(serverName) to connect to a specific Memcached server, ensuring thread-safe and singleton connection instances.

The Connect() method uses a mutex to manage concurrent access and returns a pointer to the established Memcached client connection or an error if the connection process fails.

Example:

client, err := memcached.Connect("my_server")
if err != nil {
    log.Fatalf("Failed to connect to Memcached: %v", err)
}

// Use the Memcached client for further operations, e.g., get or set data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAll

func CloseAll() error

func CloseCluster

func CloseCluster(serverName string) error

func SetConnectionConfig

func SetConnectionConfig(clusterName string, config *ConnectionConfig)

Types

type ConnectionConfig

type ConnectionConfig struct {
	Addresses   []string
	Timeout     time.Duration
	MaxIdleConn int64
}

func GetConnectionConfig

func GetConnectionConfig(clusterName string) *ConnectionConfig

type Memcached

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

Memcached encapsulates the client instance for an memcached server.

func (*Memcached) Close

func (m *Memcached) Close() error

Close shuts down the connection to Memcached.

func (*Memcached) GetClient

func (m *Memcached) GetClient() MemcachedClientInterface

GetClient returns the underlying client for the memcached instance.

func (*Memcached) SetClient

func (m *Memcached) SetClient(client MemcachedClientInterface)

type MemcachedClientInterface

type MemcachedClientInterface interface {
	// CRUD operations
	Get(key string) (*memcache.Item, error)
	GetMulti(keys []string) (map[string]*memcache.Item, error)
	GetAndTouch(key string, expiration int32) (*memcache.Item, error)
	Set(item *memcache.Item) error
	Add(item *memcache.Item) error
	Replace(item *memcache.Item) error
	Delete(key string) error
	DeleteAll() error
	FlushAll() error

	// Mutations
	Increment(key string, delta uint64) (uint64, error)
	Decrement(key string, delta uint64) (uint64, error)
	Append(item *memcache.Item) error
	Prepend(item *memcache.Item) error
	CompareAndSwap(item *memcache.Item) error

	// TTL Management
	Touch(key string, seconds int32) error

	// Health
	Ping() error

	// Connection
	Close() error
}

MemcacheClientInterface defines the methods needed from a Memcached client. It abstracts the essential cache operations for easy mocking and testing.

type MemcachedConnector

type MemcachedConnector struct {
	Memcached *Memcached
}

MemcachedConnector represents a connector to a Memcached instance

func (*MemcachedConnector) New

func (mc *MemcachedConnector) New(server ...string) MemcachedInterface

New attmpets to establish a connection to a Memcached instance.

Parameters: - server: List of host addresses

Returns: - An instance of MemcachedInterface, representing the connection

type MemcachedConnectorInterface

type MemcachedConnectorInterface interface {

	// New establishes a connection to the memcached server
	New(server ...string) MemcachedInterface
}

MemcachedConnectorInterface defines methods to establish a connection to a Memcached cluster and retrieve a client interface.

type MemcachedInterface

type MemcachedInterface interface {
	GetClient() MemcachedClientInterface
	SetClient(client MemcachedClientInterface)
	Close() error
}

MemcachedInterface defines methods to interact with an memcached server.

func Connect

func Connect(connector MemcachedConnectorInterface, serverName string) (MemcachedInterface, error)

Connect establishes a connection to a Memcached server specified by the given serverName. If a connection to the server already exists, the existing connection is reused. This method uses a singleton pattern to ensure only one connection i nstance per serverName.

Parameters:

  • serverName: The name of the Memcached server to connect to.

Returns:

  • *memcache.Client: A pointer to the established Memcached client connection. If a connection to the given serverName already exists, the existing connection will be returned.
  • error: An error object that describes the reason for any connection failures. It returns nil if the connection was successful.

Note: This method is thread-safe and uses mutexes to handle concurrent access.

Jump to

Keyboard shortcuts

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