Memcached - Clear Data
Memcached - Clearing All Data (Flush)
Section titled “Memcached - Clearing All Data (Flush)”The flush_all command invalidates all existing items in the Memcached server immediately or after a specified delay. It is a powerful but destructive command that should be used with extreme caution, especially in a production environment.
Warning
Section titled “Warning”Executing flush_all will wipe the entire cache for that Memcached instance. This can cause a “thundering herd” problem, where a flood of requests suddenly hits your backend database or services, potentially overwhelming them. This command is typically used in development or testing environments to reset the cache state.
Syntax
Section titled “Syntax”The basic syntax for the flush_all command is:
flush_all [delay] [noreply]The optional delay parameter specifies a number of seconds after which all items will be invalidated. The server will always respond with OK.
Command Line Example
Section titled “Command Line Example”In this example, we set a couple of keys, verify they exist, then flush the cache and confirm they are gone.
# Set some dataset key1 0 900 5helloSTOREDset key2 0 900 5worldSTORED
# Verify data existsget key1VALUE key1 0 5helloEND
# Flush the entire cacheflush_allOK
# Attempt to get the data againget key1ENDget key2ENDFlushing Data with a Modern Java Client
Section titled “Flushing Data with a Modern Java Client”Modern client libraries provide a flush() method to execute this command. The following example shows how to use it with spymemcached in a Java application.
1. Add Dependency
Section titled “1. Add Dependency”Ensure the spymemcached dependency is in your Maven pom.xml:
<dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.12.3</version></dependency>2. Java Code Example
Section titled “2. Java Code Example”This program sets two keys, verifies their existence, calls flush(), and then confirms that the keys have been invalidated.
import net.spy.memcached.MemcachedClient;import net.spy.memcached.AddrUtil;import net.spy.memcached.internal.OperationFuture;
import java.io.IOException;import java.util.concurrent.ExecutionException;
public class MemcachedFlushExample { public static void main(String[] args) { String serverAddress = "127.0.0.1:11211";
try (MemcachedClient client = new MemcachedClient(AddrUtil.getAddresses(serverAddress))) { System.out.println("Connection to Memcached server successful.");
// 1. Set a few keys client.set("config:feature_flags", 0, "{\"new_dashboard\": true}").get(); client.set("user:45:session", 0, "active").get(); System.out.println("Set initial keys: 'config:feature_flags' and 'user:45:session'.");
// 2. Verify keys exist before flushing System.out.println("Value of 'config:feature_flags' before flush: " + client.get("config:feature_flags")); System.out.println("Value of 'user:45:session' before flush: " + client.get("user:45:session"));
// 3. Flush the cache System.out.println("\n--- Flushing the cache --- "); OperationFuture<Boolean> flushFuture = client.flush(); if (flushFuture.get()) { System.out.println("Cache flush command sent successfully."); } else { System.err.println("Cache flush failed."); } // Note: The flush may not be instantaneous. Thread.sleep(100); // Small delay to allow flush to propagate
// 4. Verify keys are gone after flushing System.out.println("Value of 'config:feature_flags' after flush: " + client.get("config:feature_flags")); System.out.println("Value of 'user:45:session' after flush: " + client.get("user:45:session"));
} catch (IOException | InterruptedException | ExecutionException e) { System.err.println("An error occurred: " + e.getMessage()); Thread.currentThread().interrupt(); } }}Output
Section titled “Output”When you run this Java code, the output will be:
Connection to Memcached server successful.Set initial keys: 'config:feature_flags' and 'user:45:session'.Value of 'config:feature_flags' before flush: {"new_dashboard": true}Value of 'user:45:session' before flush: active
--- Flushing the cache ---Cache flush command sent successfully.Value of 'config:feature_flags' after flush: nullValue of 'user:45:session' after flush: null