你怎么知道 memcached 是否在做任何事情?

发布于 2024-07-15 00:43:34 字数 79 浏览 12 评论 0原文

我正在测试使用 memcached 来缓存 django 视图。 如何从 Linux 命令行判断 memcached 是否确实缓存了任何内容?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

梦与时光遇 2024-07-22 00:43:34

您可以使用官方 perl 脚本:

memcached-tool 127.0.0.1:11211 stats 

或者只使用 telnet 和 stats 命令,例如:

# 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

You could use the official perl script:

memcached-tool 127.0.0.1:11211 stats 

Or just 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
醉态萌生 2024-07-22 00:43:34

我知道这个问题已经很老了,但是这里是使用 django 测试 memcached 的另一种有用方法:

正如 @Jacob 提到的,您可以以非常详细的模式启动 memcached (不是作为守护进程):

memcached -vv

要测试您的 django 缓存配置,您可以使用低级缓存 API。

  1. 首先,启动 python 解释器并加载 django 项目设置:

    python manage.py shell 
      
  2. 从 shell 中,您可以使用低级缓存 api 来测试您的 memcache 服务器:

    从 django.core.cache 导入缓存 
      cache.set('测试', '测试值') 
      

如果您的缓存配置正确,您应该在 memcache 中看到类似于以下内容的输出:

<32 set :1:test 0 300 10
>32 STORED

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
难如初 2024-07-22 00:43:34

启动 memcache 不是作为守护进程,而是正常启动,因此只需运行 memcached -vv 即可获得非常详细的信息。 您将看到 get 和 set 何时进入内存缓存服务器。

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.

怪异←思 2024-07-22 00:43:34

测试内存缓存工作的简单方法是在提供的每个页面上潜入注释掉的时间戳。 如果对某个页面的多个请求中时间戳保持相同,则该页面正在被 memcache 缓存。

在 Django 设置中,我还设置了缓存机制以使用文件系统上的文件缓存(非常慢),但是在打开页面后,我可以看到实际的缓存文件被放置在文件路径中,因此我可以确认缓存是活跃在 Django 中。

我使用这两个步骤来解决我的缓存问题。 实际上我没有在 Django 中正确打开缓存。 激活缓存的较新方法是使用“django.middleware.cache.CacheMiddleware”中间件(不是具有两个必须是第一个/最后一个中间件设置的中间件的中间件。)

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.)

世界等同你 2024-07-22 00:43:34

从命令行尝试以下命令:

echo stats | nc 127.0.0.1 11211

如果没有返回任何内容,则 memcache 未运行。 否则它应该返回一堆统计数据,包括正常运行时间(以及命中和未命中计数)

参考文章在这里,
https://www.percona.com/blog/2008/11/26/a-quick-way-to-get-memcached-status/" percona.com/blog/2008/11/26/a-quick-way-to-get-memcached-status/

每 2 秒查看一次更改:

watch "echo stats | nc 127.0.0.1 11211"

From the command line, try the command below:

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/

To see changes every 2 seconds:

watch "echo stats | nc 127.0.0.1 11211"
橙味迷妹 2024-07-22 00:43:34

在 Bash 中,您可以通过以下命令检查 memcache 的统计信息:

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

要刷新缓存,请使用 memflush 命令:

echo flush_all >/dev/tcp/localhost/11211

并检查统计信息是否增加。

要转储所有缓存的对象,请使用 memdumpmemcdump 命令(memcached/libmemcached 包的一部分):

memcdump --servers=localhost:11211

或:

memdump --servers=localhost:11211

如果您使用的是 PHP,要查看是否支持,请检查: php -i | grep memcached


跟踪

要检查 memcached 进程正在处理什么,您可以使用网络嗅探器或调试器(例如 Linux 上的 strace 或 Unix 上的 dtrace/dtruss) OS X)为此。 检查下面的一些示例。

Strace

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

要以更好的方式格式化输出,请检查:如何将 shell 中的 strace 解析为纯文本?

Dtruss

Dtruss 是一个 dtrace Unix 系统上可用的包装器。 运行它:

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

Tcpdump

sudo tcpdump -i lo0 -s1500 -w- -ln port 11211 | strings -10

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 or memcdump command (part of memcached/libmemcached package):

memcdump --servers=localhost:11211

or:

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
不爱素颜 2024-07-22 00:43:34

为了扩展 Node 的响应,您可以使用 socat UNIX-CONNECT:/var/run/memcached.sock STDIN 来调试 unix 套接字。

例子:

$ 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

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
强者自强 2024-07-22 00:43:34

Memcached 实际上可以自行写入日志文件,而无需手动重新启动它。 /etc/init.d/memcached 初始化脚本(EL7+ 上的/usr/lib/systemd/system/memcached.service;呃)可以使用指定的选项调用 memcached在 /etc/memcached.conf(或 EL5+ 上的 /etc/sysconfig/memcached)中。 这些选项包括详细程度和日志文件路径。

简而言之,您只需将这两行添加(或取消注释)到该 conf/sysconfig 文件...

-vv
logfile /path/to/log

...并使用 service memcached restart(EL3-7) 或 /etc/init.d/memcached restart(debuntus)

然后你可以用传统的方式监控这个日志,例如tail -f /path/to/log

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 (/usr/lib/systemd/system/memcached.service on EL7+; ugh) can call memcached with the options specified in /etc/memcached.conf (or /etc/sysconfig/memcached on EL5+). Among these options are verbosity and log file path.

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

-vv
logfile /path/to/log

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

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

高跟鞋的旋律 2024-07-22 00:43:34

我编写了一个 expect 脚本 is-memcached-running 来测试 memcached 是否在主机/端口组合上运行(作为 is-memcached-running localhost 11211< /code>):

#! /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

如果您从 Makefile 规则运行系统,则可以使您的启动依赖于断言系统已启动并正在运行(或帮助您获取该状态)的 make 目标。 当检查失败时,它是冗长的,使我们能够轻松调试失败的 ci 运行,当 memcached 丢失时,它会安装它,否则,它是简短而切中要点的:

#! /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

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
樱娆 2024-07-22 00:43:34

您可以使用curl 获取页面数百次并对结果进行计时吗? 您还可以考虑在服务器上运行一个进程,在执行此操作时模拟高 CPU/磁盘负载。

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.

长途伴 2024-07-22 00:43:34

您可以通过下面的脚本测试 memcached 或任何服务器,

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

如果它返回 0,则服务器实际上正在运行,如果返回 1,则不是,如果您想知道服务器实际上正在某个端口上运行,请使用以下脚本

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

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
跨年 2024-07-22 00:43:34

如果您使用的是 RHEL 或 Centos 8
要将 memcached 日志内容保存到 /var/log/messages(无需旋转即可快速)

https://serverfault.com/questions/208538/how-to-specify-the-log-file-for-memcached-on-rhel-centos/1054741#1054741

If you're using RHEL or Centos 8
To get memcached log stuff to /var/log/messages (quick without rotation)

https://serverfault.com/questions/208538/how-to-specify-the-log-file-for-memcached-on-rhel-centos/1054741#1054741

酒几许 2024-07-22 00:43:34

在 Aryashree 帖子之后,如果 memcached 未在本地运行,这会帮助我收到错误:

import subprocess

port = 11211
res = subprocess.Popen(f"echo stats | nc 127.0.0.1 {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[-1]
    print(f"[MemCached] pid {pid} Running on port {port}")

else:
    raise RuntimeError(f"No Memcached is present on port {port}")

Following Aryashree post, this helped me to get an error if memcached not running locally:

import subprocess

port = 11211
res = subprocess.Popen(f"echo stats | nc 127.0.0.1 {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[-1]
    print(f"[MemCached] pid {pid} Running on port {port}")

else:
    raise RuntimeError(f"No Memcached is present on port {port}")
隱形的亼 2024-07-22 00:43:34

我正在使用夹层,唯一对我有用的答案是雅各布斯的答案。 因此,停止守护进程并运行 memcached -vv

I'm using Mezzanine and the only answer that worked for me was Jacobs answer. So stopping the daemon and running memcached -vv

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文