|
Posted by Rasmus Lerdorf on 09/27/05 10:23
Kevin Wang wrote:
> My php5 web application needs to parse/marshall a bunch of large xml files into
> php5 objects at the beginning of handling each request. These xml files are
> static across all the requests and it is really time consuming to
> parse/marshall them into php5 objects.
What sort of PHP 5 objects? Do you mean simplexml objects, or do you mean
dom objects?
> I am wondering if there is any means to cache these xml objects so that each
> request will not go through the same time consuming xml parsing/building
> operation. Serialization doesn't work in my case as deserialization is also
> very expensive.
>
> I am using Apache as the web server. What I am thinking is that if php5 allows
> us to keep certain objects (and their references) around across all the
> requests in a child process instead of cleaning all the object memory every
> time after a script/request is finished.
Generally the best way to do this is to parse the data down closer to its final usage.
Typically the final thing you need is not a simplexml or a dom object, what you really
need is structured data and this can typically be represented with an associative
array. Once you have it down to an array, you can use pecl/apc and its apc_store()/
apc_fetch() functions to cache these in shared memory without the slowdown of
serialization. A decent example of this technique can be found in the little simple_rss
function I wrote a while ago which parses all sorts of RSS feeds into a nested array and
caches this final array in shared memory using APC.
There really is no decent way to cache an object in shared memory without serialization.
The simpler data types can however be cached with APC. Making them persistent is also
a problem as it completely violates PHP's request sandbox concept. If you really feel
you need this, write yourself a PHP extension and initialize these objects in your MINIT
hook so they only get loaded at server startup and they will be available for the life of
that process.
Jasper Bryant-Greene wrote:
> Have you looked at memcache?
> http://www.php.net/manual/en/ref.memcache.php
>
> You install and run the memcached server (it can be on localhost for a
> single server setup, or for many servers it can be shared if the
> interconnects are fast enough). Then you can cache just about any PHP
> variable, including objects, in memory.
He did say that serialization wasn't an option and you can't use memcached
without serializing. You may not realize you are serializing, but the memcache
extension serializes internally. There was also no mention of needing to cache
across servers here.
-Rasmus (attempting to use the new Yahoo! Mail webmail for php-general)
Navigation:
[Reply to this message]
|