PHP:对于所有用户请求仅加载一次属性集(从配置文件)
我有一个包含应用程序特定属性(例如超时等)的文件,我已将其放置在 my_app.ini
属性文件中,并使用 parse_ini_file()
读取该文件。
我应该如何/在哪里让此代码针对所有用户请求运行一次(我不希望我的应用程序在每个请求上读取 .ini
文件)?
我想将配置文件读取代码放入单独的 PHP 中,并将其与 include_once
/require_once
包含在需要访问配置变量的所有页面中。但是:
- 这是否意味着该文件将 每个不同的页面读一次 (自从我有不同的时间以来 多个页面
require_once('config_setup.php')
) ? - 是
require_once
/include_once
在所有用户中仅执行一次 要求?
我有兴趣可能听到其他(更好)的方法来做到这一点。
I have a file which contains application specific properties (e.g. timeouts etc.) which I have placed in a my_app.ini
properties file and which I read using parse_ini_file()
.
How/where should I make this code run exactly once for all my user requests (I do not want my application to read the .ini
file on every request) ?
I though of putting the config file reading code in a separate PHP and including this with include_once
/require_once
in all pages that require access to the config variables. But:
- Will this mean that the file will be
read once for every different page
(so different times since I have
multiple pages withrequire_once('config_setup.php')
)
? - Is
require_once
/include_once
executed only once across all user
requests ?
I am interested in possibly hearing other (better) ways how to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PHP 不保留多个请求之间的状态。这意味着独立 PHP 无法在请求之间将变量存储在内存中。会话保存到磁盘(默认情况下),需要重新解析配置文件等。
但是,您可以使用诸如 APC 或 Memcache/Memcached 在内存中存储变量。这些扩展需要在服务器上编译/安装才能使用它们的功能。
解析 INI 不会成为应用程序的瓶颈。为了简单起见,每次都解析它会更容易。否则你节省的 0.00001 毫秒是不值得的。
include_once()
和require_once()
与持久性无关。它只是告诉 PHP 仅包含该脚本(如果在此请求期间尚未包含该脚本)。PHP doesn't keep state between multiple requests. This means that standalone PHP cannot store variables in memory between requests. Sessions are saved to disk (by default), config files needs to be reparsed, etc.
You can however use extensions such as APC or Memcache/Memcached to store variables in memory. These extensions needs to be compiled/installed on the server so that their functions are available.
Parsing your INI will not be a bottleneck for your application. For simplicity's sake, it's easier just to parse it everytime. That 0.00001 ms you'll be saving otherwise isn't worth it.
include_once()
andrequire_once()
has nothing to do with persistance. It just tells PHP to only include the script if it wasn't already included during this request.include/require_once表示在处理单个请求的过程中该文件只会被加载一次;但是,每当收到新请求时,都会在解析脚本时再次读取该请求。
此外,对于您想要在此处执行的操作,您必须读取配置并将其值放入会话中;那么,如果定义了会话数组中使用的特定密钥,则不需要重新解析文件:
但是,这种方法有一个主要缺点 - 如果配置文件发生更改,已经在站点上的用户将继续使用文件的先前版本。
include/require_once mean that the file will only be loaded once during the processing of a single request; however, whenever a new request is received, it will be read again as the script is parsed.
In addition, for what you want to do here, you would have to read the configuration and place its value in the session; then, if the specific key you use in the session array is defined, you do not need to reparse the file:
However, this approach has a major disadvantage - if the configuration file changes, users that are already on the site will keep on using the previous version of the file.
首先,在正常情况下,读取
*.ini
几乎不会成为任何服务的瓶颈,因此请认真考虑您寻求的优点(哪些是......?)是否超过缺点。我扫描 INI 文件没有问题,尽管我通常使用本机 PHP 进行一些设置,只要非 PHP 程序员不需要访问它们。当然,人们现在会提倡像 APC 和 APC 这样的键/值存储。 Memcached 因为持久性问题,但只有当您的文件对于 ini 文件来说(相对)巨大时,这才是一个优势,否则,如果您尚未使用它,那么它只是开销。我知道如何轻松地在没有 Apache 扩展的情况下跨请求获取(标量)设置的唯一方法是在主机配置中执行一些 SetEnv 业务(因此标量值只是从环境中读取),但是这些充其量是笨拙的,并且需要重新加载服务器或重新启动才能接受更改。
保留 ini 文件,或者可能保留带有设置的直接 PHP 文件,在这种情况下,APC 可以通过缓存文件直到它发生更改来为您带来一点好处。
First of all, reading the
*.ini
is hardly going to be the bottleneck of any service in normal circumstances, so really think whether the advantages you seek (which are...?) outweigh the disadvantages. I have no problem scanning INI files, although I usually use native PHP to do some settings as long as no non-PHP programmer needs to reach them. Of course, people are now going to advocate key/value stores like APC & Memcached because of the persistence issue, but that will only be an advantage if your file is (relatively) huge for an ini file, otherwise it's just overhead if you don't use it already.The only way I know how to get a (scalar) setting across requests without extensions in Apache easily is to do some
SetEnv
business in the host configuration (so scalar values are just read from the environment), but those are kludgy at best, and require a server reload or restart to pick up alterations.Stay with the ini file, or possibly with a straight PHP file with settings, in which case APC could grant you a small advantage by caching the file until it changes.