开发者

How to cache code in PHP?

I am creating a custom form building system, which includes various tokens. These tokens are found using Regular Expressions, and depending on the type of toke, parsed. Some require simple replacement, some require cycles, and so forth.

Now I know, that RegExp is quite resource and time consuming, so I would like to be able to parse the code for the form once, creating a php code, and then save the PHP code, for next uses. How would I go about doing this?

So far I have only seen output caching. Is there a way to cache commands like echo and cycles like foreach()?


Because of misunderstandings, I'll create an example.

Unparsed template data:

Thank You for Your interest, [*Title*] [*Firstname*] [*Lastname*]. Here are the details of Your order!
[*KeyValuePairs*]
Here is the link to Your request: [*LinkToRequest*].

Parsed template:

"Thank You for Your interest, <?php echo $data->title;?> <?php echo $data->firstname;?> <?p开发者_Go百科hp echo $data->lastname;?>. Here are the details of Your order!
 <?php foreach($data->values as $key=>$value){
echo $key."-".$value
}?>
  Here is the link to Your request: <?php echo $data->linkToRequest;?>.

I would then save the parsed template, and instead of parsing the template every time, just pass the $data variable to the already parsed one, which would generate an output.


You simply generate the included file, you save it in a non-publicly accessible folder, and you include inside a PHP function using include($filename);

A code example:

    function render( $___template, $___data_array = array() )
    {
            ob_start();
            extract( $___data_array );
            include ( $___template);
            $output = ob_get_clean();
            echo $output;
    }   
    $data = array('Title' => 'My title', 'FirstName' => 'John');
    render('templates/mytemplate.php', $data);

Note the key point is using extract ( http://php.net/extract ) to expand the array contents in real vars.

(inside the scope of the function $___data['FirstName'] becomes $FirstName)

UPDATE: this is, roughly, the method used by Wordpress, CodeIgniter and other frameworks to load their PHP based templates.


I'm not sure if understood your problem, but did you try using APC?

With APC you could cache variables so if you echo a specific variable, you could get it from cache.

You do all your calculations, save the information in some variables, and save those variables in the cache. Then, next time you just fetch that information from cache.

It's really easy to use APC. You just have to call apc_fetch($key) to fetch, and apc_store($key, $value, $howLongYouWant2Cache) to save it.


You best bet would to simply generate a PHP file and save it. I.e.,

$replacement = 'foobar';

$phpCodeTemplate = "<?php echo '$replacement'; ?>";

file_put_contents('some_unique_file_name.php', $phpCodeTemplate);

Just be very careful when dynamically generating PHP files, as you don't want to allow users to manipulate data to include anything malicious.

Then, in your process, simply check if the file exists, is so, run it, otherwise, generate the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜