Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,4 +51,3 @@ Or run:
```shell
$ godoc github.com/bradfitz/gomemcache/memcache
```

9 changes: 8 additions & 1 deletion memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down