memcached は、データとオブジェクトをメモリ内にキャッシュすることで、
データベースなどからデータを頻繁に取りにいくことを減らしてパフォーマンスを改善させたり、
セッションをmemcached上に載せる事で、高速なセッション管理を行ったりする事が出来る。

The latest stable memcached release is
v1.4.13
memcached.org

Free & open source, high-performance, distributed memory object caching system,
generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

libeventのインストール

[root@ Sun May 27]# yum install libevent libevent-devel
Loaded plugins: fastestmirror, priorities, security, update-motd
Loading mirror speeds from cached hostfile
* amzn-main: packages.ap-northeast-1.amazonaws.com
* amzn-updates: packages.ap-northeast-1.amazonaws.com
amzn-main | 2.1 kB 00:00
amzn-updates | 2.3 kB 00:00
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package libevent.x86_64 0:1.4.13-1.6.amzn1 will be installed
—> Package libevent-devel.x86_64 0:1.4.13-1.6.amzn1 will be installed
–> Finished Dependency Resolution

省略…
Dependencies ResolvedDownloading Packages:
(1/2): libevent-1.4.13-1.6.amzn1.x86_64.rpm | 114 kB 00:00
(2/2): libevent-devel-1.4.13-1.6.amzn1.x86_64.rpm | 386 kB 00:00

MEMCACHEDインストール

[root@ Sun May 27]# wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
–2012-05-27 17:56:05– http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
Resolving memcached.googlecode.com… 72.14.203.82
Connecting to memcached.googlecode.com|72.14.203.82|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 320751 (313K) [application/x-gzip]
Saving to: “memcached-1.4.13.tar.gz”

100%[==========================================================================>] 320,751 1.37M/s in 0.2s

2012-05-27 17:56:06 (1.37 MB/s) – “memcached-1.4.13.tar.gz” saved [320751/320751]

[root@ Sun May 27]#

[root@ Sun May 27]# tar zxf memcached-1.4.13.tar.gz
[root@ Sun May 27]# ls -l
total 320
drwxr-xr-x 6 1000 1000 4096 Feb 3 06:24 memcached-1.4.13
-rw-r–r– 1 root root 320751 Feb 3 06:27 memcached-1.4.13.tar.gz
[root@ Sun May 27]# cd memcached-1.4.13
[root@ Sun May 27]# ./configure
checking build system type… x86_64-unknown-linux-gnu
checking host system type… x86_64-unknown-linux-gnu
checking target system type… x86_64-unknown-linux-gnu
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for a thread-safe mkdir -p… /bin/mkdir -p
checking for gawk… gawk
checking whether make sets $(MAKE)… yes
checking for gcc… gcc
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether gcc accepts -g… yes

[省略]……………………………………..

[root@ Sun May 27]# make
make all-recursive
make[1]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13′
Making all in doc
make[2]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make all-am
make[3]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make[3]: Nothing to be done for `all-am’.
make[3]: Leaving directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make[2]: Leaving directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make[2]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13′
gcc -std=gnu99 -DHAVE_CONFIG_H -I. -DNDEBUG -g -O2 -pthread -Wall -Werror -pedantic
-Wmissing-prototypes -Wmissing-declarations -Wredundant -decls -fno-strict-aliasing -MT memcached-memcached.o
-MD -MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f ‘memcached.c’ || echo ‘./’`memcached.c

[root@ Sun May 27]# make install
make install-recursive
make[1]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13′
Making install in doc
make[2]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make install-am
make[3]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make[4]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make[4]: Nothing to be done for `install-exec-am’.
test -z “/usr/local/share/man/man1” || /bin/mkdir -p “/usr/local/share/man/man1”
/usr/bin/install -c -m 644 memcached.1 ‘/usr/local/share/man/man1′
make[4]: Leaving directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make[3]: Leaving directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make[2]: Leaving directory `/home/ec2-user/memcached/memcached-1.4.13/doc’
make[2]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13′
make[3]: Entering directory `/home/ec2-user/memcached/memcached-1.4.13′
test -z “/usr/local/bin” || /bin/mkdir -p “/usr/local/bin”
/usr/bin/install -c memcached ‘/usr/local/bin’
test -z “/usr/local/include/memcached” || /bin/mkdir -p “/usr/local/include/memcached”
/usr/bin/install -c -m 644 protocol_binary.h ‘/usr/local/include/memcached’
make[3]: Leaving directory `/home/ec2-user/memcached/memcached-1.4.13′
make[2]: Leaving directory `/home/ec2-user/memcached/memcached-1.4.13′
make[1]: Leaving directory `/home/ec2-user/memcached/memcached-1.4.13′
[root@ Sun May 27]#

[root@ Sun May 27]# whereis memcached
memcached: /usr/local/bin/memcached
[root@ Sun May 27]#

基本起動確認

[ec2-user@ Sun May 27]$ /usr/local/bin/memcached -p 11211 -m 10m -vv
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 229376, now 268435456 <29 send buffer was 229376, now 268435456 <28 server listening (udp) <29 server listening (udp)

バックグラウンド起動し動作検証

デーモンとしてバックグラウンド起動
[ec2-user@ Sun May 27]$ /usr/local/bin/memcached -p 11211 -m 10m -d
[ec2-user@ Sun May 27]$ ps -ef | grep mem
ec2-user 18639 1 0 18:04 ? 00:00:00 /usr/local/bin/memcached -p 11211 -m 10m -d
ec2-user 18646 17197 0 18:04 pts/0 00:00:00 grep mem
[ec2-user@ Sun May 27]$

[ec2-user@ Sun May 27]$ /usr/local/bin/memcached -h
memcached 1.4.13
-p TCP port number to listen on (default: 11211)
-U UDP port number to listen on (default: 11211, 0 is off)
-s UNIX socket path to listen on (disables network support)
-a access mask for UNIX socket, in octal (default: 0700)
-l interface to listen on (default: INADDR_ANY, all addresses)
may be specified as host:port. If you don’t specify
a port number, the value you specified with -p or -U is
used. You may specify multiple addresses separated by comma
or by using -l multiple times
-d run as a daemon
-r maximize core file limit
-u assume identity of (only when run as root)
-m max memory to use for items in megabytes (default: 64 MB)
-M return error on memory exhausted (rather than removing items)
-c max simultaneous connections (default: 1024)
-k lock down all paged memory. Note that there is a
limit on how much memory you may lock. Trying to
allocate more than that would fail, so be sure you
set the limit correctly for the user you started
the daemon with (not for -u user;
under sh this is done with ‘ulimit -S -l NUM_KB’).
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-vvv extremely verbose (also print internal state transitions)
-h print this help and exit
-i print memcached and libevent license
-P save PID in , only used with -d option
-f chunk size growth factor (default: 1.25)
-n minimum space allocated for key+value+flags (default: 48)
-L Try to use large memory pages (if available). Increasing
the memory page size could reduce the number of TLB misses
and improve the performance. In order to get large pages
from the OS, memcached will allocate the total item-cache
in one large chunk.
-D Use as the delimiter between key prefixes and IDs.
This is used for per-prefix stats reporting. The default is
“:” (colon). If this option is specified, stats collection
is turned on automatically; if not, then it may be turned on
by sending the “stats detail on” command to the server.
-t number of threads to use (default: 4)
-R Maximum number of requests per event, limits the number of
requests process for a given connection to prevent
starvation (default: 20)
-C Disable use of CAS
-b Set the backlog queue limit (default: 1024)
-B Binding protocol – one of ascii, binary, or auto (default)
-I Override the size of each slab page. Adjusts max item size
(default: 1mb, min: 1k, max: 128m)
-o Comma separated list of extended or experimental options
– (EXPERIMENTAL) maxconns_fast: immediately close new
connections if over maxconns limit
– hashpower: An integer multiplier for how large the hash
table should be. Can be grown at runtime if not big enough.
Set this based on “STAT hash_power_level” before a
restart.
[ec2-user@ Sun May 27]$

telnet経由でデータの入力などの基本動作検証

[ec2-user@ Sun May 27]$ telnet localhost 11211
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
stats
STAT pid 18639
STAT uptime 1139
STAT time 1338110621
STAT version 1.4.13
STAT libevent 1.4.13-stable
STAT pointer_size 64
STAT rusage_user 0.015997
STAT rusage_system 0.015997
STAT curr_connections 10
STAT total_connections 11

set key01 0 0 5
00001
set key02 0 0 5
00002
set key03 0 0 5
00003

memcached

memcached

Comments are closed.

Post Navigation