diff --git a/README.md b/README.md index 350c4d0..1ae9320 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,21 @@ func main() { } ``` +## Disabling CAS Support + +By default, the client uses `gets` commands to support CAS (Compare-And-Swap) operations. +If your memcached setup does not support CAS or you want to disable it (e.g., for performance or compatibility), +you can set the `DisableCAS` field on the client: + +```go +mc := memcache.New("127.0.0.1:11211") +mc.DisableCAS = true +``` + +When `DisableCAS` is enabled: +- The client will use `get` instead of `gets` when fetching items. +- CAS-based methods (like `CompareAndSwap`) will either be no-ops or fall back to simpler operations (e.g., `Set`). + ## Full docs, see: See https://pkg.go.dev/github.com/bradfitz/gomemcache/memcache @@ -36,4 +51,3 @@ Or run: ```shell $ godoc github.com/bradfitz/gomemcache/memcache ``` - diff --git a/memcache/memcache.go b/memcache/memcache.go index 6f48caa..53ab88c 100644 --- a/memcache/memcache.go +++ b/memcache/memcache.go @@ -153,6 +153,9 @@ type Client struct { // be set to a number higher than your peak parallel requests. MaxIdleConns int + // DisableCAS makes the client use "get" instead of "gets", so CAS IDs won't be fetched. + DisableCAS bool + selector ServerSelector mu sync.Mutex @@ -382,7 +385,11 @@ func (c *Client) withKeyRw(key string, fn func(*conn) error) error { func (c *Client) getFromAddr(addr net.Addr, keys []string, cb func(*Item)) error { return c.withAddrRw(addr, func(conn *conn) error { rw := conn.rw - if _, err := fmt.Fprintf(rw, "gets %s\r\n", strings.Join(keys, " ")); err != nil { + cmd := "gets" + if c.DisableCAS { + cmd = "get" + } + if _, err := fmt.Fprintf(rw, "%s %s\r\n", cmd, strings.Join(keys, " ")); err != nil { return err } if err := rw.Flush(); err != nil {