minimemcached

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MIT Imports: 13 Imported by: 0

README

Minimemcached

Go Reference Test

Minimemcached is a Memcached server for written in Go, aimed for unittests in Go projects.


When you have to test codes that use Memcached server, running actual Memcached server instance could be quite expensive, depending on your environment.

Minimemcached aims to solve this problem by implementing Memcached's TCP interface 100% in Go, and works perfectly well with gomemcache, a memcache client for Go.

Implemented commands

  • get
  • gets
  • cas
  • set
  • touch
  • add
  • replace
  • append
  • prepend
  • delete
  • incr
  • decr
  • flush_all
  • version

Setup

  • To use Minimemcached, you can import it as below. You can also view example code.
package main

import (
	"fmt"

	"github.com/bradfitz/gomemcache/memcache"

	"github.com/daangn/minimemcached"
)

func main() {
	cfg := &minimemcached.Config{
		Port: 8080,
	}
	m, err := minimemcached.Run(cfg)
	if err != nil {
		fmt.Println("failed to start mini-memcached server.")
		return
	}

	defer m.Close()

	fmt.Println("mini-memcached started")

	mc := memcache.New(fmt.Sprintf("localhost:%d", m.Port()))
	err = mc.Set(&memcache.Item{Key: "foo", Value: []byte("my value"), Expiration: int32(60)})
	if err != nil {
		fmt.Printf("err(set): %v\n", err)
	}

	it, err := mc.Get("foo")
	if err != nil {
		fmt.Printf("err(get): %v\n", err)
	} else {
		fmt.Printf("key: %s, value: %s\n", it.Key, it.Value)
		fmt.Printf("value: %s\n", string(it.Value))
	}
}

Benchmarks

  • Running same test cases on memcached server on a docker and minimemcached, minimemcached outperformed memcached running on docker container.
  • You can run this benchmark yourself. Check here.
Benchmark Environment

  • goos: darwin
  • goarch: arm64
  • memcached docker image: memcached:1.5.16

Results
# Memcached running on a docker container.
BenchmarkMemcached-8       	     610	   1721643 ns/op
BenchmarkMemcached-8       	     729	   1714716 ns/op
BenchmarkMemcached-8       	     717	   1716914 ns/op
BenchmarkMemcached-8       	     698	   1783312 ns/op
BenchmarkMemcached-8       	     693	   1784781 ns/op

# Minimemcached.
BenchmarkMinimemcached-8    24710	     46661 ns/op
BenchmarkMinimemcached-8    24684	     47918 ns/op
BenchmarkMinimemcached-8    24866	     47558 ns/op
BenchmarkMinimemcached-8    25046	     46770 ns/op
BenchmarkMinimemcached-8    26085	     46707 ns/op

op: set, get, delete operation

  • As shown in the result above, minimemcached took about 47122.8 ns per operation, when memcached took about 1744273.2 ns per operation.

Author

Contributions

  • If you want to contribute to Minimemcached, feel free to create an issue or pull request!

Documentation

Index

Constants

View Source
const (
	Version = "1.1.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Port is the port number where mini-memcached will start.
	// When given 0, mini-memcached will start running on a random available port.
	Port uint16
}

Config contains minimum attributes to run mini-memcached. TODO(@sang-w0o): Selectively accept log writer and log level.

type MiniMemcached

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

func Run

func Run(cfg *Config, opts ...Option) (*MiniMemcached, error)

Run creates and starts a MiniMemcached server on a random, available port. Close with Close().

func (*MiniMemcached) Close

func (m *MiniMemcached) Close()

Close closes mini-memcached server and clears all objects stored.

func (*MiniMemcached) Port

func (m *MiniMemcached) Port() uint16

type Option added in v1.0.1

type Option func(m *MiniMemcached)

func WithClock added in v1.0.1

func WithClock(clk clock.Clock) Option

WithClock applies custom Clock interface. Clock will be used when item is created.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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