Memcached - Append Data
Memcached - Appending Data
Section titled “Memcached - Appending Data”The Memcached append command adds data to the end of an existing value. This operation is atomic but less common than set or cas. It’s useful for scenarios where you need to build up a value over time, such as logging or tracking, without fetching the entire value first.
Important: append only works if the key already exists. It will not create a new key.
Protocol Syntax
Section titled “Protocol Syntax”The basic syntax for the append command is:
append key flags exptime bytes [noreply]valueThe parameters flags and exptime are currently ignored by the server but are required for syntax compatibility. The bytes and value are the data you wish to append.
Expected Responses
Section titled “Expected Responses”STOREDindicates the data was successfully appended.NOT_STOREDindicates that the append failed, most commonly because the key does not exist.CLIENT_ERRORindicates a syntax error.
Telnet Example
Section titled “Telnet Example”This example shows that append fails on a non-existent key, then succeeds after the key has been created with set.
# 1. Attempt to append to a key that doesn't exist.append activity_log 0 0 7,startNOT_STORED
# 2. Set an initial value for the key.set activity_log 0 900 4initSTORED
# 3. Append to the existing key.append activity_log 0 900 7,startSTORED
# 4. Check the result.get activity_logVALUE activity_log 0 11init,startEND
# 5. Append again.append activity_log 0 900 5,stopSTORED
# 6. Check the final result.get activity_logVALUE activity_log 0 16init,start,stopENDModern Application: Appending Data with Python
Section titled “Modern Application: Appending Data with Python”Here’s how to use the append functionality with the pymemcache library in Python.
Setup and Dependencies
Section titled “Setup and Dependencies”Ensure pymemcache is installed:
pip install pymemcachePython Code Example
Section titled “Python Code Example”from pymemcache.client.base import Client
MEMCACHED_SERVER = ('127.0.0.1', 11211)
def run_append_example(): """Demonstrates the append operation.""" client = Client(MEMCACHED_SERVER) key = 'audit_trail:user:77'
# Ensure we have a clean slate client.delete(key)
try: # 1. Attempt to append to a non-existent key # Note: Appending bytes is required. was_appended = client.append(key, b':login_failed') print(f"-> APPEND to non-existent key '{key}'... Success: {was_appended}")
# 2. Set an initial value # Note: Values must be bytes, strings, or serializable. client.set(key, b'2023-10-27T10:00:00Z:session_created', expire=600) print(f"-> SET: Initialized key '{key}'.")
# 3. Append to the existing key was_appended = client.append(key, b'\n2023-10-27T10:01:00Z:profile_viewed') print(f"-> APPEND to existing key... Success: {was_appended}")
# 4. Append another event was_appended = client.append(key, b'\n2023-10-27T10:02:00Z:logged_out') print(f"-> APPEND again... Success: {was_appended}")
# 5. Retrieve the final concatenated value final_value = client.get(key) if final_value: print("\n--- Final Value in Cache ---") print(final_value.decode('utf-8')) print("--------------------------")
except Exception as e: print(f"An unexpected error occurred: {e}") finally: client.close()
if __name__ == "__main__": run_append_example()Expected Output
Section titled “Expected Output”-> APPEND to non-existent key 'audit_trail:user:77'... Success: False-> SET: Initialized key 'audit_trail:user:77'.-> APPEND to existing key... Success: True-> APPEND again... Success: True
--- Final Value in Cache ---2023-10-27T10:00:00Z:session_created2023-10-27T10:01:00Z:profile_viewed2023-10-27T10:02:00Z:logged_out--------------------------Best Practices and Considerations
Section titled “Best Practices and Considerations”- Data Format:
appendoperates at the byte level. It does not understand data structures like JSON or lists. If you store a serialized JSON string and append to it, you will likely create invalid JSON. It is best used for raw text, logs, or comma-separated values. - Alternative to Read-Modify-Write: The main advantage of
appendis avoiding aget-> concatenate ->setcycle, which reduces network traffic and eliminates the need for CAS if you are only ever adding to the end of a value. prependCommand: A correspondingprependcommand also exists, which adds data to the beginning of the value. It has the same syntax and constraints asappend.- Limited Use Cases: In modern systems, structured data (like JSON) is more common. For lists, it’s often better to use a tool designed for data structures, like Redis Lists. Therefore,
appendhas more niche applications today.