项目作者: memcachier

项目描述 :
具有SASL支持的Go的二进制Memcached客户端
高级语言: Go
项目地址: git://github.com/memcachier/mc.git
创建时间: 2012-06-14T02:45:57Z
项目社区:https://github.com/memcachier/mc

开源协议:MIT License

下载


mc.go: A Go client for Memcached

godoc
Build Status

This is a (pure) Go client for Memcached. It supports
the binary Memcached protocol, SASL authentication and Compression. It’s thread-safe.
It allows connections to entire Memcached clusters and supports connection
pools, timeouts, and failover.

Install

Module-aware mode:

  1. $ go get github.com/memcachier/mc/v3

Legacy GOPATH mode:

  1. $ go get github.com/memcachier/mc

Use

  1. import "github.com/memcachier/mc/v3"
  2. // Legacy GOPATH mode:
  3. // import "github.com/memcachier/mc"
  4. func main() {
  5. // Error handling omitted for demo
  6. // Only PLAIN SASL auth supported right now
  7. c := mc.NewMC("localhost:11211", "username", "password")
  8. defer c.Quit()
  9. exp := 3600 // 2 hours
  10. cas, err = c.Set("foo", "bar", flags, exp, cas)
  11. if err != nil {
  12. ...
  13. }
  14. val, flags, cas, err = c.Get("foo")
  15. if err != nil {
  16. ...
  17. }
  18. err = c.Del("foo")
  19. if err != nil {
  20. ...
  21. }
  22. }

Using zlib Compression

  1. import (
  2. "github.com/memcachier/mc/v3"
  3. "compress/zlib"
  4. )
  5. // Legacy GOPATH mode:
  6. // import "github.com/memcachier/mc"
  7. func main() {
  8. // Error handling omitted for demo
  9. // Only PLAIN SASL auth supported right now
  10. config := mc.DefaultConfig()
  11. // You have to set the functions to compress and descompress
  12. // At this example we are using zlib.
  13. config.Compression.Decompress = func(value string) (string, error) {
  14. var compressedValue bytes.Buffer
  15. zw, err := zlib.NewWriterLevel(&compressedValue, -1)
  16. if err != nil {
  17. return value, err
  18. }
  19. if _, err = zw.Write([]byte(value)); err != nil {
  20. return value, err
  21. }
  22. zw.Close()
  23. return compressedValue.String(), nil
  24. }
  25. config.Compression.Compress = func(value string) (string, error) {
  26. if value == "" {
  27. return value, nil
  28. }
  29. zr, err := zlib.NewReader(strings.NewReader(value))
  30. if err != nil {
  31. return value, nil // Does not return error, the value could be not compressed
  32. }
  33. defer zr.Close()
  34. var unCompressedValue bytes.Buffer
  35. _, err = io.Copy(&unCompressedValue, zr)
  36. if err != nil {
  37. return value, nil
  38. }
  39. return unCompressedValue.String(), nil
  40. }
  41. c := mc.NewMCwithConfig("localhost:11211", "username", "password", config)
  42. defer c.Quit()
  43. exp := 3600 // 2 hours
  44. cas, err = c.Set("foo", "bar", flags, exp, cas)
  45. if err != nil {
  46. ...
  47. }
  48. val, flags, cas, err = c.Get("foo")
  49. if err != nil {
  50. ...
  51. }
  52. err = c.Del("foo")
  53. if err != nil {
  54. ...
  55. }
  56. }

Using gzip Compression

  1. import (
  2. "github.com/memcachier/mc/v3"
  3. "compress/gzip"
  4. )
  5. // Legacy GOPATH mode:
  6. // import "github.com/memcachier/mc"
  7. func main() {
  8. // Error handling omitted for demo
  9. // Only PLAIN SASL auth supported right now
  10. config := mc.DefaultConfig()
  11. // You have to set the functions to compress and descompress
  12. // At this example we are using gzip.
  13. config.Compression.Decompress = func(value string) (string, error) {
  14. var compressedValue bytes.Buffer
  15. zw, err := gzip.NewWriterLevel(&compressedValue, -1)
  16. if err != nil {
  17. return value, err
  18. }
  19. if _, err = zw.Write([]byte(value)); err != nil {
  20. return value, err
  21. }
  22. zw.Close()
  23. return compressedValue.String(), nil
  24. }
  25. config.Compression.Compress = func(value string) (string, error) {
  26. if value == "" {
  27. return value, nil
  28. }
  29. zr, err := gzip.NewReader(strings.NewReader(value))
  30. if err != nil {
  31. return value, nil // Does not return error, the value could be not compressed
  32. }
  33. defer zr.Close()
  34. var unCompressedValue bytes.Buffer
  35. _, err = io.Copy(&unCompressedValue, zr)
  36. if err != nil {
  37. return value, nil
  38. }
  39. return unCompressedValue.String(), nil
  40. }
  41. c := mc.NewMCwithConfig("localhost:11211", "username", "password", config)
  42. defer c.Quit()
  43. exp := 3600 // 2 hours
  44. cas, err = c.Set("foo", "bar", flags, exp, cas)
  45. if err != nil {
  46. ...
  47. }
  48. val, flags, cas, err = c.Get("foo")
  49. if err != nil {
  50. ...
  51. }
  52. err = c.Del("foo")
  53. if err != nil {
  54. ...
  55. }
  56. }

Missing Feature

There is nearly coverage of the Memcached protocol.
The biggest missing protocol feature is support for multi_get and other
batched operations.

There is also no support for asynchronous IO.

Performance

Right now we use a single per-connection mutex and don’t support pipe-lining any
operations. There is however support for connection pools which should make up
for it.

Get involved!

We are happy to receive bug reports, fixes, documentation enhancements,
and other improvements.

Please report bugs via the
github issue tracker.

Master git repository:

  • git clone git://github.com/memcachier/mc.git

Licensing

This library is MIT-licensed.

Authors

This library is written and maintained by MemCachier.
It was originally written by Blake Mizerany.