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 客户端命令可以分为三类:
基本
高级
管理