gpt4 book ai didi

php - 你如何在 PHP 中使用内存缓存

转载 作者:可可西里 更新时间:2023-11-01 12:31:42 24 4
gpt4 key购买 nike

我终于在家用电脑上运行了内存缓存,所以我终于可以开始使用它进行开发了!

虽然我正在尝试使用上的代码,但我没有一个好的开始

php.net @ memcache-set我无法使他们发布的任何示例代码正常工作

我试过这个:

<?php
/* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_set($memcache_obj, 'var_key', 'some variable', 0, 30);
echo memcache_get($memcache_obj, 'var_key');
?>


然后

<?php
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
echo $memcache_obj->get('var_key');
?>


并从上面的代码中得到这些错误;

Warning: Memcache::connect() [memcache.connect]: Can't connect to memcache_host:11211, A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060) in C:\webserver\htdocs\test\memcache\index.php on line 36

Warning: Memcache::set() [memcache.set]: Failed to extract 'connection' variable from object in C:\webserver\htdocs\test\memcache\index.php on line 42

Warning: Memcache::get() [memcache.get]: Failed to extract 'connection' variable from object in C:\webserver\htdocs\test\memcache\index.php on line 44


然后我在网上的某个地方找到了这段代码,它确实有效

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
// add cache
$memcache->set('key', $tmp_object, false, 30) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 30 seconds)<br/>\n";
// get cache
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>


我怎样才能使 PHP.net 中的示例正常工作?


此外,我很乐意看到您可能想要分享的任何涉及内存缓存的示例代码我真的很高兴看到一些工作示例

最佳答案

您是否意识到需要用您的主机名和/或本地主机替换“memcache_host”?还是我完全错过了重点?另外,尝试 telnet localhost 11211 然后 telnet your-memcache-host-name 11211 看看是否得到相同的结果(应该)。

关于php - 你如何在 PHP 中使用内存缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1206283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com