gpt4 book ai didi

php - 如何使用 PHP 将数据存储在 RAM 内存中?

转载 作者:可可西里 更新时间:2023-10-31 23:03:14 27 4
gpt4 key购买 nike

有没有一种方法可以使用 PHP 将小数据存储在 RAM 内存中,以便我可以访问不同 session 之间的数据而不是重新生成数据。类似于 memcached 的东西(我无权访问 memcahced)。我目前的解决方案只是将数据保存在文件中。

最佳答案

APC

它的工作方式与 memcached 不同;在 memcached 中,您可以从各种语言(c、python 等)访问数据,而 APC 仅适用于 PHP。

编辑 您确定 APC 安装正确吗?您是否在 php.ini 中添加了 extension=apc.so?并重新启动 apache(我假设你在使用 apache2 的 lamp 服务器上)? phpinfo(); 你对 APC 有什么看法?

这是一个非常适合我的简单测试:

<?php
/*
* page.php
* Store the variable for 30 seconds,
* see http://it.php.net/manual/en/function.apc-add.php
* */
if(apc_add('foo', 'bar', 30)){
header('Location: page2.php');
}else{
die("Cant add foo to apc!");
}

<?php
/*
* page2.php
* */
echo 'foo is been set as: ' . apc_fetch('foo');

p.s: 我更喜欢使用 apc_add 而不是 apc_store,但它们之间的唯一区别是 apc_add 不会覆盖变量,但如果使用相同的 key 调用两次则会失败:

Store the variable using this name. keys are cache-unique, so attempting to use apc_add() to store data with a key that already exists will not overwrite the existing data, and will instead return FALSE. (This is the only difference between apc_add() and apc_store().)

这是脚本的品味/任务问题,但上面的示例也适用于 apc_store。

关于php - 如何使用 PHP 将数据存储在 RAM 内存中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4089361/

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