How do you know memcached is doing something?

I am testing using memcached to cache django views. How can I determine if memcached really caches anything from the Linux command line?

+71
django memcached django-cache
Mar 10 '09 at 19:30
source share
13 answers

An easy way to test memcache was to sneak into the marked timestamp on each page that is up. If the timestamp remains the same for several requests per page, the page was cached by memcache.

In Django’s settings, I also configure the caching mechanism to use the file cache in the file system (very slowly), but after getting to the pages I could see that the actual cache files were placed in the file path so that I could confirm the caching was active in Django.

I used both of these steps to solve my caching problem. In fact, I did not have caching in Django. A new way to activate caching is to use the django.middleware.cache.CacheMiddleware middleware (and not the middleware with two middleware, which should be the first / last middleware settings.)

+9
Mar 18 '09 at 14:47
source share
β€” -

I know this question is old, but here is another useful approach for testing memcached with django:

As @Jacob mentioned, you can run memcached in very verbose mode (and not as a daemon):

memcached -vv 

To check the configuration of the django cache, you can use the low-level api cache.

  • First run the python interpreter and load the settings for the django project:

     python manage.py shell 
  • From the shell, you can use the low-level api cache to check memcache server:

     from django.core.cache import cache cache.set('test', 'test value') 

If your cache configuration is correct, you should see memcache output similar to this:

 <32 set :1:test 0 300 10 >32 STORED 
+43
Aug 08 '11 at 23:18
source share

You can use the telnet command and the statistics command, for example:

 # telnet localhost [memcacheport] Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. stats STAT pid 2239 STAT uptime 10228704 STAT time 1236714928 STAT version 1.2.3 STAT pointer_size 32 STAT rusage_user 2781.185813 STAT rusage_system 2187.764726 STAT curr_items 598669 STAT total_items 31363235 STAT bytes 37540884 STAT curr_connections 131 STAT total_connections 8666 STAT connection_structures 267 STAT cmd_get 27 STAT cmd_set 30694598 STAT get_hits 16 STAT get_misses 11 STAT evictions 0 STAT bytes_read 2346004016 STAT bytes_written 388732988 STAT limit_maxbytes 268435456 STAT threads 4 END 
+42
Mar 10 '09 at 19:59
source share

Run memcache not as a daemon, but normal, so just run memcached -vv for very verbose. You will see when get and sets enter the memcache server.

+42
Apr 09 '09 at 16:35
source share

Memcached can actually write to the log file itself, without having to restart it manually. The initialization script /etc/init.d/memcached ( /usr/lib/systemd/system/memcached.service on EL7 +; ugh) can call memcached with the parameters specified in /etc/memcached.conf (or /etc/sysconfig/memcached on EL5 +). Among these parameters are the detail and path to the log file.

In short, you just need to add (or uncomment) these two lines to this conf / sysconfig file ...

 -vv logfile /path/to/log 

... and restart the daemon using service memcached restart (EL3-7) or /etc/init.d/memcached restart (debuntus)

And then you can track this log in the traditional way, for example, tail -f/path/to/log .

+6
Mar 07 '14 at 1:34
source share

For Node's extended answer, you can use socat UNIX-CONNECT:/var/run/memcached.sock STDIN to debug a unix socket.

Example:

 $ socat UNIX-CONNECT:/var/run/memcached.sock STDIN stats STAT pid 931 STAT uptime 10 STAT time 1378574384 STAT version 1.4.13 STAT libevent 2.0.19-stable STAT pointer_size 32 STAT rusage_user 0.000000 STAT rusage_system 0.015625 STAT curr_connections 1 STAT total_connections 2 STAT connection_structures 2 
+4
Sep 07 '13 at 17:17
source share

You can test memcached or any server below script

 lsof -i :11211 | grep 'LISTEN'>/dev/null 2>/dev/null;echo $? 

if it returns 0, then the server is actually running or if it is not, if you want to know that the server is actually running on some port, use the following script

 lsof -i :11211 | grep 'LISTEN'>/dev/null 2>/dev/null; if [ $? -eq 0]; then echo "Your memcache server is running" else echo "No its not running" fi 
+3
Aug 25 '13 at 22:20
source share

In Bash, you can check memcache statistics with this command:

 exec 3<>/dev/tcp/localhost/11211; printf "stats\nquit\n" >&3; cat <&3 

To clear the cache, use the memflush command:

 echo flush_all >/dev/tcp/localhost/11211 

and check if the statistics have increased.

To reset all cached objects, use memdump or memcdump (part of the memcached / libmemcached ):

 memcdump --servers=localhost:11211 

or:

 memdump --servers=localhost:11211 

If you use PHP to find out if it is supported check: php -i | grep memcached php -i | grep memcached php -i | grep memcached php -i | grep memcached .




trace

To check which particular memcached process is processing, you can use network sniffers or debuggers (for example, strace on Linux or dtrace / dtruss on Unix / OS X). Check out some examples below.

Strain

 sudo strace -e read,write -fp $(pgrep memcached) 

To better format the output, check: How to parse strace in a shell into plain text?

Dtruss

Dtruss is a dtrace shell available on Unix systems. Run this as:

 sudo dtruss -t read -fp $(pgrep memcached) 

Tcpdump

 sudo tcpdump -i lo0 -s1500 -w- -ln port 11211 | strings -10 
+3
Apr 12 '16 at 17:18
source share

Can you use curl to get the page a few hundred times and time results? You can also see how the process runs on the server, which simulates the load on the processor / disk at the same time.

+1
Mar 10 '09 at 19:37
source share

I wrote an expect script is-memcached-running that checks if memcached is running in a host / port combination (running as is-memcached-running localhost 11211 ):

 #! /usr/bin/env expect set timeout 1 set ip [lindex $argv 0] set port [lindex $argv 1] spawn telnet $ip $port expect "Escape character is '^]'." send stats\r expect "END" send quit\r expect eof 

If you start your system from a Makefile rule, you can make your launch dependent on a target that claims to be up and running (or helps you get this state). This circumstance, when the check does not allow us to easily debug broken qi, starts memcached when it is absent, and is short and otherwise:

 #! /bin/bash if [[ "$(type -P memcached)" ]]; then echo 'memcached installed; checking if it is running' memcached_debug=`mktemp memcache-check.XXXXX` if is-memcached-running localhost 11211 >$memcached_debug 2>&1; then echo 'Yep; memcached online' else cat $memcached_debug echo echo '****** Error: memcached is not running! ******' if [[ "$OSTYPE" =~ ^darwin ]]; then echo echo 'Instructions to auto-spawn on login (or just start now) are shown' echo 'at the end of a "brew install memcached" run (try now, if you did' echo 'not do so already) or, if you did, after a "brew info memcached".' echo fi exit 1 fi rm -f $memcached_debug else echo memcached was not found on your system. if [[ "$OSTYPE" =~ ^darwin ]]; then brew install memcached elif [[ "$OSTYPE" =~ ^linux ]]; then sudo apt-get install memcached else exit 1 fi fi 
+1
Jun 23 '13 at 21:51
source share

At the command prompt, try the command

Echo Statistics | nc 127.0.0.1 11211 *

If it returns nothing, memcache is not running. Otherwise, it should return a bunch of statistics, including uptime (and the number of hits and passes)

Help article here, https://www.percona.com/blog/2008/11/26/a-quick-way-to-get-memcached-status/

+1
Aug 07 '15 at 12:22
source share

I use Mezzanine, and the only answer that worked for me was Jacobs's answer. Thus, stopping the daemon and starting memcached -vv

0
Dec 09 '13 at 17:37
source share

After posting to Aryashree, this helped me get an error if memcached doesn't work locally:

 import subprocess port=11211 res=subprocess.Popen('echo stats | nc 127.0.0.1 %d' % (port), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) if res.stdout: lines=res.stdout.read() lineArr=lines.split('\r\n') pidlineArr=lineArr[0].split(' ') pid=pidlineArr[len(pidlineArr)-1] print("[MemCached] pid %s Running on port %d" % (pid, port)) else: raise RuntimeError("No Memcached is present on port %d" % port) 
0
Mar 21 '18 at 14:17
source share



All Articles