Question

I was wondering if it is possible to store function variables in memcached. I wrote basic templating system which compiles xml templates into functions. The templates can get very big and I think I could get a performance boost if I could cache it. Would something like this work, or would I just be caching a reference to the function?

$populate_template = function($values){
   //compiled template
};
$memcached_object->set("some_key",$populated_template);

EDIT: I do realize that there are php accelerators that do exactly what I want, however it would be a lot easier to be able to use memcached because I won't have to go through the process of getting another technology approved.

Was it helpful?

Solution

I don't think so. Naturally, I looked at serializing it. But:

PHP Warning:  Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in php shell code:1

With memcached, all you can probably do is store the code as a string, then call create_function after you pull it out.

OTHER TIPS

I don't think there would be a performance boost with "serializing" the function source code as a string and recreating it via create_function. It will rather be hundred times slower than defining the function in PHP, because of the input/output with external medium + doing what would PHP need to do in either case.

You need to go with an opcode cache. But if PHP performance is the only lasting bottleneck in your application, you're a happy man.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top