<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Memcache</id>
	<title>Memcache - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Memcache"/>
	<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Memcache&amp;action=history"/>
	<updated>2026-05-09T17:58:16Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://aznot.com/index.php?title=Memcache&amp;diff=534&amp;oldid=prev</id>
		<title>Kenneth: Created page with &quot;==Summary== [http://www.danga.com/memcached/ memcached]  First, you start up the memcached daemon on as many spare machines as you have. The daemon has no configuration file, ...&quot;</title>
		<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Memcache&amp;diff=534&amp;oldid=prev"/>
		<updated>2014-07-29T05:38:12Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Summary== [http://www.danga.com/memcached/ memcached]  First, you start up the memcached daemon on as many spare machines as you have. The daemon has no configuration file, ...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Summary==&lt;br /&gt;
[http://www.danga.com/memcached/ memcached]&lt;br /&gt;
&lt;br /&gt;
First, you start up the memcached daemon on as many spare machines as you have. The daemon has no configuration file, just a few command line options, only 3 or 4 of which you&amp;#039;ll likely use:&lt;br /&gt;
&lt;br /&gt;
 ./memcached -d -m 2048 -l 10.0.0.40 -p 11211&lt;br /&gt;
&lt;br /&gt;
This starts memcached up as a daemon, using 2GB of memory, and listening on IP 10.0.0.40, port 11211. Because a 32-bit process can only address 4GB of virtual memory (usually significantly less, depending on your operating system), if you have a 32-bit server with 4-64GB of memory using PAE you can just run multiple processes on the machine, each using 2 or 3GB of memory.&lt;br /&gt;
&lt;br /&gt;
*[http://www.socialtext.net/memcached/index.cgi?faq memcached FAQ]&lt;br /&gt;
*[http://dealnews.com/developers/memcached.html Fastest languages for memcached]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/ACID ACID]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Create%2C_read%2C_update_and_delete Create, read, update and delete]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt Memcache Protocol]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What are some limits in memcached I might hit?: [http://www.socialtext.net/memcached/index.cgi?faq#what_are_some_limits_in_memcached_i_might_hit]&lt;br /&gt;
 The simple limits you will probably see with memcache are the key and item size limits. Keys are&lt;br /&gt;
 restricted to 250 characters. Stored data cannot exceed 1 megabyte in size, since that is the&lt;br /&gt;
 largest typical slab size.&lt;br /&gt;
&lt;br /&gt;
==Installation==&lt;br /&gt;
&lt;br /&gt;
=== Yum Method ===&lt;br /&gt;
&lt;br /&gt;
RPMForge Method: (version 1.4.5-1.el5.rf as of 2010.10.25)&lt;br /&gt;
* See [[RPMforge]] for RPMforge installation&lt;br /&gt;
* Yum method info provided here: http://serverfault.com/questions/342826/problems-installing-memcached&lt;br /&gt;
&lt;br /&gt;
 yum install memcached --enablerepo=rpmforge --enablerepo=rpmforge-extras&lt;br /&gt;
 &lt;br /&gt;
 # x64 only - to fix x64 issue with SSLeay:&lt;br /&gt;
 # wget http://pkgs.repoforge.org/perl-Net-SSLeay/perl-Net-SSLeay-1.36-1.el5.rfx.x86_64.rpm&lt;br /&gt;
 # rpm -Uvh perl-Net-SSLeay-1.36-1.el5.rfx.x86_64.rpm&lt;br /&gt;
&lt;br /&gt;
 # to fix: Starting memcached: chown: cannot access `/var/run/memcached&amp;#039;: No such file or directory&lt;br /&gt;
 # touch /var/run/memcached&lt;br /&gt;
&lt;br /&gt;
/etc/sysconfig/memcached (original):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PORT=&amp;quot;11211&amp;quot;&lt;br /&gt;
USER=&amp;quot;nobody&amp;quot;&lt;br /&gt;
MAXCONN=&amp;quot;1024&amp;quot;&lt;br /&gt;
CACHESIZE=&amp;quot;64&amp;quot;&lt;br /&gt;
OPTIONS=&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/etc/sysconfig/memcached (modified, with cachesize in megabytes):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PORT=&amp;quot;11211&amp;quot;&lt;br /&gt;
USER=&amp;quot;nobody&amp;quot;&lt;br /&gt;
MAXCONN=&amp;quot;1024&amp;quot;&lt;br /&gt;
# in megabytes:&lt;br /&gt;
CACHESIZE=&amp;quot;128&amp;quot;&lt;br /&gt;
# additional options, specify address to listen on&lt;br /&gt;
OPTIONS=&amp;quot;-l 127.0.0.1&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start service:&lt;br /&gt;
 chkconfig memcached on&lt;br /&gt;
 service memcached start&lt;br /&gt;
&lt;br /&gt;
# ?eh? yum install php-eaccelerator&lt;br /&gt;
&lt;br /&gt;
=== Manual Method ===&lt;br /&gt;
&lt;br /&gt;
[http://blog.ajohnstone.com/archives/installing-memcached/ Installing memcached]&lt;br /&gt;
&lt;br /&gt;
Download [http://www.monkey.org/~provos/libevent/ Libevent]:&lt;br /&gt;
 mkdir -p ~/.src ; cd ~/.src&lt;br /&gt;
 wget http://www.monkey.org/~provos/libevent-1.3e.tar.gz&lt;br /&gt;
 &lt;br /&gt;
 tar -zvxf libevent-1.3e.tar.gz&lt;br /&gt;
 cd libevent-1.3e&lt;br /&gt;
 ./configure; make; make install;&lt;br /&gt;
&lt;br /&gt;
 #32bit:&lt;br /&gt;
 ln -s /usr/local/lib/libevent-1.3e.so.1 /lib/libevent-1.3e.so.1&lt;br /&gt;
 #64bit:&lt;br /&gt;
 ln -s /usr/local/lib/libevent-1.3e.so.1 /lib64/libevent-1.3e.so.1&lt;br /&gt;
&lt;br /&gt;
Download [http://www.danga.com/memcached/ memcached]:&lt;br /&gt;
 mkdir -p ~/.src ; cd ~/.src&lt;br /&gt;
 wget http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz&lt;br /&gt;
 &lt;br /&gt;
 tar -zxvf memcached-1.2.2.tar.gz&lt;br /&gt;
 cd memcached-1.2.2&lt;br /&gt;
 ./configure; make; make install;&lt;br /&gt;
&lt;br /&gt;
Start memcached:&lt;br /&gt;
 memcached -d -u nobody -m 512 127.0.0.1 -p 11211&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
Often libevent.so cannot be found when executing memcache. A useful command LD_DEBUG, is very helpful to determine where libraries are being loaded from.&lt;br /&gt;
 # help&lt;br /&gt;
 LD_DEBUG=help memcached -v&lt;br /&gt;
  &lt;br /&gt;
 LD_DEBUG=libs memcached -v 2&amp;gt;&amp;amp;1 &amp;gt; /dev/null | less&lt;br /&gt;
 18990: find library=libevent-1.3b.so.1 [0]; searching&lt;br /&gt;
 ...&lt;br /&gt;
 18990: trying file=/usr/lib/libevent-1.3b.so.1&lt;br /&gt;
 18990:&lt;br /&gt;
 memcached: error while loading shared libraries: libevent-1.3b.so.1: cannot open shared object file: No such file or directory&lt;br /&gt;
&lt;br /&gt;
Simply place the library where memcached will find it and execute memcached:&lt;br /&gt;
 ln -s /usr/local/lib/libevent-1.3e.so.1 /lib/libevent-1.3e.so.1&lt;br /&gt;
 #64bit:&lt;br /&gt;
 ln -s /usr/local/lib/libevent-1.3e.so.1 /lib64/libevent-1.3e.so.1&lt;br /&gt;
&lt;br /&gt;
==Testing memcache==&lt;br /&gt;
[http://www.lullabot.com/articles/how_install_memcache_debian_etch How to install memcache on Debian Etch]&lt;br /&gt;
&lt;br /&gt;
Quick stats check:&lt;br /&gt;
 echo -e &amp;quot;stats\nquit&amp;quot; | nc 127.0.0.1 11211&lt;br /&gt;
&lt;br /&gt;
Testing the memcached daemon with telnet:&lt;br /&gt;
 telnet 1.2.3.4 11211&lt;br /&gt;
&lt;br /&gt;
The commands look like this:&lt;br /&gt;
 set &amp;lt;key&amp;gt; &amp;lt;flag&amp;gt; &amp;lt;exptime&amp;gt; &amp;lt;bytes&amp;gt;\r\n&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
 set test2 1 0 2&lt;br /&gt;
 ab&lt;br /&gt;
 STORED&lt;br /&gt;
&lt;br /&gt;
 get test2&lt;br /&gt;
 VALUE test2 1 2&lt;br /&gt;
 ab&lt;br /&gt;
 END&lt;br /&gt;
&lt;br /&gt;
 set test4 1 0 2&lt;br /&gt;
 abcde&lt;br /&gt;
 CLIENT_ERROR bad data chunk&lt;br /&gt;
 ERROR&lt;br /&gt;
&lt;br /&gt;
To see statistics:&lt;br /&gt;
 stats&lt;br /&gt;
&lt;br /&gt;
To exit:&lt;br /&gt;
 quit&lt;br /&gt;
&lt;br /&gt;
The flag is an arbitrary number that you can use in your client logic. It is intended to be metadata that you can assign to each cached object.&lt;br /&gt;
&lt;br /&gt;
I set the exptime to zero (never expire)&lt;br /&gt;
&lt;br /&gt;
the bytes to the number of characters I want to store.  If the bytes and the number of characters don&amp;#039;t match, an error occurs.&lt;br /&gt;
&lt;br /&gt;
For other commands see [http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt protocol.txt]&lt;br /&gt;
&lt;br /&gt;
An example of how to get the [http://lists.danga.com/pipermail/memcached/2005-September/001657.html stats using a bash script]&lt;br /&gt;
&lt;br /&gt;
== Stats Check ==&lt;br /&gt;
&lt;br /&gt;
Dump all stats:&lt;br /&gt;
 echo -e &amp;quot;stats\nquit&amp;quot; | nc 127.0.0.1 11211&lt;br /&gt;
&lt;br /&gt;
Get important items:&lt;br /&gt;
 echo -e &amp;quot;stats\nquit&amp;quot; | nc 127.0.0.1 11211 | grep -e &amp;quot;\(uptime\|get_hits\|get_misses\|bytes \|limit_maxbytes\)&amp;quot;&lt;br /&gt;
 # uptime - Number of seconds this server has been running&lt;br /&gt;
 # get_hits - request that are in cache&lt;br /&gt;
 # get_misses - requests that weren&amp;#039;t in cache&lt;br /&gt;
 # limit_maxbytes - total max bytes can store&lt;br /&gt;
 # bytes - current bytes in cache&lt;br /&gt;
&lt;br /&gt;
=== Stats Protocol ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Name              Type     Meaning&lt;br /&gt;
----------------------------------&lt;br /&gt;
pid               32u      Process id of this server process&lt;br /&gt;
uptime            32u      Number of seconds this server has been running&lt;br /&gt;
time              32u      current UNIX time according to the server&lt;br /&gt;
version           string   Version string of this server&lt;br /&gt;
pointer_size      32       Default size of pointers on the host OS&lt;br /&gt;
                           (generally 32 or 64)&lt;br /&gt;
rusage_user       32u:32u  Accumulated user time for this process &lt;br /&gt;
                           (seconds:microseconds)&lt;br /&gt;
rusage_system     32u:32u  Accumulated system time for this process &lt;br /&gt;
                           (seconds:microseconds)&lt;br /&gt;
curr_items        32u      Current number of items stored by the server&lt;br /&gt;
total_items       32u      Total number of items stored by this server &lt;br /&gt;
                           ever since it started&lt;br /&gt;
bytes             64u      Current number of bytes used by this server &lt;br /&gt;
                           to store items&lt;br /&gt;
curr_connections  32u      Number of open connections&lt;br /&gt;
total_connections 32u      Total number of connections opened since &lt;br /&gt;
                           the server started running&lt;br /&gt;
connection_structures 32u  Number of connection structures allocated &lt;br /&gt;
                           by the server&lt;br /&gt;
cmd_get           64u      Cumulative number of retrieval requests&lt;br /&gt;
cmd_set           64u      Cumulative number of storage requests&lt;br /&gt;
get_hits          64u      Number of keys that have been requested and &lt;br /&gt;
                           found present&lt;br /&gt;
get_misses        64u      Number of items that have been requested &lt;br /&gt;
                           and not found&lt;br /&gt;
evictions         64u      Number of valid items removed from cache&lt;br /&gt;
                           to free memory for new items&lt;br /&gt;
bytes_read        64u      Total number of bytes read by this server &lt;br /&gt;
                           from network&lt;br /&gt;
bytes_written     64u      Total number of bytes sent by this server to &lt;br /&gt;
                           network&lt;br /&gt;
limit_maxbytes    32u      Number of bytes this server is allowed to&lt;br /&gt;
                           use for storage. &lt;br /&gt;
threads           32u      Number of worker threads requested.&lt;br /&gt;
                           (see doc/threads.txt)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Source: Protocol - http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt&lt;br /&gt;
&lt;br /&gt;
==Slab Size==&lt;br /&gt;
&lt;br /&gt;
[http://www.socialtext.net/memcached/index.cgi?faq#what_are_some_limits_in_memcached_i_might_hit What are some limits in memcached I might hit?]:&lt;br /&gt;
:&amp;quot;The simple limits you will probably see with memcache are the key and item size limits. Keys are restricted to 250 characters. Stored data cannot exceed 1 megabyte in size, since that is the largest typical slab size.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[http://www.socialtext.net/memcached/index.cgi?faq#how_does_memcached_s_memory_allocation_work_why_not_use_malloc_free_why_the_hell_does_it_use_slabs  How does memcached&amp;#039;s memory allocation work? Why not use malloc/free!? Why the hell does it use slabs!?]:&lt;br /&gt;
:&amp;quot;Actually, it&amp;#039;s a compile time option. The default is to use the internal slab allocator. You really really want to use the built-in slab allocator. At first memcached did just use malloc/free for everything. However this does not play very well with OS memory managers. You get fragmentation, and your OS ends up spending more time trying to find contiguous blocks of memory to feed malloc() than it does running the memcached process. If you disagree, of course you&amp;#039;re free to try malloc! just don&amp;#039;t complain on the lists ;)&lt;br /&gt;
: &amp;amp;nbsp;&lt;br /&gt;
:The slab allocator was built to work around this. Memory is allocated in chunks internally and constantly reused. Since memory is broken into different size slabs, you do waste memory if your items do not fit perfectly into the slab the server chooses to put it in. This has enjoyed considerable efficiency improvements by Steven Grimm.&lt;br /&gt;
: &amp;amp;nbsp;&lt;br /&gt;
:Some older posts about the slab changes (power of n vs power of 2), and some tradeoffs are on the mailing list:&lt;br /&gt;
:http://lists.danga.com/pipermail/memcached/2006-May/002163.html&lt;br /&gt;
:http://lists.danga.com/pipermail/memcached/2007-March/003753.html&lt;br /&gt;
: &amp;amp;nbsp;&lt;br /&gt;
:And if you&amp;#039;d like to attempt to use malloc/free and see how it works, you may define &amp;#039;USE_SYSTEM_MALLOC&amp;#039; in build process. It might not be tested very well, so getting developer support for it is unlikely.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
*[http://lists.danga.com/pipermail/memcached/2006-January/001879.html single object size limit]&lt;br /&gt;
:&amp;quot;The maximum object size is a few bytes under 1MB.  You&amp;#039;re trying to set about 250k more than it can take.&amp;quot;&lt;br /&gt;
:&amp;quot;You can recompile memcached with a higher POWER_LARGEST if you need more than 1MB.  It&amp;#039;s in slabs.c at the top.  Make it 21 to get 2MB max, or 22 for 4MB max, etc.&amp;quot;&lt;br /&gt;
*[http://lists.danga.com/pipermail/memcached/2006-October/002986.html SERVER_ERROR object too large for cache]&lt;br /&gt;
:&amp;quot;The maximum size is 1 megabyte. It can be increased; I believe there was a patch (against the old 1.1.12 version) to do it. The trick is that memcached&amp;#039;s memory manager allocates memory in 1MB chunks, so increasing the maximum is not just a matter of changing a &amp;quot;maximum object size&amp;quot; setting somewhere. But it is certainly doable.&amp;quot;&lt;br /&gt;
*[http://brad.livejournal.com/1923210.html new memcached memory layout]&lt;br /&gt;
:&amp;quot;I propose all objects are stored as a linked list of 64 byte chunks.&amp;quot;&lt;br /&gt;
*[http://lists.danga.com/pipermail/memcached/2007-March/003753.html Memory management]&lt;br /&gt;
:&amp;quot;We&amp;#039;ve talked internally about eventually moving away from the slab-class-based system. It should be possible to run a job in the background (especially in the multithreaded version, but even in the single-threaded one) to shuffle objects around in memory to pack them optimally. If you separate the fixed-size headers from the item data, you can even do away with byte-alignment-based waste. That would give you 100% memory efficiency for any arbitrary mix of item sizes, and true LRU behavior across the whole cache, at the cost of greater CPU consumption.&amp;quot;&lt;br /&gt;
*[http://lists.danga.com/pipermail/memcached/2006-March/002058.html Patch - Configurable slab size and large object support]&lt;br /&gt;
*[http://lists.danga.com/pipermail/memcached/2006-November/003045.html memcached overrides memory limit]&lt;br /&gt;
*[http://lists.danga.com/pipermail/memcached/2006-May/002163.html Patch - Increased memory efficiency]&lt;br /&gt;
&lt;br /&gt;
==Startup Scripts==&lt;br /&gt;
[http://www.vbseo.com/blogs/danny-bembibre/daemon-scripts-memcached-44/ Daemon scripts for memcached]:&lt;br /&gt;
&lt;br /&gt;
 touch /etc/memcached.conf&lt;br /&gt;
 cat &amp;gt; /etc/memcached.conf&lt;br /&gt;
&lt;br /&gt;
 #Memory a usar&lt;br /&gt;
 -m 16&lt;br /&gt;
 # default port&lt;br /&gt;
 -p 11211&lt;br /&gt;
 # user to run daemon nobody/apache/www-data&lt;br /&gt;
 -u nobody&lt;br /&gt;
 # only listen locally&lt;br /&gt;
 -l 127.0.0.1&lt;br /&gt;
&lt;br /&gt;
 touch /etc/init.d/memcached&lt;br /&gt;
 chmod +x /etc/init.d/memcached&lt;br /&gt;
 cat &amp;gt; /etc/init.d/memcached&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#&lt;br /&gt;
# memcached    This shell script takes care of starting and stopping&lt;br /&gt;
#              standalone memcached.&lt;br /&gt;
#&lt;br /&gt;
# chkconfig: - 80 12&lt;br /&gt;
# description: memcached is a high-performance, distributed memory&lt;br /&gt;
#              object caching system, generic in nature, but&lt;br /&gt;
#              intended for use in speeding up dynamic web&lt;br /&gt;
#              applications by alleviating database load.&lt;br /&gt;
# processname: memcached&lt;br /&gt;
# config: /etc/memcached.conf&lt;br /&gt;
# Source function library.&lt;br /&gt;
. /etc/rc.d/init.d/functions&lt;br /&gt;
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin&lt;br /&gt;
DAEMON=/usr/local/bin/memcached&lt;br /&gt;
DAEMONBOOTSTRAP=/usr/local/bin/start-memcached&lt;br /&gt;
DAEMONCONF=/etc/memcached.conf&lt;br /&gt;
NAME=memcached&lt;br /&gt;
DESC=memcached&lt;br /&gt;
PIDFILE=/var/run/$NAME.pid&lt;br /&gt;
[ -x $DAEMON ] || exit 0&lt;br /&gt;
[ -x $DAEMONBOOTSTRAP ] || exit 0&lt;br /&gt;
RETVAL=0&lt;br /&gt;
start() {&lt;br /&gt;
 echo -n $&amp;quot;Starting $DESC: &amp;quot;&lt;br /&gt;
 daemon $DAEMONBOOTSTRAP $DAEMONCONF&lt;br /&gt;
 RETVAL=$?&lt;br /&gt;
 [ $RETVAL -eq 0 ] &amp;amp;&amp;amp; touch $PIDFILE&lt;br /&gt;
 echo&lt;br /&gt;
 return $RETVAL&lt;br /&gt;
}&lt;br /&gt;
stop() {&lt;br /&gt;
 echo -n $&amp;quot;Shutting down $DESC: &amp;quot;&lt;br /&gt;
 killproc $NAME&lt;br /&gt;
 RETVAL=$?&lt;br /&gt;
 echo&lt;br /&gt;
 [ $RETVAL -eq 0 ] &amp;amp;&amp;amp; rm -f $PIDFILE&lt;br /&gt;
 return $RETVAL&lt;br /&gt;
}&lt;br /&gt;
# See how we were called.&lt;br /&gt;
case &amp;quot;$1&amp;quot; in&lt;br /&gt;
 start)&lt;br /&gt;
  start&lt;br /&gt;
  ;;&lt;br /&gt;
 stop)&lt;br /&gt;
  stop&lt;br /&gt;
  ;;&lt;br /&gt;
 restart|reload)&lt;br /&gt;
  stop&lt;br /&gt;
  start&lt;br /&gt;
  RETVAL=$?&lt;br /&gt;
  ;;&lt;br /&gt;
 status)&lt;br /&gt;
  status $prog&lt;br /&gt;
  RETVAL=$?&lt;br /&gt;
  ;;&lt;br /&gt;
 *)&lt;br /&gt;
  echo $&amp;quot;Usage: $0 {start|stop|restart|status}&amp;quot;&lt;br /&gt;
  exit 1&lt;br /&gt;
esac&lt;br /&gt;
exit $RETVAL&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 touch /usr/local/bin/start-memcached&lt;br /&gt;
 chmod +x  /usr/local/bin/start-memcached&lt;br /&gt;
 cat &amp;gt; /usr/local/bin/start-memcached&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
# start-memcached&lt;br /&gt;
# 2003/2004 - Jay Bonci &amp;lt;jaybonci@debian.org&amp;gt;&lt;br /&gt;
# This script handles the parsing of the /etc/memcached.conf file&lt;br /&gt;
# and was originally created for the Debian distribution.&lt;br /&gt;
# Anyone may use this little script under the same terms as&lt;br /&gt;
# memcached itself.&lt;br /&gt;
use strict;&lt;br /&gt;
if ($&amp;gt; != 0 and $&amp;lt; != 0) {&lt;br /&gt;
 print STDERR &amp;quot;Only root wants to run start-memcached.\n&amp;quot;;&lt;br /&gt;
 exit;&lt;br /&gt;
}&lt;br /&gt;
my $etcfile = shift || &amp;quot;/etc/memcached.conf&amp;quot;;&lt;br /&gt;
my $params = [];&lt;br /&gt;
my $etchandle; &lt;br /&gt;
# This script assumes that memcached is located at /usr/bin/memcached, and&lt;br /&gt;
# that the pidfile is writable at /var/run/memcached.pid&lt;br /&gt;
my $memcached = &amp;quot;/usr/local/bin/memcached&amp;quot;;&lt;br /&gt;
my $pidfile = &amp;quot;/var/run/memcached.pid&amp;quot;;&lt;br /&gt;
# If we don&amp;#039;t get a valid logfile parameter in the /etc/memcached.conf file,&lt;br /&gt;
# we&amp;#039;ll just throw away all of our in-daemon output. We need to re-tie it so&lt;br /&gt;
# that non-bash shells will not hang on logout. Thanks to Michael Renner for &lt;br /&gt;
# the tip&lt;br /&gt;
my $fd_reopened = &amp;quot;/dev/null&amp;quot;;&lt;br /&gt;
sub handle_logfile {&lt;br /&gt;
 my ($logfile) = @_;&lt;br /&gt;
 $fd_reopened = $logfile;&lt;br /&gt;
}&lt;br /&gt;
sub reopen_logfile {&lt;br /&gt;
 my ($logfile) = @_;&lt;br /&gt;
 open *STDERR, &amp;quot;&amp;gt;&amp;gt;$logfile&amp;quot;;&lt;br /&gt;
 open *STDOUT, &amp;quot;&amp;gt;&amp;gt;$logfile&amp;quot;;&lt;br /&gt;
 open *STDIN, &amp;quot;&amp;gt;&amp;gt;/dev/null&amp;quot;;&lt;br /&gt;
 $fd_reopened = $logfile;&lt;br /&gt;
}&lt;br /&gt;
# This is set up in place here to support other non -[a-z] directives&lt;br /&gt;
my $conf_directives = {&lt;br /&gt;
 &amp;quot;logfile&amp;quot; =&amp;gt; \&amp;amp;handle_logfile&lt;br /&gt;
};&lt;br /&gt;
if (open $etchandle, $etcfile) {&lt;br /&gt;
 foreach my $line (&amp;lt;$etchandle&amp;gt;) {&lt;br /&gt;
  $line =~ s/\#.*//go;&lt;br /&gt;
  $line = join &amp;#039; &amp;#039;, split &amp;#039; &amp;#039;, $line;&lt;br /&gt;
  next unless $line;&lt;br /&gt;
  next if $line =~ /^\-[dh]/o;&lt;br /&gt;
  if ($line =~ /^[^\-]/o) {&lt;br /&gt;
   my ($directive, $arg) = $line =~ /^(.*?)\s+(.*)/; &lt;br /&gt;
   $conf_directives-&amp;gt;{$directive}-&amp;gt;($arg);&lt;br /&gt;
   next;&lt;br /&gt;
  }&lt;br /&gt;
  push @$params, $line;&lt;br /&gt;
 }&lt;br /&gt;
}&lt;br /&gt;
unshift @$params, &amp;quot;-u root&amp;quot; unless (grep $_ eq &amp;#039;-u&amp;#039;, @$params);&lt;br /&gt;
$params = join &amp;quot; &amp;quot;, @$params;&lt;br /&gt;
if (-e $pidfile) {&lt;br /&gt;
 open PIDHANDLE, &amp;quot;$pidfile&amp;quot;;&lt;br /&gt;
 my $localpid = &amp;lt;PIDHANDLE&amp;gt;;&lt;br /&gt;
 close PIDHANDLE;&lt;br /&gt;
 chomp $localpid;&lt;br /&gt;
 if (-d &amp;quot;/proc/$localpid&amp;quot;) {&lt;br /&gt;
  print STDERR &amp;quot;memcached is already running.\n&amp;quot;; &lt;br /&gt;
  exit;&lt;br /&gt;
 } else {&lt;br /&gt;
  `rm -f $localpid`;&lt;br /&gt;
 }&lt;br /&gt;
}&lt;br /&gt;
my $pid = fork();&lt;br /&gt;
if ($pid == 0) {&lt;br /&gt;
 reopen_logfile($fd_reopened);&lt;br /&gt;
 exec &amp;quot;$memcached $params&amp;quot;;&lt;br /&gt;
 exit(0);&lt;br /&gt;
} elsif (open PIDHANDLE,&amp;quot;&amp;gt;$pidfile&amp;quot;) {&lt;br /&gt;
 print PIDHANDLE $pid;&lt;br /&gt;
 close PIDHANDLE;&lt;br /&gt;
} else {&lt;br /&gt;
 print STDERR &amp;quot;Can&amp;#039;t write pidfile to $pidfile.\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 service memcached restart&lt;br /&gt;
 chkconfig memcached on&lt;br /&gt;
&lt;br /&gt;
An alternative startup script:&lt;br /&gt;
*[http://www.dev411.com/wiki/Memcached_startup_files_for_Red_Hat_(RHEL) Memcached startup files for Red Hat (RHEL)]&lt;br /&gt;
&lt;br /&gt;
==Bash Stat Command==&lt;br /&gt;
[http://lists.danga.com/pipermail/memcached/2005-September/001657.html Bash Stat Command]: &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#&lt;br /&gt;
# Make a &amp;#039;stat&amp;#039; command on a memcache daemon server&lt;br /&gt;
# Outputs to a pipe for later using with syslog-ng&lt;br /&gt;
#&lt;br /&gt;
# alexandre.abreu at gmail.com&lt;br /&gt;
&lt;br /&gt;
PATH=/bin:/usr/bin:/sbin:/usr/sbin&lt;br /&gt;
SERVER=localhost&lt;br /&gt;
PORT=11111&lt;br /&gt;
FIFO=/export/logs/memcache.fifo&lt;br /&gt;
BUFF=&amp;quot;memcache: &amp;quot;&lt;br /&gt;
&lt;br /&gt;
[ -p $FIFO ] || mkfifo $FIFO || exit 1&lt;br /&gt;
&lt;br /&gt;
exec 666&amp;lt;&amp;gt;/dev/tcp/$SERVER/$PORT || exit 1&lt;br /&gt;
&lt;br /&gt;
append() {&lt;br /&gt;
    BUFF=&amp;quot;$BUFF $@&amp;quot;&lt;br /&gt;
    [ &amp;quot;$1&amp;quot; == &amp;quot;uptime&amp;quot; ] &amp;amp;&amp;amp; RUNTIME=`echo &amp;quot;scale=3; $2/60&amp;quot; | bc`&lt;br /&gt;
&lt;br /&gt;
    [ &amp;quot;$1&amp;quot; == &amp;quot;get_hits&amp;quot; ] &amp;amp;&amp;amp; {&lt;br /&gt;
        rate=`echo &amp;quot;scale=3; $2/$RUNTIME&amp;quot; | bc`&lt;br /&gt;
        rate=`printf &amp;quot;%.3f&amp;quot; $rate`&lt;br /&gt;
        append hitrate $rate&lt;br /&gt;
    }&lt;br /&gt;
    [ &amp;quot;$1&amp;quot; == &amp;quot;get_misses&amp;quot; ] &amp;amp;&amp;amp; {&lt;br /&gt;
        rate=`echo &amp;quot;scale=3; $2/$RUNTIME&amp;quot; | bc`&lt;br /&gt;
        rate=`printf &amp;quot;%.3f&amp;quot; $rate`&lt;br /&gt;
        append missrate $rate&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;stats&amp;quot; &amp;gt;&amp;amp;666&lt;br /&gt;
&lt;br /&gt;
while read line; do&lt;br /&gt;
    line=`echo $line | sed &amp;#039;s|[[:cntrl:]]||g&amp;#039;`&lt;br /&gt;
    set - $line&lt;br /&gt;
    append $2 $3&lt;br /&gt;
    [ &amp;quot;$2&amp;quot; == &amp;quot;limit_maxbytes&amp;quot; ] &amp;amp;&amp;amp; break&lt;br /&gt;
done &amp;lt;&amp;amp;666&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;$BUFF&amp;quot; &amp;gt; $FIFO&lt;br /&gt;
echo &amp;quot;quit&amp;quot; &amp;gt;&amp;amp;666&lt;br /&gt;
exit 0&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
My Version:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#&lt;br /&gt;
# Make a &amp;#039;stat&amp;#039; command on a memcache daemon server&lt;br /&gt;
# Outputs to a pipe for later using with syslog-ng&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
PATH=/bin:/usr/bin:/sbin:/usr/sbin&lt;br /&gt;
SERVER=10.20.30.40&lt;br /&gt;
PORT=11211&lt;br /&gt;
FIFO=/tmp/memcache.fifo&lt;br /&gt;
&lt;br /&gt;
[ -p $FIFO ] || mkfifo $FIFO || exit 1&lt;br /&gt;
&lt;br /&gt;
exec 666&amp;lt;&amp;gt;/dev/tcp/$SERVER/$PORT || exit 1&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;stats&amp;quot; &amp;gt;&amp;amp;666&lt;br /&gt;
&lt;br /&gt;
while read line; do&lt;br /&gt;
    echo $line;&lt;br /&gt;
    FLAG=`echo $line | cut -b 1-3`&lt;br /&gt;
    [ &amp;quot;$FLAG&amp;quot; == &amp;quot;END&amp;quot; ] &amp;amp;&amp;amp; break&lt;br /&gt;
done &amp;lt;&amp;amp;666&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;quit&amp;quot; &amp;gt;&amp;amp;666&lt;br /&gt;
exit 0&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kenneth</name></author>
	</entry>
</feed>