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 07:55

Memcached can actually write to a logfile on its own, without having to resort to restarting it manually. The /etc/init.d/memcached init script can call memcached with the options specified in /etc/memcached.conf. Among these options are verbosity and log file path.

In short, you just need to add (or uncomment) these two lines to /etc/memcached.conf:

-vv
logfile /path/to/log

...and restart the daemon with service memcached restart or /etc/init.d/memcached restart

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

查看更多
虎瘦雄心在
3楼-- · 2019-01-30 07:56

Start memcache not as a daemon but normal, so just run memcached -vv for very verbose. You will see when get's and sets come in to the memcache server.

查看更多
地球回转人心会变
4楼-- · 2019-01-30 07:56

I wrote an expect script is-memcached-running that tests if memcached is running on a host/port combination (run 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 run your system from a Makefile rule, you could make your startup depend on a make target that asserts it is up and running (or helps you get that state). It is verbose when the check fails to make it easy for us to debug failed ci runs, installs memcached when it's missing, and is brief and to the point 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
查看更多
\"骚年 ilove
5楼-- · 2019-01-30 08:03

You could use telnet and the stats command e.g.:

# 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
查看更多
We Are One
6楼-- · 2019-01-30 08:03

Can you use curl to fetch a page a few hundred times and time the results? You could also look at running a process on the server that simulates heavy CPU/disk load while doing this.

查看更多
我只想做你的唯一
7楼-- · 2019-01-30 08:03

From the command line, try below command

echo stats | nc 127.0.0.1 11211*

If it doesn't return anything, memcache isn't running. Otherwise it should return a bunch of stats including uptime (and hit and miss counts)

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

查看更多
登录 后发表回答