memcached

package module
v0.0.0-...-4da99d4 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

README

Memcached Client Library

This library implements basic operations for memcached communication such as Set and Get. It provides an idle pool of connections that can be reused as per memcached protocol the server performance is better when connections are maintained opened rather than established for every request. For applications critical to the network latency, the library provides a profiling interface to collect information about network connections, set or get times, and some detailed information for socket operations.

The client instantiated with NewClient() is thread safe and therefore can be used in multiple goroutines.

The internal pool of reusable connections is maintained for optimization purposes. The memcached protocol suggests to reuse network connections as long as possible rather than close them at the end of every request. This pool size can be set via maxIdleConn parameter of the NewClient(). In case of a peak load the new connections will still be allowed to be created even if the number exceeds the pool size. They will be closed once used so that the preferred pool size is maintained.

WARNING: The default memcached server time is 2 minutes until it closes inactive connection. This library uses a default and at this point can only be changed via Client.idleTimeout.

Using The Client Library

This library can be used as a Go package. To create an instance of memcached client

    // Instantiate
    client, err := memcached.NewClient("127.0.0.1:11211", 100)
    if err != nil {
        return err
    }
	
    // Set data with key "1/2/3" and expiration time 30 seconds
    if err := client.Set(context.Background(), "1/2/3", []byte{"hello"}, 30); err != nil {
        return err
    }
	
    // Get data
    resp, err := client.Get(context.Background(), "1/2/3")
    if err != nil {
        return err
    }
    process(resp.Data)

To enable profiler and receive the network io values

    p := memcached.NewFuncProfiler(func(name string, duration time.Duration) {
        // put any custom code here that can read metric name/duration
        metrics.Record("memcached_profiler", duration, name)
    })
    memcachedClient.SetProfiler(p)

To read some client stats

    
    t := time.NewTicker(1 * time.Second)
    for {
        select {
        case <-c.Done():
            return
        case _ = <-t.C:
            stats := client.GetStats()
            metrics.Save("first_metric", stats.ConnPoolLen, "conn_pool_len")
        }
    }

License

Comcast.github.io is licensed under Apache License 2.0. Valid-License-Identifier: Apache-2.0

Code of Conduct

We take our code of conduct very seriously. Please abide by it.

Contributing

Please read our contributing guide for details on how to contribute to our project.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyNotFound = errors.New("key not found")

ErrKeyNotFound returned when given key not found.

Functions

This section is empty.

Types

type Client

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

Client is used for basic operations such as set and get.

func NewClient

func NewClient(addr string, maxIdleConn int) (*Client, error)

NewClient instantiates a client with a given address in a form of host:port and maxIdleConn conn pool. Max idle connection specifies the max number of reused connections and should be greater than zero.

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string) (*Response, error)

Get returns the value associated with the key. If the context contains timeout, a deadline for the connection will be used. It is up to the client to set the timeout. Returns ErrKeyNotFound for non-existing key.

func (*Client) GetProfiler

func (c *Client) GetProfiler() Profiler

GetProfiler returns profiler.

func (*Client) GetStats

func (c *Client) GetStats() Stats

GetStats returns internal stats such as number of connections, etc.

func (*Client) Set

func (c *Client) Set(ctx context.Context, key string, data []byte, expiration int) error

Set sets value associated with a key. If the context contains timeout, a deadline for the connection will be used. It is up to the client to set the timeout. Returns an error in case NOT_STORED response received from the server or if any other error occurred such as connection error, etc.

func (*Client) SetProfiler

func (c *Client) SetProfiler(prof Profiler)

SetProfiler sets a profiler to measure network io read/write times.

type FuncProfiler

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

func NewFuncProfiler

func NewFuncProfiler(f func(string, time.Duration)) *FuncProfiler

NewFuncProfiler instantiates profiler. func f is exposed to the client of this method to customize reported metrics handling.

func (*FuncProfiler) Report

func (f *FuncProfiler) Report(name string, duration time.Duration)

Report reports given metric name and duration to the caller.

type Profiler

type Profiler interface {
	Report(name string, duration time.Duration)
}

Profiler is an interface for profiling the client network io performance.

type Response

type Response struct {
	// Key is a unique key that has some value
	Key string
	// Data is the value associated with the key
	Data []byte
}

Response structure for operations such as set and get.

type Stats

type Stats struct {
	ConnPoolLen   int    // ConnPoolLen is the current size of connections pool
	OpenConnCount uint32 // OpenConnCount is the current number of opened connections
}

Stats represents internal stats such as connections count, etc.

Jump to

Keyboard shortcuts

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