Skip to content
Merged
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
27 changes: 27 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,33 @@ func TestE2E_Replace(t *testing.T) {
})
}

func TestE2E_Append(t *testing.T) {
t.Parallel()

address, done := memctest.LaunchTCP(t, nil)
t.Cleanup(done)

c := New([]string{address})
defer ignore.Close(c)

t.Run("success", func(t *testing.T) {
err := Set(c, "key1", "value1")
must.NoError(t, err)

err = Append(c, "key1", ".appended")
must.NoError(t, err)

v, verr := Get[string](c, "key1")
must.NoError(t, verr)
must.Eq(t, "value1.appended", v)
})

t.Run("not found", func(t *testing.T) {
err := Append(c, "key-does-not-exist", "value")
must.ErrorIs(t, err, ErrNotStored)
})
}

func TestE2E_Increment(t *testing.T) {
t.Parallel()

Expand Down
79 changes: 79 additions & 0 deletions verbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,85 @@ func Replace[T any](c *Client, key string, item T, opts ...Option) error {
})
}

// Append will append the given value to the value associated with the given key.
//
// Append differs from Set in that it is meant to add additional data to an
// existing key, rather than replace the existing value entirely. The key
// must already exist.
//
// Uses Client c to connect to a memcached instance, and automatically handles
// connection pooling and reuse.
//
// One or more Option(s) may be applied to configure things such as the
// value expiration TTL or its associated flags.
func Append[T any](c *Client, key string, item T, opts ...Option) error {
if err := check(key); err != nil {
return err
}

options := &Options{
expiration: c.expiration,
flags: 0,
}

for _, opt := range opts {
opt(options)
}

return c.do(key, func(conn *iopool.Buffer) error {
encoding, encerr := encode(item)
if encerr != nil {
return encerr
}

expiration, experr := c.seconds(options.expiration)
if experr != nil {
return experr
}

// write the header components
if _, err := fmt.Fprintf(
conn,
"append %s %d %d %d\r\n",
key, options.flags, expiration, len(encoding),
); err != nil {
return err
}

// write the payload
if _, err := conn.Write(encoding); err != nil {
return err
}

// write clrf
if _, err := io.WriteString(conn, "\r\n"); err != nil {
return err
}

// flush the buffer
if err := conn.Flush(); err != nil {
return err
}

// read response
line, lerr := conn.ReadSlice('\n')
if lerr != nil {
return lerr
}

switch string(line) {
case "STORED\r\n":
return nil
case "NOT_STORED\r\n":
return ErrNotStored
case "NOT_FOUND\r\n":
return ErrNotFound
default:
return fmt.Errorf("memc: unexpected response to append: %q", string(line))
}
})
}

// Add will store the item using the given key, but only if no item currently
// exists. New items are at the top of the LRU.
//
Expand Down
Loading