当前位置: 首页 > memcache > 正文

two memcache libraries in golang

跟其他语言一样,golang除了可以连接数据库(mysql)之外,也可以连接redis,memcache等基于内存的数据库,下面介绍两种golang的memcache库,名字都叫做gomemcache。

(1) bradfitz/gomemcache
install

go get -u github.com/bradfitz/gomemcache/memcache

或者

$ git clone git://github.com/bradfitz/gomemcache
$ cd gomemcache/memcache
$ make install

测试页面:

cat go_memcache.go
package main

import (
        "fmt"
        "github.com/bradfitz/gomemcache/memcache"
)

func main() {
     mc := memcache.New("127.0.0.1:11211")
     mc.Set(&memcache.Item{Key: "siteName", Value: []byte("sudops.com")})

     //mc.Delete("siteName")
     v,err := mc.Get("siteName")
     fmt.Printf("%s\n ", v)
     fmt.Println(err)     
}

go run gomem.go 
&{siteName sudops.com <nil> %!s(uint32=0) %!s(int32=0) %!s(uint64=17)}
 <nil>

不好理解的是golang中memcache的value的数据类型是[]byte.
直接打印出来是这样:
&{siteName [115 117 100 111 112 115 46 99 111 109] 0 0 18}

type Item struct {
    // Key is the Item's key (250 bytes maximum).
    Key string

    // Value is the Item's value.
    Value []byte

    // Object is the Item's value for use with a Codec.
    Object interface{}

    // Flags are server-opaque flags whose semantics are entirely
    // up to the app.
    Flags uint32

    // Expiration is the cache expiration time, in seconds: either a relative
    // time from now (up to 1 month), or an absolute Unix epoch time.
    // Zero means the Item has no expiration time.
    Expiration int32
    // contains filtered or unexported fields
}

Item is an item to be got or stored in a memcached server.

memcache stats

stats
STAT pid 12516
STAT uptime 22471
STAT time 1394321553
STAT version 1.4.17
STAT libevent 1.4.13-stable
STAT pointer_size 64
STAT rusage_user 0.422935
STAT rusage_system 0.518921
STAT curr_connections 10
STAT total_connections 30
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 18
STAT cmd_set 18
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 17
STAT get_misses 1
STAT delete_misses 0
STAT delete_hits 1
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 795
STAT bytes_written 1803
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT bytes 155
STAT curr_items 2
STAT total_items 18
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END

(2)kklis/gomemcache

INSTALL

go get -u github.com/kklis/gomemcache

样例:

cat go_mem.go 
package main

import (
	"fmt"
	"github.com/kklis/gomemcache"
)

func main() {
	memc, err := gomemcache.Connect("127.0.0.1", 11211)
	if err != nil {
		panic(err)
	}
	err = memc.Set("siteName", []uint8("sudops.com"), 0, 0)
	if err != nil {
		panic(err)
	}
	val, fl, _ := memc.Get("siteName")
	fmt.Printf("%s %d\n", val, fl)
}

运行结果:

go run go_mem.go 
sudops.com 0

本文固定链接: https://sudops.com/two-memcache-libraries-golang.html | 运维速度

该日志由 u2 于2014年03月09日发表在 memcache 分类下,
原创文章转载请注明: two memcache libraries in golang | 运维速度
关键字:

报歉!评论已关闭.