memcached 键可以包含空格吗?

发布于 2024-11-03 16:25:02 字数 45 浏览 0 评论 0原文

我似乎对带有空格的 memcached 键有问题,尽管我无法准确指出是什么。

I seem to have problems with memcached keys that have spaces, though I can't pinpoint exactly what.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖树树初阳… 2024-11-10 16:25:02

更明确的答案(达斯汀提到,但没有引用):

按键

memcached 存储的数据通过密钥来识别。一把钥匙
是一个文本字符串,应唯一标识客户端的数据
对存储和检索它感兴趣的人。目前
key的长度限制设置为250个字符(当然,正常情况下
客户端不需要使用这么长的密钥);密钥不得包含
控制字符或空格。

来源:protocol.txt (具体版本

A more explicit answer (referred to by Dustin, but not referenced):

Keys

Data stored by memcached is identified with the help of a key. A key
is a text string which should uniquely identify the data for clients
that are interested in storing and retrieving it. Currently the
length limit of a key is set at 250 characters (of course, normally
clients wouldn't need to use such long keys); the key must not include
control characters or whitespace.

Source: protocol.txt (Specific Version)

凉月流沐 2024-11-10 16:25:02

不可以。Memcached 键不能包含空格。

No. Memcached keys cannot contain spaces.

猫腻 2024-11-10 16:25:02

Memcached 客户端似乎不会为了性能而验证密钥。

我通常做的是创建一个名为 createWellFormedKey($key) 的方法,并将返回的结果传递给 set()get() 方法memcached 客户端的。

我不使用 md5 和 sha1 哈希,除非 base64 版本超过 250 个字符。这是因为 md5 和 sha1 的操作性能成本更高。

示例 PHP 代码如下所示:

/**
 * Generates a well formed key using the following algorithm:
 * 1. base64_encode the key first to make sure all characters are valid
 * 2. Check length of result, less than 250 then return it
 * 3. Length of result more than 250 then create a key that is md5($validKey).sha1($validKey).strlen($validKey)
 */
private function createWellFormedKey($key) {
    // Get rid of all spaces, control characters, etc using base64
    $validKey = base64_encode($key);

    $validKeyLength = strlen($validKey);
    // 250 is the maximum memcached can handle
    if (strlen($validKey) < 250) {
        return $validKey;
    }

    $validKey = md5($validKey).sha1($validKey).$validKeyLength;
    return $validKey;
}

Memcached clients seem not to validate keys in favor of performance.

What I usually do is create a method named createWellFormedKey($key) and pass the returned result to the set() and get() methods of the memcached client.

I do not use md5 and sha1 hashing unless the base64 version exceeds 250 characters. This is because md5 and sha1 are more expensive operations performance wise.

A sample PHP code looks like this:

/**
 * Generates a well formed key using the following algorithm:
 * 1. base64_encode the key first to make sure all characters are valid
 * 2. Check length of result, less than 250 then return it
 * 3. Length of result more than 250 then create a key that is md5($validKey).sha1($validKey).strlen($validKey)
 */
private function createWellFormedKey($key) {
    // Get rid of all spaces, control characters, etc using base64
    $validKey = base64_encode($key);

    $validKeyLength = strlen($validKey);
    // 250 is the maximum memcached can handle
    if (strlen($validKey) < 250) {
        return $validKey;
    }

    $validKey = md5($validKey).sha1($validKey).$validKeyLength;
    return $validKey;
}
故事还在继续 2024-11-10 16:25:02

目前,我正在使用 PHP 来使用 memcached,恕我直言,可以通过使用 md5 和 sha1(或任何其他)等哈希算法轻松解决所描述的问题。

我使用 md5 哈希、sha1 哈希和 sha256 + 给定密钥长度的组合。
显然,这种方法可以减少为两个哈希方法+密钥长度,因此您可以轻松避免使用空格或其他不应出现在密钥中的字符。

在我看来,可以避免哈希碰撞,因为两种哈希算法发生冲突的机会几乎为 0。通过在密钥中额外使用密钥长度,冲突问题为 0。

At the moment I'm playing around with memcached with PHP and the Problem described IMHO can be easily solved by using hash algorithms like md5 and sha1 (or any other).

I'm using a combination of a md5-hash, sha1-hash and sha256 + the length of the key given.
Obviously this method can be reduced to two hash-methods + length of the key, so you can easily avoid using space or other characters that should not be in the key.

In my opinion the hash-collions are avoided because the chance that both hash algorithms have a collision is nearly 0. By additionally using the key length in the key the problem of a collision is 0.

掀纱窥君容 2024-11-10 16:25:02

使用 memcached binary 协议的应用程序可以使用包含空格的键,但仍然有 250 字节的长度限制。

Applications using the memcached binary protocol can use whitespace-containing keys, though there is still a 250-byte length limit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文