How to persist objects between requests in PHP

后端 未结 3 1194
南笙
南笙 2020-12-15 20:53

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 t

相关标签:
3条回答
  • 2020-12-15 21:05

    I think you're making some incorrect generalizations. All of those frameworks (ex: Rails) can be run with different configurations. Under some, a process is created for every request. This obviously hurts performance, but it shows that these frameworks don't rely on a long-running process. They can set things up (reparse config files, create objects, etc.) every request if needed.

    Of course, mod_php (the way PHP is usually used) runs inside the web server process, unlike CGI. So I don't see anything fundamentally different between CakePHP (for example) and Rails.

    I think perhaps you are looking for something like Python's WSGI or Ruby's Rack, but for PHP. This specifies an interface (independent of how the language is run) for an application. For a new request, a new instance of an application object is created. As far as I know, this does not exist for PHP.

    0 讨论(0)
  • 2020-12-15 21:14

    PHP (and ruby for that matter) are interpretive languages. That is they parse the files each time they are requested and I suppose you could say are converted to a pseudo byte code. It is more 'apparent' one could say that PHP is more like this than say RoR but they both behave the same way.

    The feature of persisting data between requests is a feature of the server not of the language itself. For example, the RoR routing you speak of is in fact cached but that's cached in the server's local memory. It isn't compiled and stored for faster readins. The server (and by server I mean both the box & the web service instances) restarts this information is gone. The 'setting up the framework' you speak of still involves parsing EACH file involved in the framework. Rails parses each file during the request again and again, the production level features may in fact cache this data in memory but certainly in development it does not. The only reason I mention that is because it illustrates that it's a feature of the server not the language.

    To achieve the same thing in PHP you could use Zend Server. As far as I know this is the only PHP interpreter that will 'compile' and use byte code when told to. Otherwise you'll need to find a way to store the data you want to persist over requests. APC as you mentioned is a very powerful feature, a more distributed one is Memcached and then of course there's more persistent forms like disc & sql.

    I am interested in knowing why you'd like this particular feature. Are you noticing performance issues that would be 'solved' by doing this?

    0 讨论(0)
  • 2020-12-15 21:27

    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.

    0 讨论(0)
提交回复
热议问题