1.美圖
2. 安裝 Homebrew
3.安裝 memcached
安裝前,可以先查找一下,看看有沒有:
base) lcc@lcc github$ brew search memcache
==> Formulae
libmemcached memcache-top memcached memcacheq
(base) lcc@lcc github$
說明和關鍵字memcache相關的有上面這四個,這樣就確認了,有我們需要的東西,第一個是客戶端,第三個是服務器。
3.1 安裝服務器
(base) lcc@lcc github$ brew install memcached
=> Installing dependencies for memcached: libevent
==> Installing memcached dependency: libevent
==> Downloading https://homebrew.bintray.com/bottles/libevent-2.1.11_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/1e/1e14fc34baae0b65cac6d7c75bc5ed0ccb1f6bbaa30c8f0f8477ab8ba85fb3c5?__gda__=exp=1577504066~hmac=62a6
######################################################################## 100.0%
==> Pouring libevent-2.1.11_1.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/libevent/2.1.11_1: 1,063 files, 5MB
==> Installing memcached
==> Downloading https://homebrew.bintray.com/bottles/memcached-1.5.20.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring memcached-1.5.20.mojave.bottle.tar.gz
==> Caveats
To have launchd start memcached now and restart at login:
brew services start memcached
Or, if you don't want/need a background service you can just run:
/usr/local/opt/memcached/bin/memcached
==> Summary
🍺 /usr/local/Cellar/memcached/1.5.20: 11 files, 221.8KB
==> Caveats
==> memcached
To have launchd start memcached now and restart at login:
brew services start memcached
Or, if you don't want/need a background service you can just run:
/usr/local/opt/memcached/bin/memcached
(base) lcc@lcc github$
從上面安裝日誌,可以看出:
- 安裝 memcached 前,先安裝了其所依賴的 libevent 庫
- 下載的libevent和memcached,被安裝到/usr/local/Cellar下面,但是又自動在/usr/local/bin下面建立了軟連接,方便使用。
3.2 查看
(base) lcc@lcc github$ which memcached
/usr/local/bin/memcached
(base) lcc@lcc github$
(base) lcc@lcc github$ memcached -h
memcached 1.5.20
-p, --port=<num> TCP port to listen on (default: 11211)
-U, --udp-port=<num> UDP port to listen on (default: 0, off)
-s, --unix-socket=<file> UNIX socket to listen on (disables network support)
-A, --enable-shutdown enable ascii "shutdown" command
-a, --unix-mask=<mask> access mask for UNIX socket, in octal (default: 0700)
-l, --listen=<addr> interface to listen on (default: INADDR_ANY)
-d, --daemon run as a daemon
-r, --enable-coredumps maximize core file limit
-u, --user=<user> assume identity of <username> (only when run as root)
-m, --memory-limit=<num> item memory in megabytes (default: 64 MB)
-M, --disable-evictions return error on memory exhausted instead of evicting
-c, --conn-limit=<num> max simultaneous connections (default: 1024)
-k, --lock-memory lock down all paged memory
-v, --verbose verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/responses)
-vvv extremely verbose (internal state transitions)
-h, --help print this help and exit
-i, --license print memcached and libevent license
-V, --version print version and exit
-P, --pidfile=<file> save PID in <file>, only used with -d option
-f, --slab-growth-factor=<num> chunk size growth factor (default: 1.25)
-n, --slab-min-size=<bytes> min space used for key+value+flags (default: 48)
-L, --enable-largepages try to use large memory pages (if available)
-D <char> Use <char> 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, --threads=<num> number of threads to use (default: 4)
-R, --max-reqs-per-event maximum number of requests per event, limits the
requests processed per connection to prevent
starvation (default: 20)
-C, --disable-cas disable use of CAS
-b, --listen-backlog=<num> set the backlog queue limit (default: 1024)
-B, --protocol=<name> protocol - one of ascii, binary, or auto (default)
-I, --max-item-size=<num> adjusts max item size
(default: 1mb, min: 1k, max: 1024m)
-F, --disable-flush-all disable flush_all command
-X, --disable-dumping disable stats cachedump and lru_crawler metadump
-Y, --auth-file=<file> (EXPERIMENTAL) enable ASCII protocol authentication. format:
user:pass\nuser2:pass2\n
-e, --memory-file=<file> (EXPERIMENTAL) mmap a file for item memory.
use only in ram disks or persistent memory mounts!
enables restartable cache (stop with SIGUSR1)
-o, --extended comma separated list of extended options
most options have a 'no_' prefix to disable
- maxconns_fast: immediately close new connections after limit
- hashpower: an integer multiplier for how large the hash
table should be. normally grows at runtime.
set based on "STAT hash_power_level"
- tail_repair_time: time in seconds for how long to wait before
forcefully killing LRU tail item.
disabled by default; very dangerous option.
- hash_algorithm: the hash table algorithm
default is murmur3 hash. options: jenkins, murmur3
- lru_crawler: enable LRU Crawler background thread
- lru_crawler_sleep: microseconds to sleep between items
default is 100.
- lru_crawler_tocrawl: max items to crawl per slab per run
default is 0 (unlimited)
- lru_maintainer: enable new LRU system + background thread
- hot_lru_pct: pct of slab memory to reserve for hot lru.
(requires lru_maintainer)
- warm_lru_pct: pct of slab memory to reserve for warm lru.
(requires lru_maintainer)
- hot_max_factor: items idle > cold lru age * drop from hot lru.
- warm_max_factor: items idle > cold lru age * this drop from warm.
- temporary_ttl: TTL's below get separate LRU, can't be evicted.
(requires lru_maintainer)
- idle_timeout: timeout for idle connections
- slab_chunk_max: (EXPERIMENTAL) maximum slab size. use extreme care.
- watcher_logbuf_size: size in kilobytes of per-watcher write buffer.
- worker_logbuf_size: size in kilobytes of per-worker-thread buffer
read by background thread, then written to watchers.
- track_sizes: enable dynamic reports for 'stats sizes' command.
- no_hashexpand: disables hash table expansion (dangerous)
- modern: enables options which will be default in future.
currently: nothing
- no_modern: uses defaults of previous major version (1.4.x)
(base) lcc@lcc github$
4.安裝 libmemcached
繼續安裝客戶端庫:
base) lcc@lcc github$ brew install libmemcached
==> Downloading https://homebrew.bintray.com/bottles/libmemcached-1.0.18_2.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/0c/0c7902542fe3b047ce0a512296b1ec3e0da3d731fef0cbd5143152ce17d8f778?__gda__=exp=1577504392~hmac=f955
######################################################################## 100.0%
==> Pouring libmemcached-1.0.18_2.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/libmemcached/1.0.18_2: 231 files, 1.7MB
4.1 啓動服務器
先默認參數啓動吧:
(base) lcc@lcc github$ /usr/local/bin/memcached -d
(base) lcc@lcc github$
5.測試
5.1 啓動 memcached
(base) lcc@lcc github$ /usr/local/bin/memcached -d -m 2048 -l localhost -p 11211
這會以守護程序的形式啓動 memcached(-d),爲其分配 2GB 內存(-m 2048),並指定監聽 localhost,即端口 11211。您可以根據需要修改這些值,但以上設置足以完成本文中的練習。接下來,您需要連接到 memcached。您將使用一個簡單的 telnet 客戶機連接到 memcached 服務器。
具體參數解釋如下:
-p 監聽的端口
-l 連接的IP地址, 默認是本機
-d start 啓動memcached服務
-d restart 重起memcached服務
-d stop|shutdown 關閉正在運行的memcached服務
-d install 安裝memcached服務
-d uninstall 卸載memcached服務
-u 以的身份運行 (僅在以root運行的時候有效)
-m 最大內存使用,單位MB。默認64MB
-M 內存耗盡時返回錯誤,而不是刪除項
-c 最大同時連接數,默認是1024
-f 塊大小增長因子,默認是1.25
-n 最小分配空間,key+value+flags默認是48
-h 顯示幫助
大多數操作系統都提供了內置的 telnet 客戶機,但如果您使用的是基於 Windows 的操作系統,則需要下載第三方客戶機。我推薦使用 PuTTy。
安裝了 telnet 客戶機之後,執行清單 4 中的命令:
5.2 連接到 memcached
(base) lcc@lcc github$ telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
如果一切正常,則應該得到一個 telnet 響應,它會指示 Connected to localhost(已經連接到 localhost)。如果未獲得此響應,則應該返回之前的步驟並確保 libevent 和 memcached 的源文件都已成功生成。
6.命令
您現現已經登錄到 memcached 服務器。此後,您將能夠通過一系列簡單的命令來與 memcached 通信。9 個 memcached 客戶端命令可以分爲三類:
基本
高級
管理