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

标签 php memcached shared-memory

有没有一种方法可以使用 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/

相关文章:

php - Symfony 6 中的 VichUploaderBundle

docker - 如何刷新Docker memcached的所有缓存内容?

c - c中共享内存的重新分配

c++ - C 中的 fork()、共享内存和指针

c - 共享内存和memcpy问题

php - Yii2 - 在运行时附加组件

php - 使用 jquery-chosen 和选定的值

php - 了解 url 缩短函数

memcached - play框架2.0支持memcached吗?

memcached - 如何找到从内存缓存中驱逐的键?