如何在PHP中的请求之间持久化对象 [英] How to persist objects between requests in PHP

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

问题描述

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

就我所知,PHP更像是一个CGI脚本,每次运行时都会将其编译为某些字节码,并且在请求之后将其丢弃。当然,你可以有会话,在来自同一用户的请求之间保持数据,并且我看到有像APC这样的扩展,你可以在服务器级别的请求之间持久化对象。



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



谢谢。

编辑:这有一个非常大的设置时间,但很小的运行时间(就像在上面提到的框架中那样),那么我应该如何缓存已经建立的事情(这可能意味着很多事情,除了可能的数据库连接,因为你已经在PHP中建立了持久连接)。



证明设置时间很长:如果我使用PHP反射来检查哪些对象可用,以及根据这个设置运行时间。进行大量的反射通常很慢,但一次只能做一次(并且只在源代码被修改时重新评估)。



编辑2:看起来好像那就是APC。它自动缓存字节码的事实是很好的了解。

解决方案

不确定是否APC是唯一的解决方案,但APC确实照顾首先,您的脚本将使用APC编译一次,字节码存储在内存中。



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

  $ table = @apc_fetch(TABLE_KEY); 

if(!$ table){
$ table = new Table(); //花费很长时间
apc_store(TABLE_KEY,$ table);



$ b $ p
$ b

使用APC,创建表的任务仅在每个服务器实例中执行一次。

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).

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.

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?

Thanks.

EDIT: 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).

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).

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

解决方案

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

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

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);
            }

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

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

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