Rust 版 Memcached 來了

大家好,我是螃蟹哥。

也許現在 Memcached 用的不多,更多可能使用的 Redis,但曾經風靡一時。在 Go 圈,有一個庫 https://github.com/golang/groupcache,是 Memcached 的作者實現的。今天發現 Rust 圈也有輪子,它就是 memcrsd。

項目地址:https://github.com/memc-rs/memc-rs,官網:https://www.memc.rs/。

這是一個與 Memcached 兼容的通用分佈式內存緩存系統。

它有如下特性:

官方提供了 Docker 鏡像,也可以下載編譯好的。不過目前只提供了 Linux 版本下載:https://www.memc.rs/download。

你也可以通過源碼構建。先安裝 Rust:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

以上會安裝 rustup,這是 Rust 的版本管理工具,同時會安裝上 Rust。

如果已經安裝了,可以通過 rustup 升級到最新版本(因爲 memc.rs 要求 Rust 2021 版):

$ rustup update

之後下載 memc.rs 源碼,編譯安裝:

$ git clone https://github.com/memc-rs/memc-rs.git
$ cd memc-rs
$ cargo build --release

然後運行:

$ ./target/release/memcrsd -v -c 50000 -r 7 -l 0.0.0.0 -v -m 2048
2021-12-24T03:13:33.088441Z  INFO memcrsd: Listen address: 0.0.0.0
2021-12-24T03:13:33.088470Z  INFO memcrsd: Listen port: 11211
2021-12-24T03:13:33.088475Z  INFO memcrsd: Connection limit: 50000
2021-12-24T03:13:33.088478Z  INFO memcrsd: Number of runtimes: 7
2021-12-24T03:13:33.088480Z  INFO memcrsd: Number of threads total: 8
2021-12-24T03:13:33.088482Z  INFO memcrsd: Max item size: 1000000
2021-12-24T03:13:33.088485Z  INFO memcrsd: Memory limit: 2048 MB

該項目中還包含了一個簡單的驗證客戶端,源碼構建時,默認沒有編譯,在項目中的 Cargo.toml 註釋打開:

members = [
    "memcrs"
    "memclt"
]

再次編譯即可。

然後運行測試:

$ ./target/release/memclt
Foo: "test" 0 1
Foo: "bistestbas" 0 3
Server returned: 3050
Version: [("memcache://127.0.0.1:11211?timeout=120&tcp_nodelay=true&protocol=binary""0.0.1")]

也許你不會實際使用 memcrsd,但作爲 Rust 愛好者,可以將該項目作爲學習之用。

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/3oRBYGpGZ35xNB7lLFGynQ