Skip to content

Commit 9eb14d3

Browse files
committed
DisableCAS
1 parent 8d39553 commit 9eb14d3

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ func main() {
2727
}
2828
```
2929

30+
## Disabling CAS Support
31+
32+
By default, the client uses `gets` commands to support CAS (Compare-And-Swap) operations.
33+
If your memcached setup does not support CAS or you want to disable it (e.g., for performance or compatibility),
34+
you can set the `DisableCAS` field on the client:
35+
36+
```go
37+
mc := memcache.New("127.0.0.1:11211")
38+
mc.DisableCAS = true
39+
```
40+
41+
When `DisableCAS` is enabled:
42+
- The client will use `get` instead of `gets` when fetching items.
43+
- CAS-based methods (like `CompareAndSwap`) will either be no-ops or fall back to simpler operations (e.g., `Set`).
44+
3045
## Full docs, see:
3146

3247
See https://pkg.go.dev/github.com/bradfitz/gomemcache/memcache
@@ -36,4 +51,4 @@ Or run:
3651
```shell
3752
$ godoc github.com/bradfitz/gomemcache/memcache
3853
```
39-
54+
```

memcache/memcache.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ type Client struct {
153153
// be set to a number higher than your peak parallel requests.
154154
MaxIdleConns int
155155

156+
// DisableCAS disables CAS (Compare-And-Swap) operations if set to true.
157+
// Any CAS-based commands will become no-ops or fallbacks to non-CAS equivalents.
158+
DisableCAS bool
159+
156160
selector ServerSelector
157161

158162
mu sync.Mutex
@@ -382,7 +386,11 @@ func (c *Client) withKeyRw(key string, fn func(*conn) error) error {
382386
func (c *Client) getFromAddr(addr net.Addr, keys []string, cb func(*Item)) error {
383387
return c.withAddrRw(addr, func(conn *conn) error {
384388
rw := conn.rw
385-
if _, err := fmt.Fprintf(rw, "gets %s\r\n", strings.Join(keys, " ")); err != nil {
389+
cmd := "gets"
390+
if c.DisableCAS {
391+
cmd = "get"
392+
}
393+
if _, err := fmt.Fprintf(rw, "%s %s\r\n", cmd, strings.Join(keys, " ")); err != nil {
386394
return err
387395
}
388396
if err := rw.Flush(); err != nil {

0 commit comments

Comments
 (0)