#cache #performance #service #data-structures

k-cache

rmemstore 的内部缓存实现

1 个不稳定版本

0.1.1 2024 年 8 月 12 日

#1237Web 编程

Download history 130/week @ 2024-08-12

130 每月下载量

Apache-2.0

11KB
156

rmemstore

快速、类型感知的数据结构缓存。

关于

rmemstore 与您可能使用过的其他缓存类似,例如 redis,但它有一些不同之处。rmemstore 的主要目标是类型安全、快速且作为一个数据结构缓存很有用。

当然,实用性是一个持续的过程,因为增长功能需要时间。然而,rmemstore 是一个类型感知的数据结构存储库,这意味着您可以存储映射的映射 - 服务器知道这意味着什么。

现在它很快。当需要淘汰时,rmemstore 使用新的筛子淘汰策略。在 10:1 的读/写比率下,2 个线程在 11 年前的 Intel i5 服务器上能够每秒执行超过 330 万次操作。即使在被推到淘汰的情况下。

rmemstore 基于“安全”的 Rust 代码构建。它不依赖于微妙的技巧来获得速度。它确实使用了标准库,如出色的 tokio,它们可能使用黑暗魔法,但它们是可靠的。

rmemstore 使用裸 TCP - 没有应用程序框架。您的网卡发送到或从 rmemstored 服务器传输的每个 0 和每个 1 都有直接目的。在合适的替代品存在时,发明一种新的看似可移植的线协议是一项雄心勃勃的练习。因此,rmemstore 使用 protosockets,这是上述雄心勃勃和实用主义之间的折衷。

协议

指向 rmemstored 的 TCP 流是一个标准、长度分隔的协议缓冲区 rmemstore.Rpc 结构流。这些消息携带一个 ID,而 rmemstored 用该 ID 进行响应 - 可能是顺序的。它是一个多线程、多路复用服务器。您可以尽可能快地发送尽可能多的数据,前提是您的网络和 CPU 能力允许。

rmemstored 流出的 TCP 流是一个标准、长度分隔的协议缓冲区 rmemstore.Response 结构流。这些消息携带触发响应的 Rpc 的 ID。每个 rmemstore.Rpc 都有一个相应的 rmemstore.Response

传入和传出流是:varint message varint message[...]. 消息前面的 varint 是消息的长度。因此,一旦您读取了 varint 和 varint 的长度,您就有一个完整的消息。

语言

Rust

您可以通过查看rmem了解如何使用客户端。使用方法归结为3行

let mut configuration = rmemstore::ClientConfiguration::new();
let client = configuration.connect(args.host.to_string()).await?;
client.put("some key", "some value").await?;

您还可以放置字典

client.put(
    "some key",
    HashMap::<&str, &str>::from_iter([
        ("hello", "world")
    ]),
).await?;

或者字符串和字典的字典,无论您想要多野

client
    .put(
        "some key",
        HashMap::<&str, MemstoreValue>::from_iter([
            (
                "hello",
                MemstoreValue::String {
                    string: "world".to_string(),
                },
            ),
            (
                "nested",
                MemstoreValue::Map {
                    map: HashMap::from_iter([(
                        "inner".to_string(),
                        MemstoreValue::String {
                            string: "values".to_string(),
                        },
                    )]),
                },
            ),
        ]),
    )
    .await?;

Bash

您可以使用rmem进行存储和获取。

对于字符串,输出稍微简短一些。

$ rmem put foo `{"string": "some value"}`
$ rmem get foo
some value

对于映射,交互有些冗长,但它是有类型的!

$ rmem put foo '{"map": {"bar":{"map":{"baz":{"string": "haha"}, "other": {"string": "verbose"}}, "outer": {"string": "another"}}}}'
$ rmem get foo
{
  "bar": {
    "map": {
      "baz": {
        "string": "haha"
      },
      "other": {
        "string": "verbose"
      }
    }
  }
}

Python

不想使用Rust?任何能够通过TCP发送和接收协议缓冲区编码字节的工具或语言都可以使用rmemstored。请参阅example-python中的另一个语言的示例。请注意,由于Python没有公开protobuf varint编码器,Python可能有点麻烦。

依赖项

~0–10MB
~45K SLoC