Introduction

In environments with multiple Memcache servers, it may be necessary to remove the same cache key from all nodes to ensure data consistency. This Python script allows administrators to delete a single Memcache key or multiple Memcache keys across a specified Memcache cluster.

Prerequisites

  • Python installed on the server
  • python-memcache module installed
  • Fabric Python module installed
  • SSH access to the server where the script will be executed
  • Connectivity to all Memcache servers on the configured Memcache port (default: 11211)
  • The Memcache cluster hosts configured in the script

Implementation

Step 1

Create a Python script and paste the Memcache key deletion script into the file.

Step 2

Configure the Memcache cluster details in the _MEMCACHE_CLUSTER_ section.

Example:

  • Cluster1 → IPAddress1:<port>, IPAddress2:<port>
  • Cluster2 → IPAddress3:<port>, IPAddress4:<port>

Step 3

Save the script and ensure the required Python modules are installed.

Step 4

To delete a single Memcache key from a cluster, execute:

fab delete_memcache:delete_key=keyname,hosts_group=Cluster1

Replace keyname with the cache key and Cluster1 with the appropriate cluster name.

Step 5

To delete multiple Memcache keys from a cluster, execute:

fab delete_memcache:delete_multiple_keys=keyname1;keyname2,hosts_group=Cluster1

Separate multiple keys using a semicolon (;).

Step 6

Verify that the specified cache keys have been removed successfully from all Memcache servers in the selected cluster.

Conclusion

This script simplifies Memcache management by allowing administrators to remove one or more cache keys across multiple Memcache servers simultaneously. Using a centralized approach helps maintain cache consistency across the cluster and reduces the effort required to clear stale or outdated cache entries.

Leave a Reply