你可以在 Memcache 中存储一个 PHP 数组吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1203435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:31:16  来源:igfitidea点击:

Can you store a PHP Array in Memcache?

phpmemcached

提问by JasonDavis

Can you store an Array in Memcache?

你可以在 Memcache 中存储一个数组吗?

I would like to store;

我想储存;

  1. A user ID number
  2. A user's photo URL
  3. A users name
  1. 用户 ID 号
  2. 用户的照片 URL
  3. 一个用户名

Together as an array, someone told me you could and then someone told me you couldn't Which is it?

一起作为一个数组,有人告诉我你可以,然后有人告诉我你不能这是哪一个?

回答by hobodave

Yes.

是的。

Memcache::set('someKey', array(
    'user_id' => 1,
    'url' => 'http://',
    'name' => 'Dave'
));

Please see the documentationfor very detailed examples.

有关非常详细的示例,请参阅文档

回答by Jason

max storage size of an item in memcache is 1,048,576 Bytes (1MB), serializing an array does take a bit of space.

内存缓存中项目的最大存储大小为 1,048,576 字节 (1MB),序列化数组确实需要一些空间。

if you were to structure your array as simply as:

如果您要像这样简单地构建数组:

array(
    [0] => 1,
    [1] => 2,
    [2] => 3
)

key being auto generated and value being the user id.

键是自动生成的,值是用户 ID。

a serialized array of 5000 users numbered 1-5000 using this structure has a string length of 67792 characters, 50000 users produces an array of 777794 characters.

使用此结构编号为 1-5000 的 5000 个用户的序列化数组具有 67792 个字符的字符串长度,50000 个用户生成一个包含 777794 个字符的数组。

numbering 100000 to 150000 produces a serialized string of 838917 characters.

编号 100000 到 150000 会生成一个包含 838917 个字符的序列化字符串。

so at 50k users (as referenced in your previous question), you will likely be under the 1MB limit. if you have a local cache though (APC, etc...), use that instead, or if for any reason do NOT need all the IDs at once, I would highly suggest splitting up the results or simply using the DB.

因此,在 50k 用户(如您之前的问题中所述)时,您可能会低于 1MB 限制。如果您有本地缓存​​(APC 等...),请改用它,或者如果出于任何原因不需要一次使用所有 ID,我强烈建议拆分结果或仅使用数据库。

also think about the data structure you are storing in memcached. If you are using the cache simply to give you a list of primary keys to lookup, do you need the other data?

还要考虑您在 memcached 中存储的数据结构。如果您使用缓存只是为了给您提供要查找的主键列表,您是否还需要其他数据?

回答by vishal_g

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");

$id = $_REQUEST['name'];
$key = md5("SELECT * FROM memc where FirstName='{$id}'");
$get_result = array();
$get_result = $memcache->get($key);

if ($get_result) {
    echo "<pre>\n";
    echo "FirstName: " . $get_result['FirstName'] . "\n";
    echo "Age: " . $get_result['Age'] . "\n";
    echo "</pre>\n";
} else {
    $query="SELECT * FROM memc where FirstName='{$id}'";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    echo "<pre>\n";
    echo "FirstName: " . $row[1] . "\n";
    echo "Age: " . $row[3] . "\n";
    echo "Retrieved from the Database\n";
    echo "</pre>\n";
    $memcache->set($key, $row, MEMCACHE_COMPRESSED, 60);
    mysql_free_result($result);
}

add change database setting according to ur requirement ,tested code please try

根据您的要求添加更改数据库设置,测试代码请尝试

回答by Pascal MARTIN

You can store almost whatever you want in memcached.

您几乎可以在 memcached 中存储任何您想要的内容。

See the documentation of memcache::set, which says :

请参阅 的文档memcache::set,其中说:

bool Memcache::set(string $key, mixed $var [, int $flag [, int $expire ]] )

var

The variable to store. Strings and integers are stored as is, other types

are stored serialized.

无功

The variable to store. Strings and integers are stored as is, other types

被序列化存储。

So, yes, you can store an array, or even an object if needed :-)

所以,是的,如果需要,您可以存储一个数组,甚至一个对象:-)


BTW, it's the same with Memcache::addand Memcache::replace


顺便说一句,它与Memcache::addMemcache::replace