如何在 PHP 的应用程序范围内保存数据? [英] How do I save data in an application scope in PHP?

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

问题描述

我是一名 Java 和 C# 开发人员,而且我承认我的 PHP 不是那么好.

I'm a Java and C# developer, and, I admit, I'm not that good in PHP.

我需要在应用程序范围内存储一个对象,只要应用程序本身正在运行,该对象就会存在.我无法将它保存在 Session 中,因为它已过期,我也无法将其序列化到磁盘.

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 包含对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:

  1. 它不是特定于用户会话的(如果是,则放入 $_SESSION[])
  2. 这不是真正的长期(如果是,请使用文件系统)
  3. 仅在一台 PHP 服务器上需要(如果不需要,请考虑使用 memcached)
  4. 您希望它可以立即用于您网站的每个页面,甚至是其他(非关联)PHP 程序.
  5. 您不会介意其中存储的所有数据在 Apache 重新加载/重新启动时都会丢失.
  6. 您希望数据访问速度远快于基于文件、memcached 或(尤其是)基于数据库的数据.

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屋!

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