动态查看对Memcache的使用

原创
2014/07/16 11:24
阅读数 2.6K

Web项目提升访问性能,使用Memcache做缓存是一种比较通用的方案,这篇文章记录了:动态查看应用如何使用Memcache。 这里使用的Django项目做例子。关于Django如何连接Memcache请查阅官方文档。下面开始:

  1. “显式”运行Memcache:

<pre> memcached -vv -u root -m 256m </pre>

如果你使用的是root用户,则一定要加上 -u root选项。 命令执行后会得到如下输出:

<pre> slab class 1: chunk size 96 perslab 10922 slab class 2: chunk size 120 perslab 8738 slab class 3: chunk size 152 perslab 6898 slab class 4: chunk size 192 perslab 5461 slab class 5: chunk size 240 perslab 4369 slab class 6: chunk size 304 perslab 3449 slab class 7: chunk size 384 perslab 2730 slab class 8: chunk size 480 perslab 2184 slab class 9: chunk size 600 perslab 1747 slab class 10: chunk size 752 perslab 1394 slab class 11: chunk size 944 perslab 1110 slab class 12: chunk size 1184 perslab 885 slab class 13: chunk size 1480 perslab 708 slab class 14: chunk size 1856 perslab 564 slab class 15: chunk size 2320 perslab 451 slab class 16: chunk size 2904 perslab 361 slab class 17: chunk size 3632 perslab 288 slab class 18: chunk size 4544 perslab 230 slab class 19: chunk size 5680 perslab 184 slab class 20: chunk size 7104 perslab 147 slab class 21: chunk size 8880 perslab 118 slab class 22: chunk size 11104 perslab 94 slab class 23: chunk size 13880 perslab 75 slab class 24: chunk size 17352 perslab 60 slab class 25: chunk size 21696 perslab 48 slab class 26: chunk size 27120 perslab 38 slab class 27: chunk size 33904 perslab 30 slab class 28: chunk size 42384 perslab 24 slab class 29: chunk size 52984 perslab 19 slab class 30: chunk size 66232 perslab 15 slab class 31: chunk size 82792 perslab 12 slab class 32: chunk size 103496 perslab 10 slab class 33: chunk size 129376 perslab 8 slab class 34: chunk size 161720 perslab 6 slab class 35: chunk size 202152 perslab 5 slab class 36: chunk size 252696 perslab 4 slab class 37: chunk size 315872 perslab 3 slab class 38: chunk size 394840 perslab 2 slab class 39: chunk size 493552 perslab 2 slab class 40: chunk size 616944 perslab 1 slab class 41: chunk size 771184 perslab 1 slab class 42: chunk size 1048576 perslab 1 <26 server listening (auto-negotiate) <27 server listening (auto-negotiate) <28 send buffer was 212992, now 268435456 <29 send buffer was 212992, now 268435456 <28 server listening (udp) <28 server listening (udp) <29 server listening (udp) <28 server listening (udp) <29 server listening (udp) <29 server listening (udp) <28 server listening (udp) <29 server listening (udp) </pre>


  1. 访问应用,这里是刷新下页面 Memcache的输出:

<pre> <30 new auto-negotiating client connection 30: Client using the ascii protocol <30 get :1:views.decorators.cache.cache_header..9d09a91f1b32ac17d49837bcbbef9427.zh-cn.Asia/Shanghai >30 END <30 set :1:views.decorators.cache.cache_header..9d09a91f1b32ac17d49837bcbbef9427.zh-cn.Asia/Shanghai 1 900 6 >30 STORED <30 set :1:views.decorators.cache.cache_page..GET.9d09a91f1b32ac17d49837bcbbef9427.d41d8cd98f00b204e9800998ecf8427e.zh-cn.Asia/Shanghai 1 900 66649 >30 STORED <30 connection closed. </pre>

可以看到应用尝试获取某条缓存,没有找到,便将该key-value存下来,还有缓存多长时间,存储大小等参数。

  1. 再次刷新页面 Memcache输出:

<pre> <30 new auto-negotiating client connection 30: Client using the ascii protocol <30 get :1:views.decorators.cache.cache_header..9d09a91f1b32ac17d49837bcbbef9427.zh-cn.Asia/Shanghai >30 sending key :1:views.decorators.cache.cache_header..9d09a91f1b32ac17d49837bcbbef9427.zh-cn.Asia/Shanghai >30 END <30 get :1:views.decorators.cache.cache_page..GET.9d09a91f1b32ac17d49837bcbbef9427.d41d8cd98f00b204e9800998ecf8427e.zh-cn.Asia/Shanghai >30 sending key :1:views.decorators.cache.cache_page..GET.9d09a91f1b32ac17d49837bcbbef9427.d41d8cd98f00b204e9800998ecf8427e.zh-cn.Asia/Shanghai >30 END <30 connection closed. </pre>

取了两次,都拿到了。

  1. 查看Memcache中缓存数据的详细信息

<pre> telnet 127.0.0.1 11211 </pre>

<pre> stats items STAT items:4:number 1 STAT items:4:age 1056 STAT items:4:evicted 0 STAT items:4:evicted_nonzero 0 STAT items:4:evicted_time 0 STAT items:4:outofmemory 0 STAT items:4:tailrepairs 0 STAT items:4:reclaimed 0 STAT items:4:expired_unfetched 0 STAT items:4:evicted_unfetched 0 STAT items:31:number 1 STAT items:31:age 1056 STAT items:31:evicted 0 STAT items:31:evicted_nonzero 0 STAT items:31:evicted_time 0 STAT items:31:outofmemory 0 STAT items:31:tailrepairs 0 STAT items:31:reclaimed 0 STAT items:31:expired_unfetched 0 STAT items:31:evicted_unfetched 0 END </pre>

查看具体item:

<pre> stats cachedump 4 0 ITEM :1:views.decorators.cache.cache_header..9d09a91f1b32ac17d49837bcbbef9427.zh-cn.Asia/Shanghai [6 b; 1405480402 s] END </pre>

这便是刚才保存的数据。 获取某条缓存

<pre> get :1:views.decorators.cache.cache_page..GET.d1537bd0b66a4289001629ac2e06e742.d41d8cd98f00b204e9800998ecf8427e.zh-cn.Asia/Shanghai </pre>

<pre> VALUE :1:views.decorators.cache.cache_page..GET.d1537bd0b66a4289001629ac2e06e742.d41d8cd98f00b204e9800998ecf8427e.zh-cn.Asia/Shanghai 1 16816 ccopy_reg _reconstructor p1 (cdjango.template.response TemplateResponse p2 c__builtin__ object p3 NtRp4 (dp5 S'cookies' p6 g1 (cdjango.http SimpleCookie p7 c__builtin__ dict p8 (dtRp9 sS'_headers' p10 (dp11 S'last-modified' p12 (S'Last-Modified' p13 S'Wed, 16 Jul 2014 03:20:44 GMT' tp14 sS'expires' p15 (S'Expires' p16 S'Wed, 16 Jul 2014 03:35:44 GMT' tp17 sS'content-type' p18 (S'Content-Type' p19 S'text/html; charset=utf-8' tp20 sS'cache-control' p21 (S'Cache-Control' p22 S'max-age=900' tp23 ssS'_is_rendered' p24 I01 sS'_base_content_is_iter' p25 I00 sS'_charset' p26 S'utf-8' p27 sS'_container' p28 (lp29 g1 (cdjango.utils.safestring SafeUnicode p30 c__builtin__ unicode p31 V\u000a<!DOCTYPE html>\u000a<html>\u000a <head>\u000a <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />\u000a <title>\u4f7f\u7528\u72b6\u51b5\u6982\u89c8 - EC-CLOUD</title>\u000a \u000a\u000a<script type="text/javascript" src="/static/dashboard/js/967e5ade6890.js"></script>\u000a\u000a \u000a \u000a\u000a<link rel="stylesheet" href="/static/dashboard/css/6599969e57f7.css" type="text/css" media="screen" />\u000a\u000a<link rel="shortcut icon" href="/static/dashboard/img/favicon.ico"/>\u000a\u000a \u000a <script type="text/javascript" charset="utf-8">\u000a /*\u000a Added so that we can append Horizon scoped JS events to\u000a the DOM load events without running in to the "horizon"\u000a name-space not currently being defined since we load the\u000a scripts at the bottom of the page.\u000a */\u000a var addHorizonLoadEvent = function(func) {\u000a var old_onload = window.onload;\u000a\u000a if (typeof window.onload != 'function') {\u000a window.onload = func;\u000a } else {\u000a window.onload = function() {\u000a old_onload();\u000a func();\u000a }\u000a }\u000a }\u000a</script>\u000a\u000a </head>\u000a <body id="">\u000a \u000a <div id="container">\u000a \u000a<div class="topbar" id="user_info">\u000a <table class="top-table">\u000a <tr>\u000a <td class="logo"></td>\u000a <td class="toolbar">\u000a <a href="#">\u000a <img src='/static/dashboard/img/pic/chost.png' />\u000a <span>\u4e3b\u673a</span>\u000a </a>\u000a <a href="#">\u000a <img src='/static/dashboard/img/pic/cstore.png' />\u000a <span>\u5b58\u50a8</span>\u000a </a>\u000a <a href="#">\u000a <img src='/static/dashboard/img/pic/cdesk.png' />\u000a <span>\u684c\u9762</span>\u000a </a>\u000a </td>\u000a <td></td>\u000a <td class="setting">\u000a <div class="welcome"> \u6b22\u8fce, admin </div>\u000a \u000a <div class="contain" id="allMessages" tabindex="0">\u000a <span class="message">\u6d88\u606f</span>\u000a <div class="message-num">0</div>\u000a <ul class="message-list">\u000a <li data-url="/admin/alarm_log/">\u000a <span class="lead-data">\u544a\u8b66\u65e5\u5fd7</span>\u000a <span class="alarm"></span>\u000a </li>\u000a <li data-url="/admin/workflow_message/">\u000a <span class="lead-data">\u6d41\u7a0b\u6d88\u606f</span>\u000a <span class="business"></span>\u000a </li>\u000a </ul>\u000a </div>\u000a \u000a <a class="contain" href="/auth/logout/">\u000a <span class="logout_close">\u9000\u51fa</span>\u000a </a>\u000a </td>\u000a </tr>\u000a </table>\u000a <span data-message="/project/notification_list" id="message_get"></span>\u000a</div>\u000a\u000a\u000a \u000a<div class="messages">\u000a\u000a</div>\u000a\u000a <div id='main_content'>\u000a \u000a \u000a\u000a<div id="sidebar_global" class='sidebar'>\u000a \u000a\u000a<div class='clearfix'>\u000a <div class="nav-tabs">\u000a \u000a \u000a <a href="/project/" tabindex='1'>\u000a <img src="/static/dashboard/img/pic/project.png"/>\u9879\u76ee</a>\u000a \u000a \u000a \u000a <a class="active" href="/admin/" tabindex='1'><img src="/static/dashboard/img/pic/manage.png" />\u7ba1\u7406\u5458</a>\u000a \u000a \u000a </div>\u000a</div>\u000a\u000a\u000a \u000a\u000a \u000a<div class="subnav_list">\u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u7cfb\u7edf\u9762\u677f</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/" class="active" id='left_nav_current_select' tabindex='1'>\u6982\u89c8</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/info/" tabindex='1'>\u7cfb\u7edf\u4fe1\u606f</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u7528\u6237\u7ba1\u7406</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/projects/" tabindex='1'>\u9879\u76ee</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/users/" tabindex='1'>\u7528\u6237</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u4e91\u4e3b\u673a</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/instances/" tabindex='1'>\u4e91\u4e3b\u673a</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/flavors/" tabindex='1'>\u89c4\u683c</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/images/images/" tabindex='1'>\u955c\u50cf</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u4e91\u5b58\u50a8</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/volumes/" tabindex='1'>\u5757\u5b58\u50a8</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u7f51\u7edc</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/internal_networks/" tabindex='1'>\u5185\u7f51\u7ba1\u7406</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/public_networks/" tabindex='1'>\u516c\u7f51\u7ba1\u7406</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u8d44\u6e90\u6c60</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/cluster/" tabindex='1'>\u96c6\u7fa4\u7ba1\u7406</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/compute_node/" tabindex='1'>\u8ba1\u7b97\u7ed3\u70b9\u7ba1\u7406</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/storage/" tabindex='1'>\u5b58\u50a8\u7ed3\u70b9\u7ba1\u7406</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u7cfb\u7edf\u76d1\u63a7</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/instance_monitor/" tabindex='1'>\u865a\u62df\u673a\u76d1\u63a7</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/alarm_log/" tabindex='1'>\u544a\u8b66\u65e5\u5fd7</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/alarms/" tabindex='1'>\u544a\u8b66\u89c4\u5219</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u7b56\u7565</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/rule/" tabindex='1'>\u7b56\u7565</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a <div>\u000a <h4 class="slideUp nav_item">\u5de5\u4f5c\u6d41</h4>\u000a <ul class="main_nav">\u000a \u000a <li>\u000a <a href="/admin/workflow/" tabindex='1'>\u7533\u8bf7\u5904\u7406</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/workflow_setting/" tabindex='1'>\u7533\u8bf7\u8bbe\u7f6e</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/feedback/" tabindex='1'>\u610f\u89c1\u53cd\u9988</a>\u000a </li>\u000a \u000a <li>\u000a <a href="/admin/workflow_message/" tabindex='1'>\u6d41\u7a0b\u6d88\u606f</a>\u000a </li>\u000a \u000a </ul>\u000a </div>\u000a \u000a \u000a\u000a \u000a \u000a \u000a\u000a</div>\u000a<script type="text/javascript">\u000a // Add this script to fit slideDown/slideUp effect.\u000a ele = document.getElementById("left_nav_current_select");\u000a var preparent = ele.parentNode.parentNode;\u000a preparent.style.display = "block";\u000a h4_slibling = preparent.parentNode.children[0];\u000a var tmpClassName = h4_slibling.className;\u000a h4_slibling.className = tmpClassName.split(new RegExp(" slideUp|slideUp |^slideUp$","ig")).join("");\u000a h4_slibling.className += " slideDown";\u000a</script>\u000a\u000a</div>\u000a\u000a \u000a <div id="container_main" class="main_center">\u000a <div>\u000a <div id="navigation">\u000a \u000a \u000a\u000a\u000a \u000a <a href="/admin/" class="current nav-admin">\u7ba1\u7406\u5458</a>\u000a \u000a\u000a\u000a \u000a \u000a \u000a </div>\u000a \u000a <div class='statistics-show-main'>\u000a <div class='statistic-show-title'>\u000a <span class='admin-pool-status'></span>\u7cfb\u7edf\u8d44\u6e90\u6982\u51b5\u000a </div>\u000a \u000a<div id="statistic_main">\u000a <div class="statistic-summary">\u000a <div class="common-statistic-summary" data="{used:59,all:72,name:'vCPU\u4f7f\u7528\u91cf',unit:'vCPUs'}"></div>\u000a </div>\u000a\u000a <div class="statistic-summary">\u000a <div class="common-statistic-summary" data="{used:103216,all:72309,name:'\u5185\u5b58\u4f7f\u7528\u91cf',unit:'MB'}"></div>\u000a </div>\u000a\u000a <div class="statistic-summary">\u000a \u000a <div class="common-statistic-summary" data="{used:10940,all:2257,name:'\u786c\u76d8\u4f7f\u7528\u91cf',unit:'GB'}"></div>\u000a \u000a </div>\u000a <div class="statistic-summary">\u000a <div class="common-statistic-summary"\u000a data="{used:8,all:62,name:'\u516c\u7f51IP',unit:''}"></div>\u000a </div>\u000a\u000a</div>\u000a\u000a </div>\u000a \u000a <div class="topology-show-main">\u000a <div class="topology-show-title">\u000a <span></span>\u62d3\u6251\u7ed3\u6784 - \u4e3b\u673a\u89c6\u56fe\u000a </div>\u000a <div class="topology-main-container">\u000a <div id="topology_main"></div>\u000a </div>\u000a </div>\u000a <span data-topology="/admin/topology" id="topology"></span>\u000a <div class='message-show-main'>\u000a <div class='message-show-title'>\u000a <span></span>\u544a\u8b66\u000a </div>\u000a <div>\u000a\u000a<div class="table_wrapper">\u000a \u000a \u000a\u000a<table id="notification_overview" class="table table-bordered table-striped datatable">\u000a <thead>\u000a \u000a <tr class='table_caption'>\u000a <th class='table_header' colspan='3'>\u000a <div class='table_title .h3'> </div>\u000a \u000a<div class="table_actions clearfix">\u000a\u000a\u000a</div>\u000a\u000a </th>\u000a </tr>\u000a \u000a \u000a \u000a \u000a \u000a </thead>\u000a \u000a <tbody>\u000a \u000a <tr class="odd empty">\u000a <td colspan="3">\u6ca1\u6709\u663e\u793a\u9879\u3002</td>\u000a </tr>\u000a \u000a </tbody>\u000a \u000a \u000a \u000a <tfoot>\u000a \u000a <tr>\u000a <td colspan="3">\u000a <span class="table_count">\u663e\u793a 0 \u4e2a\u6761\u76ee</span>\u000a \u000a </td>\u000a </tr>\u000a </tfoot>\u000a \u000a \u000a </table>\u000a\u000a \u000a \u000a</div>\u000a\u000a</div>\u000a </div>\u000a\u000a <div class="detail-wapper active">\u000a <div id="detail-container">\u000a <div id="tabs-header">\u000a \u000a \u000a </div>\u000a \u000a \u000a </div>\u000a <span class="close-btn"></span>\u000a </div>\u000a <div class="detail-wapper deleted">\u000a <div id="detail-container-deleted">\u000a <div id="tabs-header-deleted">\u000a \u000a \u000a </div>\u000a \u000a \u000a </div>\u000a <span class="close-btn"></span>\u000a </div>\u000a <div class="detail-loading"></div>\u000a </div>\u000a </div>\u000a <div style="clear: both;"></div>\u000a </div>\u000a </div>\u000a <script type="text/javascript">\u000a window.onresize = setSize;\u000a setSize();\u000a function setSize(){\u000a var main_content = document.getElementById("main_content");\u000a main_content.style.height = (document.documentElement.clientHeight - 60) + "px";\u000a var main = document.getElementById("container_main");\u000a main.style.height = (document.documentElement.clientHeight - 70) + "px"\u000a }\u000a </script>\u000a \u000a <div id="footer">\u000a \u000a </div>\u000a \u000a \u000a\u000a\u000a<script type="text/javascript" src="/i18n/js/horizon/"></script>\u000a\u000a\u000a<script type="text/javascript" src="/static/dashboard/js/cb507686a44b.js"></script>\u000a\u000a\u000a<script type="text/html" id="modal_template">\u000a\u000a<div class="modal hide">\u000a <div class='modal-header'>\u000a <span class='close' data-dismiss='modal'></span>\u000a <h3>{{title}}</h3>\u000a </div>\u000a <div class='modal-body'>\u000a {{body}}\u000a </div>\u000a <div class='modal-footer'>\u000a <a href='#' class='btn btn-primary close'>{{confirm}}</a>\u000a <a href='#' class='btn cancel close' data-dismiss='modal'>{{cancel}}</a>\u000a </div>\u000a</div>\u000a\u000a</script>\u000a\u000a<script type="text/html" id="empty_row_template">\u000a\u000a<tr class="odd empty"><td colspan="{{colspan}}">\u6ca1\u6709\u663e\u793a\u9879\u3002</td></tr>\u000a\u000a</script>\u000a\u000a<script type="text/html" id="alert_message_template">\u000a\u000a<div class="alert alert-block fade in alert-{{type}}">\u000a <a class="close" data-dismiss="alert" href="#">&times;</a>\u000a <p>\u000a {{type_capitalized}}<br>\u000a {{#safe}}\u000a {{{message}}}\u000a {{/safe}}\u000a {{^safe}}\u000a {{message}}\u000a {{/safe}}\u000a </p>\u000a</div>\u000a\u000a</script>\u000a\u000a<script type="text/html" id="spinner-modal">\u000a\u000a<div class="modal loading hide">\u000a <p>{{text}}&hellip;</p>\u000a</div>\u000a\u000a</script>\u000a\u000a<script type="text/html" id="project_user_template">\u000a\u000a<ul class="nav nav-pills btn-group">\u000a <li class="member" data-user-id="{{user_id}}">\u000a <span class="user_name">{{user_name}}</span>\u000a </li>\u000a <li class="active"><a class="btn btn-primary" href="#add_remove">{{text}}</a></li>\u000a <li class="dropdown role_options">\u000a <a class="dropdown-toggle" data-toggle="dropdown" href="#">\u000a <span class="roles_display">Roles</span>\u000a <b class="caret"></b>\u000a </a>\u000a <ul class="dropdown-menu role_dropdown clearfix">\u000a {{#roles}}\u000a <li data-role-id="{{role_id}}"><i class="icon-ok"></i> {{role_name}}</li>\u000a {{/roles}}\u000a </ul>\u000a </li>\u000a</ul>\u000a\u000a</script>\u000a\u000a\u000a\u000a\u000a<script type='text/javascript' charset='utf-8'>\u000a // Call init on DOM ready.\u000a $.ajaxSetup({cache:false});\u000a $(document).ready(horizon.init);\u000a</script>\u000a\u000a \u000a <div id="modal_wrapper" />\u000a </body>\u000a</html>\u000a

tRp32 asb. </pre> 可以看到被存储的页面。

展开阅读全文
加载中

作者的其它热门文章

打赏
0
6 收藏
分享
打赏
0 评论
6 收藏
0
分享
返回顶部
顶部