APPEND

Introduction

In Dragonfly, as well as in Redis and Valkey, the APPEND command is used to add a specified value to the end of a string stored at a given key. If the key doesn’t exist, the command creates a new key and initializes it with an empty string before appending. This command is particularly useful for accumulating logs or constructing strings incrementally.

Syntax

  1. APPEND key value

Parameter Explanations

  • key: The key of the string you want to append data to. If the key already exists, it must be of type string; otherwise, a new string will be created.
  • value: The string value to be appended to the existing string associated with the key.

Return Values

  • An integer indicating the length of the string after the append operation.

Code Examples

Basic Example

Appending a value to an existing key:

  1. dragonfly> SET mystring "Hello"
  2. OK
  3. dragonfly> APPEND mystring " World"
  4. (integer) 11
  5. dragonfly> GET mystring
  6. "Hello World"

Logging Use Case

Accumulating log entries for a simple logging mechanism:

  1. dragonfly> APPEND log "2023-07-26 10:00:00 - User login\n"
  2. (integer) 33
  3. dragonfly> APPEND log "2023-07-26 10:05:00 - User logout\n"
  4. (integer) 66
  5. dragonfly> GET log
  6. "2023-07-26 10:00:00 - User login\n2023-07-26 10:05:00 - User logout\n"

Incremental Data Construction

Building a configuration file or script incrementally:

  1. dragonfly> APPEND config "server {\n"
  2. (integer) 9
  3. dragonfly> APPEND config " listen 80;\n"
  4. (integer) 22
  5. dragonfly> APPEND config "}\n"
  6. (integer) 24
  7. dragonfly> GET config
  8. "server {\n listen 80;\n}\n"

Best Practices

  • Ensure that the key being appended to is of string type to avoid errors.
  • Use APPEND for operations where string concatenation is frequent but data volume is manageable.

Common Mistakes

  • Appending to a key that holds a non-string value can result in an error. Always ensure the data type compatibility.

    1. dragonfly> LPUSH mylist "item"
    2. (integer) 1
    3. dragonfly> APPEND mylist "string"
    4. (error) WRONGTYPE Operation against a key holding the wrong kind of value

FAQs

What happens if the key does not exist?

If the key does not exist, APPEND will create it as an empty string and then append the provided value.

Can I use APPEND with non-string keys?

No, APPEND only works with keys that hold string values. Attempting to use it with other data types will result in an error.