dallimin

package module
v0.0.0-...-d70dd99 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2017 License: MIT Imports: 12 Imported by: 0

README

dallimin

Build Status GoDoc

Dalli Ring written in Go.

Usage

Say, we have ruby program that use dalli to save our data to memcache:

require 'dalli'
require 'json'

options = { namespace: 'api', compress: true, serializer: JSON }
servers = %w(
  cache1.example.com:11210
  cache2.example.com:11211
  cache3.example.com:11212
)

client = Dalli::Client.new(servers, options)

client.set('v1/foo', foo: 'bar')
client.set('say:hello', 'World!')

puts client.get('v1/foo')
puts client.get('say:hello')

Then, we can access that within Go by using:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/bradfitz/gomemcache/memcache"
	"github.com/bukalapak/dallimin"
)

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
    option := dallimin.Option{
        CheckAlive: true,
        Failover: true,
    }

	servers := []string{
		"cache1.example.com:11210",
		"cache2.example.com:11211",
		"cache3.example.com:11212",
	}

    ss, err := dallimin.New(servers, option)

	checkErr(err)

	client := memcache.NewFromSelector(ss)

	type Data struct {
		Foo string `json:"foo"`
	}

	it, err := client.Get("api:v1/foo")
	checkErr(err)

	a := &Data{}
	b := bytes.NewReader(it.Value)
	j := json.NewDecoder(b)
	checkErr(err)

	err = j.Decode(a)
	checkErr(err)

	fmt.Printf("%s => %#v\n", it.Key, a) // RETURNS: api:v1/foo => &main.Data{Foo:"bar"}

	it, err = client.Get("api:say:hello")
	checkErr(err)

	fmt.Printf("%s => %s\n", it.Key, string(it.Value)) // RETURNS: api:say:hello => "World!"
}

That's it.

Documentation

Overview

Package dallimin provides dalli compatible server selector for gomemcache

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoServers = errors.New("memcache: no servers configured or available")
)

Functions

This section is empty.

Types

type Option

type Option struct {
	CheckAlive bool
	Failover   bool
}

type Ring

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

func New

func New(servers []string, option Option) (*Ring, error)

func NewWithWeights

func NewWithWeights(servers map[string]int, option Option) (*Ring, error)

func (*Ring) Each

func (h *Ring) Each(f func(net.Addr) error) error

Each iterates over each server calling the given function

func (*Ring) PickServer

func (h *Ring) PickServer(key string) (net.Addr, error)

PickServer returns the server address that a given item should be shared onto.

func (*Ring) Servers

func (h *Ring) Servers() []net.Addr

Servers return available server addresses

Jump to

Keyboard shortcuts

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