Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache Object in PHP without using serialize

I have a complex object that I create in a PHP script. I am looking for a way to store this object such that subsequent requests do not have to recreate it, or spend time unserializing and rebuilding it. Using xdebug I find that I spend half of the entire request time building this object. Even when I store the object explicitly in APC (or memcache), the time to unserialize it and load all of the classes takes almost as long as creating the object in the first place.

I do not know if it is possible to store and later load a "compiled" object in PHP. Is this possible? Are there other solutions?

I am not sure that this is possible, but I thought I should ask the community.

EDIT: The object is a binary tree, and used as a decision tree. The code is basically an API that is required to quickly return an answer from the tree. This all needs to perform at an ever increasing rate so I am trying to maximize the performance wherever possible.

like image 238
jW. Avatar asked Jul 27 '09 21:07

jW.


People also ask

Can PHP files be cached?

PHP basically has two main types of caching: 'output caching' and 'parser caching'. PHP 'output caching' saves a chunk of data somewhere that can later be read by another script faster than it can generate it. 'Parser caching' is specific feature.

What is cache mechanism PHP?

In most cases the cached page version is getting stored for a specific time on the server's hard disk. If you search for “php cache” you will find on top the PHP Cache website. The PHP cache company is the host for different PHP cache libraries and adapters for APC, Redis, MemCached, FileSystem and many others.


3 Answers

You can rewrite your app to ReactPHP what create webserver in one long-running PHP process (just like Node.js or Web.py). Then you can build your big object once (on server start) as a global variable and access it from request event handlers.

like image 168
Bobík Avatar answered Oct 27 '22 00:10

Bobík


While PHP can provide a lot of dynamic features for various data types, these operations arn't magical and the data is still stored as basic native datatypes within whats called a zval which is technically a complex hashtable within the native zend api. Like any other datatype in any language, each zval will only exist for a finite period. For PHP, this period is (at max) the period of handling a HTTP request. Under any situation, to make this data last longer than a single request, it must be converted from the original zval into some other form of and then stored in some way (this includes complex types such as PHP objects as well as basic types such as ints). This will always require re-initializing each zval, and then converting the data back from the stored form back into various PHP datatypes within the zval. Some storage formats such as BSON will be faster than PHP serialized strings, but (at least as of now) this will not provide much of a noticed performance jump as it is nowhere close to the performance of maintaining the original zval across multiple requests. You will still have to serialize this data in some way, go through the time of storing it, then fetching it, and then unserializing it. There are no real solutions for this at this time.

Note that PHP can be said to have three different scopes: the SAPI, which initiates and ultimately handles all state within each request; extensions, that are initiated before each request event is started; and then script scope which is initiated by each request. All PHP vars are initiated within the script scope, but can be accessed by both extensions and the SAPI. But the only scope that can exist beyond each single request is the SAPI. In other words, a PHP object can only be maintained across multiple requests within the SAPI (an extension cannot help with this problem at this time), therefor only a custom SAPI is able to maintain zvals across requests.

like image 43
JSON Avatar answered Oct 26 '22 22:10

JSON


As far as I'm aware, it's not possible to cache objects in PHP without serializing. In general, however, caching mechanisms (APC, Memcache, etc) are really trying to remove the db connection(s) more than improve performance (and thereby decrease the overall DB strain). This is definitely how memcache, et al are employed with regards to Drupal. In other words, the caching mechanisms should allow you to scale, though they may not particularly improve performance.
Implementing a caching mechanism should allow you to more easily scale outward, even if the performance per machine is no better than before for a single connection. At a certain threshold, DB performance will degrade sharply, and the caching mechanisms should help alleviate that issue.

like image 42
William OConnor - csevb10 Avatar answered Oct 26 '22 22:10

William OConnor - csevb10