Introduction
When multiple Memcache servers are used in a cluster, removing the same cache key from every server manually can be time-consuming.
The following Python script can be used to delete a single Memcache key or multiple Memcache keys across multiple Memcache hosts. The hosts can be grouped into different Memcache clusters, allowing you to target a specific cluster when running the script.
Prerequisites
Before using the script, make sure the following requirements are satisfied:
- The script assumes that you have multiple Memcache clusters listening on different ports. Categorize the Memcache hosts under the
MEMCACHE_CLUSTERsection. - The machine running the script must have the Python Memcache module installed.
- The origin host must be able to connect to the Memcache port, which is
11211by default, on all Memcache servers.
Implementation
Step 1: Configure the Memcache Clusters
The following script contains the Memcache cluster configuration and functions required to delete cache keys:
#!/usr/bin/python
########Script to Delete Memcache Key########
#Author:Dhanasekaran N
#Email:support@pheonixsolutions.com
#Version:1.0
#############################################
import memcache
from fabric import *
from fabric.api import *
#Mention your Cluster hosts and unique name
_MEMCACHE_CLUSTER_ = {
“Cluster1” :[‘IPAddress1:<port>’,’IPaddress2:<port>’],
“Cluster2” :[‘IPaddress3:<port>’,’IPaddress:<port>’],
}
def _run_command(host_name,verify_key):
mc = memcache.Client([host_name], debug=0)
#run(‘pwd’);
print “key name:” + verify_key;
checked_key=mc.get(verify_key);
print checked_key;
if(checked_key is None):
print “The key %s doesn’t exist ” %(verify_key)
else:
print “Deleting %s from %s” %(verify_key,host_name)
mc.delete(verify_key)
def delete_memcache(delete_key=’none’,hosts_group=’none’,delete_multiple_keys=’none’):
all_groups=_MEMCACHE_CLUSTER_.keys();
#print delete_multiple_keys;
if (delete_key==’none’ and delete_multiple_keys==’none’):
print “No delete Key or delete_multiple_key Mentioned”;
print “Eg:fab delete_memcache:delete_key=’some_key1′,hosts_group=’java_api'”
elif(hosts_group ==’none’):
print “Mention the host Group while running fab”
print _MEMCACHE_CLUSTER_.keys();
else:
if(delete_multiple_keys!=’none’):
if(delete_multiple_keys):
delete_multiple_keys = delete_multiple_keys.split(“;”)
for cache_cluster in all_groups:
if(cache_cluster == hosts_group):
#print _MEMCACHE_CLUSTER_[cache_cluster];
for cache_cluster_hosts in _MEMCACHE_CLUSTER_[cache_cluster]:
print cache_cluster_hosts;
for delete_key in delete_multiple_keys:
execute(_run_command,hosts=cache_cluster_hosts,host_name=cache_cluster_hosts,verify_key=delete_key);
else:
for cache_cluster in all_groups:
if(cache_cluster == hosts_group):
for cache_cluster_hosts in _MEMCACHE_CLUSTER_[cache_cluster]:
print cache_cluster_hosts;
execute(_run_command,hosts=cache_cluster_hosts,host_name=cache_cluster_hosts,verify_key=delete_key);
Step 2: Delete a Single Memcache Key
To delete a single key from all hosts belonging to Cluster1, use:
fab delete_memcache:delete_key=keyname,hosts_group=Cluster1
This command deletes the specified key from each Memcache host configured under Cluster1.
Step 3: Delete Multiple Memcache Keys
To delete multiple keys from a specific cluster, separate the key names using ;:
fab delete_memcache:delete_multiple_keys=keyname1;keyname2,hosts_group=Cluster1
This executes the deletion for each specified key across the hosts configured in Cluster1.
Step 4: Verify the Key Before Deletion
The script first checks whether the specified key exists:
checked_key=mc.get(verify_key);
If the key does not exist, the script displays a message indicating that the key is unavailable. If the key exists, it proceeds with the deletion:
mc.delete(verify_key)
This provides a basic verification before removing the cache entry.
Conclusion
Managing cache keys across multiple Memcache servers can be difficult when performed manually. This Python and Fabric-based script simplifies the process by allowing administrators to delete a single key or multiple keys across a selected Memcache cluster.
By grouping Memcache hosts into clusters, the required cache entries can be removed from the appropriate servers using a single command.
FAQs
1. What is the purpose of this Memcache script?
The script is used to delete one or more Memcache keys across multiple hosts belonging to a configured cluster.
2. Can I delete multiple Memcache keys at once?
Yes. Multiple keys can be supplied using the delete_multiple_keys option, with each key separated by a semicolon.
3. What port does Memcache use by default?
Memcache commonly uses port 11211. The script requires the origin server to have network connectivity to the configured Memcache hosts and ports.
4. Can I target a specific Memcache cluster?
Yes. The hosts_group parameter allows you to specify the cluster, such as Cluster1 or Cluster2, where the keys should be deleted.
Related Articles
- PHP Fatal Error: Class ‘Memcache’ Not Found – Learn how to troubleshoot the PHP Memcache extension when the
Memcacheclass is unavailable in a PHP application.
Read the article
Talk to our experts
Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.