Skip to content

Commit 4872a07

Browse files
committed
DisableCAS
extra line
1 parent 8d39553 commit 4872a07

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

README.md

Lines changed: 15 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,3 @@ Or run:
3651
```shell
3752
$ godoc github.com/bradfitz/gomemcache/memcache
3853
```
39-

memcache/memcache.go

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

156+
// DisableCAS makes the client use "get" instead of "gets", so CAS IDs won't be fetched.
157+
DisableCAS bool
158+
156159
selector ServerSelector
157160

158161
mu sync.Mutex
@@ -382,7 +385,11 @@ func (c *Client) withKeyRw(key string, fn func(*conn) error) error {
382385
func (c *Client) getFromAddr(addr net.Addr, keys []string, cb func(*Item)) error {
383386
return c.withAddrRw(addr, func(conn *conn) error {
384387
rw := conn.rw
385-
if _, err := fmt.Fprintf(rw, "gets %s\r\n", strings.Join(keys, " ")); err != nil {
388+
cmd := "gets"
389+
if c.DisableCAS {
390+
cmd = "get"
391+
}
392+
if _, err := fmt.Fprintf(rw, "%s %s\r\n", cmd, strings.Join(keys, " ")); err != nil {
386393
return err
387394
}
388395
if err := rw.Flush(); err != nil {

0 commit comments

Comments
 (0)