Categorygithub.com/yeqown/memcached

# README

Memcached

Go Reference Go Report Card GitHub license GitHub release GitHub stars GitHub issues Build Status codecov

This is a golang package for Memcached. It is a simple and easy to use package.

Features

  • Completed Memcached text protocol, includes meta text protocol.
  • Integrated serialization and deserialization function
  • Cluster support, multiple hash algorithm support, include: crc32, murmur3, redezvous and also custom hash algorithm.
  • Fully connection pool features support.
  • CLI tool support.
  • SASL support.

Installation

go get github.com/yeqown/memcached@latest

Or you can install the CLI binary by running:

go install github.com/yeqown/memcached/cmd/memcached-cli@latest

More memcached-cli usage could be found in CLI.

Usage

There is a simple example to show how to use this package. More examples could be found in the example directory.

package main

import (
    "context"
    "time"

    "github.com/yeqown/memcached"
)

func main() {
	// 1. build client
	// addrs is a string, if you have multiple memcached servers, you can use comma to separate them.
	// e.g. "localhost:11211,localhost:11212,localhost:11213"
	addrs := "localhost:11211"

	// client support options, you can set the options to client.
	// e.g. memcached.New(addrs, memcached.WithDialTimeout(5*time.Second))
	client, err := memcached.New(addrs)
	if err != nil {
		panic(err)
	}

	// 2. use client
	// now, you can use the client API to finish your work.
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	version, err := client.Version(ctx)
	if err != nil {
		panic(err)
	}
	println("Version: ", version)

	if err = client.Set(ctx, "key", []byte("value"), 0, 0); err != nil {
		panic(err)
	}
	if err = client.Set(ctx, "key2", []byte("value2"), 0, 0); err != nil {
		panic(err)
	}

	items, err := client.Gets(ctx, "key", "key2")
	if err != nil {
		panic(err)
	}

	for _, item := range items {
		println("key: ", item.Key, " value: ", string(item.Value))
	}
}

Support Commands

Now, we have implemented some commands, and we will implement more commands in the future.

CommandStatusAPI UsageDescription
---------STORAGE COMMANDS---
SetSet(ctx context.Context, key string, value []byte, flags, expiry uint32) errorSet a key-value pair to memcached
AddAdd(ctx context.Context, key string, value []byte, flags, expiry uint32) errorAdd a key-value pair to memcached
ReplaceReplace(ctx context.Context, key string, value []byte, flags, expiry uint32) errorReplace a key-value pair to memcached
AppendAppend(ctx context.Context, key string, value []byte, flags, expiry uint32) errorAppend a value to the key
PrependPrepend(ctx context.Context, key string, value []byte, flags, expiry uint32) errorPrepend a value to the key
CasCas(ctx context.Context, key string, value []byte, flags, expiry uint32, cas uint64) errorCompare and set a key-value pair to memcached
---------RETRIEVAL COMMANDS---
GetsGets(ctx context.Context, keys ...string) ([]*Item, error)Get a value by key from memcached with cas value
GetGet(ctx context.Context, key string) (*Item, error)Get a value by key from memcached
GetAndTouchGetAndTouch(ctx context.Context, expiry uint32, key string) (*Item, error)Get a value by key from memcached and touch the key's expire time
GetAndTouchesGetAndTouches(ctx context.Context, expiry uint32, keys ...string) ([]*Item, error)Get a value by key from memcached and touch the key's expire time
----------OTHER COMMANDS---
DeleteDelete(ctx context.Context, key string) errorDelete a key-value pair from memcached
IncrIncr(ctx context.Context, key string, delta uint64) (uint64, error)Increment a key's value
DecrDecr(ctx context.Context, key string, delta uint64) (uint64, error)Decrement a key's value
TouchTouch(ctx context.Context, key string, expiry uint32) errorTouch a key's expire time
MetaGetMetaGet(ctx context.Context, key []byte, options ...MetaGetOption) (*MetaItem, error)Get a key's meta information
MetaSetMetaSet(ctx context.Context, key, value []byte, options ...MetaSetOption) (*MetaItem, error)Set a key's meta information
MetaDeleteMetaDelete(ctx context.Context, key []byte, options ...MetaDeleteOption) (*MetaItem, error)Delete a key's meta information
MetaArithmeticMetaArithmetic(ctx context.Context, key []byte, delta uint64, options ...MetaArithmeticOption) (*MetaItem, error)Arithmetic a key's meta information
MetaDebugMetaDebug(ctx context.Context, key []byte, options ...MetaDebugOption) (*MetaItemDebug, error)Debug a key's meta information
MetaNoopMetaNoop(ctx context.Context) errorNoop a key's meta information
VersionVersion(ctx context.Context) (string, error)Get memcached server version
FlushAllFlushAll(ctx context.Context) errorFlush all keys in memcached server

Development Guide

Prerequisites

  • Go 1.21 or higher
  • Python (for pre-commit hooks) or just brew install pre-commit on MacOS
  • Docker (for running memcached in tests)

Setting up development environment

  1. Clone the repository:
    git clone https://github.com/yeqown/memcached.git
    cd memcached
    
  2. Install pre-commit hooks:
    pip install pre-commit
    # or MacOS
    # brew install pre-commit
    
    pre-commit install
    
  3. Install golangci-lint:
    go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
    

Running Tests

go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...

Code Style

This project follows the standard Go code style guidelines and uses golangci-lint for additional checks. The configuration can be found in .golangci.yml.

Key points:

  • Follow Go standard formatting (enforced by gofmt )
  • Ensure all code is properly tested
  • Write clear commit messages
  • Document public APIs

# Packages

No description provided by the author