Incremental string construction
Build a message or text buffer incrementally by adding fragments to one Redis string.
Redis · Module
APPEND adds a string to the end of a key's existing string value, creating the key if it is missing. It returns the resulting string length.
APPEND adds the supplied string to the existing string value at key. When the key is absent, Redis creates it with an empty starting value, then returns the resulting string length.
APPEND key value| Name | Type | Description |
|---|---|---|
key | key | The name of the key. |
value | string | The string to append to the existing value. |
Integer reply: the length of the string after the append operation.
key argument identifies which string value Redis should modify.value argument supplies the text that Redis adds after the current contents.redis-cli <<'EOF'
DEL greeting
APPEND greeting "Hello"
APPEND greeting " world"
GET greeting
EOFCreates the key on the first append, adds the second fragment, and reads the resulting string.
Build a message or text buffer incrementally by adding fragments to one Redis string.
Store fixed-width samples consecutively in one compact value for lightweight time-series data.
APPEND takes a key and a string value, adding that value after the key's current string. If the key is missing, Redis creates it, and the command returns the string's length after modification.
Follow-up: What happens if the key contains a non-string value? The command requires a string value, so it cannot append to another Redis data type.
Its syntax is APPEND key value. The value is appended to the existing string, or used as the initial content of a newly created key.
Follow-up: How can a caller verify the resulting content? Use GET on the same key after APPEND, as in the documentation example.
APPEND returns an integer equal to the length of the string after the append. Its amortized complexity is O(1) when the appended value is small, because Redis grows its dynamic string capacity efficiently.
Follow-up: Why is the complexity amortized rather than unconditionally constant? Reallocations can require additional work, especially as the stored string grows.
SETGET