InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Linux/Unix Command Line tutorials

  • Awk
  • Curl
  • Edit a file without changing its timestamp on Linux
  • Find which process is listening on a port on Linux
  • Get file size in bytes on Linux
  • How to delete files starting with dash/hyphen
  • How to kill unresponsive ssh session using escape sequence
  • How to show environment variable for a process id (pid)
  • How to sort using a specific field on Linux
  • How to specify environment variable for a command on Linux
  • How to zip/unzip a directory with password
  • ImageMagick
  • Impact on LC_ALL on Linux sort
  • Linux - find listening ports
  • Linux - find top directories by used disk size (excluding size of subdirectories)
  • Linux - how to run a command as different user
  • Linux - list only directories
  • Linux - providing sudo access to a users - some best practices
  • Linux - sending mail from command using mailutils
  • Linux file timestamps
  • Linux how to modify a user using usermod
  • Linux ping a port using netcat
  • Linux replace comma with newline
  • Linux screen - quick start guide
  • Linux what package provides a file
  • Linux/Unix - How to go to previous directory
  • Linux/Unix - find inode number of a file
  • Linux/Unix - truncate a large log file without deleting it
  • Linux/Unix history with date and time
  • Memcache - how to dump all keys and values on command line
  • Mongo - cli quick start guide
  • Perl command line - replace multi line comments
  • Python/Perl/Unix one liners
  • Rsync
  • Ruby gem - handy reference
  • SSH
  • Some handy linux gnu date commands
  • Use watch to monitor a command at some frequency on Linux
  • bower - installation and quick start guide
  • csvkit - parse csv file and data on Linux command line
  • ffmpeg
  • grep without regex (fixed string)
  • redis cli quick start tutorial
  • wget handy commands

Linux find

  • Find recently modified files on Linux
  • Linux - find and delete files older than 30 days
  • Linux - find files containing specific text
  • Linux find - ignore case in name
  • find - exclude directory or file pattern
 
  • Home
  • > Tutorials
  • > Linux/Unix Command Line

Memcache – how to dump all keys and values on command line

By admin | Last updated on Mar 20, 2016

Memcache is a very simple, distributed memory cache used widely with mysql and other databases. Often we want to inspect what is in memcache server on development environment. Here are some handy command which can be used in development environment. It may not be suitable for production environment with huge data. This article assumes that you have memcached running on local machine (Linux or Mac).

Create some test keys

This step is optional. We’ll create some test keys (600 seconds expiry). You can skip this if you already have data in memcache and don’t want to add test data to it.

$ printf "%04g\n" {1..5} | xargs -I % printf "set tmpkey% 0 600 12\r\ntmpvalue%\r\n" | nc localhost 11211
STORED
STORED
STORED
...

Dumping slabs

Use the following commands to list all items which will give us all slabs.

$ MEMCHOST=localhost; printf "stats items\n" | nc $MEMCHOST 11211

For printing total number of keys (from all slabs)

$ MEMCHOST=localhost; printf "stats items\n" | nc $MEMCHOST 11211 | grep ":number" | awk '{print $3}' | awk '{SUM+=$1} END {print SUM}'

For printing exact slab ids

$ MEMCHOST=localhost; printf "stats items\n" | nc $MEMCHOST 11211 | grep ":number" | awk -F":" '{print $2}'

Dumping all keys using slabs

Run the following command to dump upto 10000 keys values from all slabs. You can change 10000 to a higher value if needed. This also prints the expiry timestamp of the key. Note that in case if key never expires its value if less than memcache server start time (current time – up_time).

$ MEMCHOST=localhost; printf "stats items\n" | nc $MEMCHOST 11211 | grep ":number" | awk -F":" '{print $2}' | xargs -I % printf "stats cachedump % 10000\r\n" | nc $MEMCHOST 11211
ITEM tmpkey0005 [12 b; 1438189134 s]
ITEM tmpkey0004 [12 b; 1438189134 s]
ITEM tmpkey0003 [12 b; 1438189134 s]
ITEM tmpkey0002 [12 b; 1438189134 s]
ITEM tmpkey0001 [12 b; 1438189134 s]
END

Use 0 for dumping all keys (use with caution).

$ MEMCHOST=localhost; printf "stats items\n" | nc $MEMCHOST 11211 | grep ":number" | awk -F":" '{print $2}' | xargs -I % printf "stats cachedump % 0\r\n" | nc $MEMCHOST 11211

Dumping all keys and values using slabs

Run the following command to dump values along with keys using slabs.

$ MEMCHOST=localhost; printf "stats items\n" | nc $MEMCHOST 11211 | grep ":number" | awk -F":" '{print $2}' | xargs -I % printf "stats cachedump % 0\r\n" | nc $MEMCHOST 11211 | grep ITEM | awk '{print $2}' | sed -e 's/"/\\"/g'| xargs -I % printf "get %\r\n" | nc $MEMCHOST 11211

Suggested posts:

  1. migrate multiple mysql databases using mysqldump on Linux
  2. NFS client and server handy commands
  3. Generate ssh public key from private key on command line
  4. PHP apc – setup and performance benchmarks on Ubuntu Linux
  5. Perl command line – replace multi line comments
  6. Curl – follow redirects
  7. How to bypass cross site scripting (XSS) protection by browsers
  8. Linux replace comma with newline
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Linux/Unix Command Line, Mac, Memcache, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress