如何在PHP的应用程序范围内保存数据? [英] How do I save data in an application scope in PHP?
问题描述
我是Java和C#开发人员,并且我承认,我在PHP方面不那么出色.
I'm a Java and C# developer, and, I admit, I'm not that good in PHP.
我需要将对象存储在应用程序范围内,只要该应用程序本身正在运行,该对象就可以存在.我无法将其保存在会话中,因为它已过期,也无法将其序列化到磁盘.
I need to store an object in an application scope that lives as long as the app itself is running. I can't save it in the Session, because it expires, also I can't serialize it to disk.
PHP中是否有类似C#Application
对象的东西?
Is there something like a C# Application
object in PHP?
推荐答案
2018对于APC来说,时间不是,特别是因为PHP 7包括对BPC的捆绑支持Zend Optimizer +,其功能大致相同(密钥存储区除外).如今,密钥存储方面已被分入 APCu项目.
2018 Time has not been kind to APC, especially since PHP 7 includes bundled support for Zend Optimizer+, which does largely the same thing (except the key-store). These days, the key store aspect has been forked over into the APCu project.
但是,在2018年,首选的首选密钥存储区是 Redis .有关详细信息,请参见 ext-redis项目 .
However, in 2018, the preferred key-store of choice is Redis. See the ext-redis project for details.
PHP具有各种应用范围.它称为 APC(备用PHP缓存) .
PHP has an application scope of sorts. it's called APC (Alternative PHP Cache).
如果数据符合以下条件,则应将其缓存在APC中:
Data should be cached in APC if it meets the following criteria:
- 它不是特定于用户会话的(如果是,则放在$ _SESSION []中)
- 这不是长期的(如果是这样,请使用文件系统)
- 仅在一台PHP服务器上需要它(如果不需要,请考虑使用 内存缓存 )
- 您希望它即时可用于站点的每个页面,甚至其他(非关联)PHP程序也可以使用.
- 您不介意在Apache重新加载/重新启动时丢失存储在其中的所有数据.
- 您希望数据访问比基于文件,内存缓存或(尤其是)基于数据库的访问速度快得多.
APC已经安装在许多主机上,但是请按照上述指南进行安装.然后,您需要执行以下操作:
APC is installed on a great many hosts already, but follow the aforementioned guide to get installed on your box. Then you do something like this:
if (apc_exists('app:app_level_data') !== false)
{
$data = apc_fetch('app:app_level_data');
}
else
{
$data = getFromDB('foo');
apc_store('app:app_level_data', $data);
}
这篇关于如何在PHP的应用程序范围内保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!