最後更新: 2019-09-09
介紹
memcache 是一個 distributed memory object caching system
in-memory key-value store for small chunks of arbitrary data (strings, objects)
安全問題:
clients may freely connect to any server ==> deployments of Memcached exist within trusted networks
(memcached is built for speed, not for security)
目錄
- Wiki 上的 Example
- Debian 安裝 memcached
- Test with telnet
- + / - 數
- Flush Contents
- memcached-tool
- Centos 7 - memcached
- php module
- Connect with UNIX Socket
Wiki 上的 Example
get_foo 與 update_foo
// 沒有 memcache 的 function get_foo(int userid) { data = db_select("SELECT * FROM users WHERE userid = ?", userid); return data; } // 有 memcache 的 function get_foo(int userid) { /* first try the cache */ data = memcached_fetch("userrow:" + userid); if (!data) { /* not found : request database */ data = db_select("SELECT * FROM users WHERE userid = ?", userid); /* then store in cache until next get */ memcached_add("userrow:" + userid, data); } return data; } function update_foo(int userid, string dbUpdateString) { /* first update database */ result = db_execute(dbUpdateString); if (result) { /* database update successful : fetch data to be stored in cache */ data = db_select("SELECT * FROM users WHERE userid = ?", userid); /* the previous line could also look like data = createDataFromDBString(dbUpdateString); */ /* then store in cache until next get */ memcached_set("userrow:" + userid, data); } }
Debian 安裝 memcached
apt install memcached
Proccess
ps aux | grep memcache
/usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
- -m # -m, --memory-limit=N. Default: 64
- -p PORT # Default: 11211
- -u USER # Default: memcache
- -l IP # Default to 127.0.0.1
- -P FILE # path to pidfile
設定檔 - memcached.conf
/etc/memcached.conf
# Run memcached as a daemon. -d # Log memcached's output to /var/log/memcached logfile /var/log/memcached.log
改用 unix socket
#-l 127.0.0.1 #-p 11211 -s /tmp/memcached.sock -a 666
Test with telnet
Usage
telnet localhost 11211
Notes
使用 nc 會更方便
echo 'stats slabs' | nc localhost 11211
Command
version
VERSION 1.4.5
stats
STAT pid 10234
STAT uptime 1047218
STAT time 1575357582
STAT version 1.5.20
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 1186.735778
STAT rusage_system 1341.286057
STAT max_connections 1024
STAT curr_connections 73
STAT total_connections 4324
STAT rejected_connections 0
...
add <key> <flags> <exptime> <bytes>
add mykey 0 900 4
1234 # input 4 byte value, 按 'ENTER' 後 Save STORED
flag
- flag 是一個16位的無符號整數
- 屬於自定義標記
- 在緩存條目被 set 或者 get 的時一起返回
get <key>
get mykey
VALUE mykey 0 4 1234 END
set <key> <flags> <exptime> <bytes>
* SET is there to update the value, regardless of whether it already exists.
set mykey 0 900 4
test STORED
delete key
delete mykey
DELETED
dump
stats slabs
# 沒有 data 時
STAT active_slabs 0 STAT total_malloced 0 END
# 當有 data 時
STAT 11:chunk_size 872 STAT 11:chunks_per_page 1202 STAT 11:total_pages 1 STAT 11:total_chunks 1202 STAT 11:used_chunks 1 STAT 11:free_chunks 1 STAT 11:free_chunks_end 1200 STAT 11:mem_requested 769 STAT 11:get_hits 4 STAT 11:cmd_set 3 STAT 11:delete_hits 0 STAT 11:incr_hits 0 STAT 11:decr_hits 0 STAT 11:cas_hits 0 STAT 11:cas_badval 0 STAT active_slabs 4 STAT total_malloced 4193280
說明
一共有 "4" 個 slab, 而 "11" 是其中一個 slab 的 ID
stats cachedump slab_id limit_num_of_key
# first digit is the numeric ID for the slab and the second digit is how many items you want to retrieve.
# limit_num_of_key=0 means all items
stats cachedump 1 10
ITEM mytest1 [4 b; 1383214507 s] ITEM mytest2 [1 b; 1383214507 s] END
stats items
# slab id # | # \/ STAT items:1:number 2 STAT items:1:age 1772 STAT items:1:evicted 0 STAT items:1:evicted_nonzero 0 STAT items:1:evicted_time 0 STAT items:1:outofmemory 0 STAT items:1:tailrepairs 0 STAT items:1:reclaimed 0 END
flags: 32-bit unsigned integer that the server store with the data
exptime: seconds, 0 = 不過期
+ / - 數
# 把 mykey + 3
incr mykey 3
當 mykey 不是數字時
CLIENT_ERROR cannot increment or decrement non-numeric value
# 把 mykey - 3
decr mykey 3
Flush Contents
# It does not free up or flush memory at all, it just causes all items to expire.
flush_all [sec]
OK
memcached-tool
Usage: memcached-tool <host[:port]> [mode]
memcached-tool 127.0.0.1 [display] # shows slabs (default)
# Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM 2 104B 58991s 1 1 no 0 0 0 4 176B 58991s 1 1 no 0 0 0
memcached-tool 127.0.0.1 stats # shows general stats
#127.0.0.1:11211 Field Value accepting_conns 1 auth_cmds 0 auth_errors 0 bytes 694 bytes_read 17657 bytes_written 18230 cas_badval 0 cas_hits 0 cas_misses 0 cmd_flush 0 cmd_get 109 cmd_set 193 conn_yields 0 connection_structures 20 curr_connections 18 curr_items 6 decr_hits 0 decr_misses 0 delete_hits 0 delete_misses 0 evictions 0 get_hits 102 get_misses 7 incr_hits 38 incr_misses 27 limit_maxbytes 67108864 listen_disabled_num 0 pid 23536 pointer_size 32 rusage_system 3.145521 rusage_user 0.625904 threads 4 time 1435203879 total_connections 71 total_items 155 uptime 60670 version 1.4.4
memcached-tool 127.0.0.1 dump # dumps keys and values
Dumping memcache contents Number of buckets: 0 Number of items :
libmemcached-tools
Commandline tools for talking to memcached via libmemcached
安裝
apt-get install libmemcached-tools
- /usr/bin/memccapable
- /usr/bin/memccat
- /usr/bin/memccp
- /usr/bin/memcdump
- /usr/bin/memcerror
- /usr/bin/memcexist
- /usr/bin/memcflush
- /usr/bin/memcparse
- /usr/bin/memcpingr
- /usr/bin/memcrm
- /usr/bin/memcslap
- /usr/bin/memcstat
- /usr/bin/memctouch
# ping
memcping --servers 192.168.200.1
Failed to ping 192.168.200.1:11211 SYSTEM ERROR
# operating status of memcached servers
memcstat --servers=localhost
Server: localhost (11211) pid: 42590 uptime: 175 time: 1717660870 version: 1.6.14 ...
# dumps a list of "keys"
e.g.
memcdump --server localhost
231:IMAP:mailboxes.05c7329682137ecdb00f5e396343c575 231:IMAP:messagecount
# outputs keys stored in a memcached(1) server
memcat [options] key
e.g.
memccat --server localhost 231:IMAP:messagecount
Centos 7 - memcached
安裝 memcached
yum install memcached
memcached 設定
# /etc/sysconfig/memcached
TCP
PORT="11211"
USER="memcached"
# max simultaneous connections; the default is 1024
MAXCONN="1024"
# the default is 64 megabytes
CACHESIZE="128"
OPTIONS="-l 127.0.0.1"
UNIX Socket
#PORT="11211"
OPTIONS="-a 666 -s /tmp/memcached.sock"
Other Useful Setting
-L # Try to use large memory pages (if available).
-b <num> # Set the backlog queue limit to <num> connections. The default is 1024.
Enable service
systemctl start memcached
systemctl enable memcached
Checking
netstat -ntlp | grep memcached
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 31179/memcached
telnet 127.0.0.1 11211
stats items
Connect with UNIX Socket
Tools: nc 或 socat
- nc -U /var/run/socket
- socat - UNIX-CONNECT:/tmp/memcached.sock