Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.
/ dallimin Public archive

Dalli compatible server selector for Golang (gomemcache)

License

Notifications You must be signed in to change notification settings

bukalapak/dallimin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Dalli compatible server selector for Golang (gomemcache)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published