How do you know if memcached is doing anything?

2019-01-30 07:32发布

I'm testing out using memcached to cache django views. How can I tell if memcached is actually caching anything from the Linux command line?

13条回答
劳资没心,怎么记你
2楼-- · 2019-01-30 08:04

You can test memcached or any server by 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 1 its not so 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楼-- · 2019-01-30 08:05

Simple way to test for memcache working was to sneak in a commented out timestamp on every page served up. If the timestamp stayed the same on multiple requests to a page, then the page was being cached by memcache.

In Django settings, I also setup the cache mechanism to use a file cache on the filesystem (really slow), but after hitting up the pages I could see that there were actual cache files being placed in the file path so I could confirm caching was active in Django.

I used both these steps to work out my caching problem. I actually did not have caching turned on correctly in Django. The newer method to activate caching is using the 'django.middleware.cache.CacheMiddleware' middleware (not the middleware with two middleware pieces that have to be the first/last middleware settings.)

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-30 08:07

For extend Node's response, 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
查看更多
Juvenile、少年°
5楼-- · 2019-01-30 08:07

Following Aryashree post, this helped me to get an error if memcached not running 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)
查看更多
时光不老,我们不散
6楼-- · 2019-01-30 08:11

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

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

memcached -vv

To test your django cache config, you can use the low-level cache api.

  1. First, start up the python interpreter and load your django project settings:

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

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

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

<32 set :1:test 0 300 10
>32 STORED
查看更多
Rolldiameter
7楼-- · 2019-01-30 08:11

In Bash, you can check the statistics of memcache by this command:

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

To flush the cache, use memflush command:

echo flush_all >/dev/tcp/localhost/11211

and check if the stats increased.

To dump all the cached objects, use memdump command (part of memcached/libmemcached package):

memdump --servers=localhost:11211

If you're using PHP, to see whether is supported, check by: php -i | grep memcached.


Tracing

To check what memcached process is exactly processing, you can use network sniffers or debuggers (e.g. strace on Linux or dtrace/dtruss on Unix/OS X) for that. Check some examples below.

Strace

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

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

Dtruss

Dtruss is a dtrace wrapper which is available on Unix systems. Run it as:

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

Tcpdump

sudo tcpdump -i lo0 -s1500 -w- -ln port 11211 | strings -10
查看更多
登录 后发表回答