如何使用PHP将数据存储在RAM内存中? [英] How can I store data in RAM memory using PHP?

查看:340
本文介绍了如何使用PHP将数据存储在RAM内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使用PHP在RAM内存中存储小数据,这样我就可以访问不同会话之间的数据,而无需重新生成它.与memcached类似(我无法访问memcahced).我当前的解决方案是将数据保存在文件中.

Is there a way to store small data in RAM memory using PHP so that I can have access to the data between different session instead of regenerating it. Something similar to memcached (I don't have access to memcahced). My current solution is just to save the data in file.

推荐答案

APC ?

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

It works differents from memcached; in memcached you can access the data from various languages (c, python, etc..) while APC works only for PHP.

编辑,您确定正确安装了APC吗? 您是否在php.ini中添加了extension=apc.so?并重新启动apache(假设您使用apache2在灯泡服务器上)?您phpinfo();对APC怎么说?

EDIT are you sure that APC is installed correctly? Did you add extension=apc.so in your php.ini? And to restart apache (im assuming youre on a lamp server with apache2)? What does you phpinfo(); say about APC?

这是一个对我来说非常有效的简单测试:

This is a simply test that work perfectly for me:

<?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不会覆盖变量,但是如果使用相同的键两次调用将失败:

p.s: i prefer to use apc_add over apc_store, but the only difference between them is that apc_add doesnt overwrite the variable but will fail if called twice with the same key:

使用此名称存储变量.键是唯一的缓存,因此尝试使用apc_add()存储具有已存在键的数据将不会覆盖现有数据,而是返回FALSE. (这是apc_add()和apc_store()之间的唯一区别.)

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.

It's a matter of taste/task of the script, but the example above works with apc_store too.

这篇关于如何使用PHP将数据存储在RAM内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆