专属目录
Mac 之 Memcached 的安装与使用
前言
前面我们说了 Spring boot 集成 Memcached,不了解的小伙伴们可以点击查看,那么集成完成了以后,需要在日常需求中简单快速
的操作 Memcached 缓存存储的数据,我们又说了 Java 工具类之 Memcached 工具类,可是没有安装 Memcached 缓存系统
又怎么能够使用 Memcached 呢,接下来我们就来说下 Mac 上如何安装和使用 Memcached
!Windows
系统的我们后续再说
!
瞧好了!
安装
在 Mac
系统上安装 Memcached
相对于 Windows
比较简单
,只需要使用 brew
命令进行安装就OK了
使用 brew
安装,下面为执行的安装命令
brew install memcached
执行命令后,小伙伴们只需要执行完上述命令后,稍候片刻,出现下述命令后,则表示安装完成✅,若与下述不一样,可不必纠结,因为我使用的 Mac 以前没有安装过,一些自制的软件都没有更新,所以会先更新自制的软件和下载一些必须的软件!
如若看到以下信息就代表安装成功了
==> 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
完整的安装过程信息!
wht:~ wht$ brew install memcached
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
alsa-lib libpciaccess libxxf86vm
asroute libpthread-stubs localstack
cdktf libseccomp matplotplusplus
clang-format@8 libsm mhonarc
cloudformation-guard libx11 openfst
commitizen libxau ormolu
croc libxaw periscope
dbdeployer libxaw3d podman
diskonaut libxcb prometheus-cpp
envoy libxcomposite protoc-gen-go-grpc
fleet-cli libxcursor protoc-gen-gogofaster
flit libxdamage python@3.9
font-util libxdmcp rustscan
git-hound libxext server-go
gitql libxfixes shtools
halide libxfont snap
httpx libxft sponge
infracost libxi structurizr-cli
ioctl libxinerama terrascan
ladspa-sdk libxkbfile tfsec
leaf libxmu trunk
libaio libxpm util-macros
libdmx libxrandr vivid
libfontenc libxrender vtk@8.2
libfs libxres webify
libgccjit libxscrnsaver x86_64-elf-gdb
libhandy libxshmfence xcb-proto
libice libxt xdpyinfo
libirecovery libxtst xorgproto
libmnl libxv xtrans
libnetfilter-queue libxvmc
libnfnetlink libxxf86dga
==> Updated Formulae
Updated 2885 formulae.
==> Renamed Formulae
gst-validate -> gst-devtools
==> Deleted Formulae
xu4
==> Downloading https://homebrew.bintray.com/bottles/openssl%401.1-1.1.1h.catali
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/4e5357c0cfd55cfa4ef0b
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/libevent-2.1.12.catalina.bo
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/b5f5e7607d76b9b41ecac
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/memcached-1.6.7.catalina.bo
######################################################################## 100.0%
==> Installing dependencies for memcached: openssl@1.1 and libevent
==> Installing memcached dependency: openssl@1.1
==> Pouring openssl@1.1-1.1.1h.catalina.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/usr/local/etc/openssl@1.1/certs
and run
/usr/local/opt/openssl@1.1/bin/c_rehash
openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.
If you need to have openssl@1.1 first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/wht/.bash_profile
For compilers to find openssl@1.1 you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
For pkg-config to find openssl@1.1 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"
==> Summary
🍺 /usr/local/Cellar/openssl@1.1/1.1.1h: 8,067 files, 18.5MB
==> Installing memcached dependency: libevent
==> Pouring libevent-2.1.12.catalina.bottle.tar.gz
🍺 /usr/local/Cellar/libevent/2.1.12: 57 files, 1.9MB
==> Installing memcached
==> Pouring memcached-1.6.7.catalina.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.6.7: 12 files, 270.9KB
==> Caveats
==> openssl@1.1
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/usr/local/etc/openssl@1.1/certs
and run
/usr/local/opt/openssl@1.1/bin/c_rehash
openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.
If you need to have openssl@1.1 first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/wht/.bash_profile
For compilers to find openssl@1.1 you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
For pkg-config to find openssl@1.1 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"
==> 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
wht:~ wht$
安装成功后,可以使用以下方式启动
、停止
、重启memcached
并 查看memcached的状态
。
启动
启动 memcached 有两种方式
可以启动
第一种【建议使用】
安装完成后,其实命令信息中已经告诉了我们如何启动 memcached
启动 memcached
执行以下命令
brew services start memcached
第二种 【个人不建议使用】
第二种启动方法,执行以下命令启动
memcached -p 11211 -u nobody -c 1024 -m 64m -d -vv
启动命令选项解析
选项 | 说明 |
---|---|
-p | 使用的TCP端口,默认为11211 |
-u | 用户名(只使用root 运行的时候) |
-c | 最大连接数 |
-m | 最大内存大小默认为64m |
-d | 作为daemon在后台启动 |
-vv | 用very verbose模式启动,调试信息和错误输出到控制台 |
查看状态
查看 memcached 状态
执行以下命令即可
ps -ef | grep memcached
停止
停止 memcached
执行以下命令即可
brew services stop memcached
重启
重启 memcached
执行以下命令即可
brew services restart memcached
使用
连接 memcached
启动 memcached 后
,可直接使用 telnet
命令 连接到 memcached 端口
本地连接使用 localhost
或者127.0.0.1
都可以
telnet localhost 11211
或
telnet 127.0.0.1 11211
缓存数据写入
set test 0 0 13
memcachedTest
#显示 `STORED`,表示数据写入成功
STORED
语法解析
写入缓存的语法为
set key flag expires byte
对应选项的解析
选项 | 说明 |
---|---|
key | 为缓存自起用来区分值 value的key |
flag | 表示是否压缩数据,0不压缩,1压缩 |
expires | 指定数据保存的时间,一种方法是秒数(不能超过30天),另外一种可以使用unix 时间戳指定,0 为有效期无限 |
byte | 保存值的字节数 |
读取对应key的缓存数据
get test
# 显示如下,表示数据读取成功
VALUE test 0 13
memcachedTest
END
退出连接
直接输入 telnet
的退出命令
: quit
即可
quit
总结
当你看到这今天的 Mac 安装与使用 Memcached 就到此结束了。快去安装试试,非常nice的缓存系统。 别着急,还有更多的秘密等着你来探索!
分享嗨起来,在看点起来,美德传起来,点个星标从此探索之路不迷茫!
微信扫描二维码,关注我的原创日更公众号,可以查看更多程序的秘密!