如何在 PHP 中的请求之间持久化对象

How to persist objects between requests in PHP(如何在 PHP 中的请求之间持久化对象)
本文介绍了如何在 PHP 中的请求之间持久化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去一直在使用 rails、merb、django 和 asp.net mvc 应用程序.他们的共同点(与问题相关)是他们拥有设置框架的代码.这通常意味着创建持续存在的对象和状态,直到 Web 服务器被回收(例如设置路由,或检查哪些控制器可用等).

I've been using rails, merb, django and asp.net mvc applications in the past. What they have common (that is relevant to the question) is that they have code that sets up the framework. This usually means creating objects and state that is persisted until the web server is recycled (like setting up routing, or checking which controllers are available, etc).

据我所知,PHP 更像是一个 CGI 脚本,每次运行时都会被编译成一些字节码,而在发出请求后它会被丢弃.当然,您可以拥有会话,以便在来自同一用户的请求之间持久化数据,并且我看到有像 APC 这样的扩展,您可以使用它在服务器级别的请求之间持久化对象.

As far as I know PHP is more like a CGI script that gets compiled to some bytecode each time it's run, and after the request it's discarded. Of course you can have sessions, to persist data between requests from the same user, and as I see there are extensions like APC, with which you can persist objects between requests at the server level.

我的问题是:如何创建一个像 rails 之类的 PHP 应用程序?我的意思是一个应用程序在第一次请求时设置框架,然后在第二次和以后的请求中使用已经设置的对象.mod_php 中是否有一些内置的缓存设施?(例如,存储已执行 php 应用程序的编译字节码)还是使用 APC 或一些类似的扩展来解决这个问题的唯一方法?你会怎么做?

My question is: how can one create a PHP application that works like rails and such? I mean an application that on the first requests sets up the framework, then on the 2nd and later requests use the objects that are already set up. Is there some built in caching facility in mod_php? (for example that stores the compiled bytecode of the executed php applications) Or is using APC or some similar extensions the only way to solve this problem? How would you do it?

谢谢.

替代问题:如果我创建一个大型 PHP 应用程序,它的设置时间很长,但运行时间很短(如在上面提到的框架中),那么我应该如何缓存"已经设置的东西(这可能意味着很多事情,除了数据库连接,因为您已经在 PHP 中拥有持久连接).

Alternative question: if I create a large PHP application that has a very large set up time, but minor running time (like in the frameworks mentioned above) then how should I "cache" the things that are already set up (this might mean a lot of things, except for maybe the database connections, because for that you have persistent connections in PHP already).

为了证明大的设置时间是合理的:如果我使用 PHP 反射来检查哪些对象可用并据此设置运行时会怎样.进行大量反思通常很慢,但只需要做一次(并且只有在修改源代码时才重新评估).

To justify large set up time: what if I'm using PHP reflection to check what objects are available and set the runtime according to that. Doing a lot of reflection is usually slow, but one has to do it only once (and re-evaluate only if the source code is modified).

那似乎是APC.很高兴知道它会自动缓存字节码.

It seems it's APC then. The fact that it caches bytecode automatically is good to know.

推荐答案

不确定 APC 是否是唯一的解决方案,但 APC 确实可以解决您的所有问题.

Not sure if APC is the only solution but APC does take care of all your issues.

首先,您的脚本将使用 APC 编译一次,并将字节码存储在内存中.

First, your script will be compiled once with APC and the bytecode is stored in memory.

如果您的设置需要很长时间,您还可以将其作为用户数据缓存在 APC 中.例如,我一直这样做,

If you have something taking long time to setup, you can also cache it in APC as user data. For example, I do this all the time,

            $table = @apc_fetch(TABLE_KEY);

            if (!$table) {
                    $table = new Table(); // Take long time
                    apc_store(TABLE_KEY, $table);
            }

使用 APC,每个服务器实例只执行一次创建表的任务.

With APC, the task of creating table is only performed once per server instance.

这篇关于如何在 PHP 中的请求之间持久化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)