缓存包含或需要的文件,以便它可以反复使用?

With PHP i'm rendering an HTML table which look somewhat like this:

--------------------------------------------------
| title    | description         | actions       |
-------------------------------------------------|
| title1   | desc1               | edit / delete |
| title2   | desc2               | edit / delete |
| title3   | desc3               | edit / delete |
| title4   | desc4               | edit / delete |
--------------------------------------------------

The code works like this (stripped down version! no thead, tbody etc. in this examle):

<table>
  <?php foreach ( $tableData as $row ): ?>
  <tr>

    <!-- Render columns (title, description)
    <?php foreach ( $columnData as $column ): ?>
    <td>
      <?= $row[$column['name']]; ?>
    </td>
    <?php endforeach; ?>

    <!-- Render actions -->
    <?php foreach ( $actions as $action ): ?>
    <td>
      <?= include($action); // include the proper action button! ?>
    </td>
    <? endforeach; ?>

  </tr>
  <?php endforeach; ?>
</table>

This gives me the desired result. But I only have one problem with this. It gets kinda slow when I have 1000+ records. I already noticed that this is because i'm doing an include for every table row. When I remove the include then everything runs very fast.

The included file contains some PHP logic. So I can't just do a file_get_contents. Well, I could, but then I have to use eval() to parse the contents. But i'd rather not use that function at all.

So now i'm wondering if it is possible to cache the included file somehow? So that PHP don't actively have to include the actual file over and over again but gets it from its cache? Is something like this possible?

Or are there any better alternatives?

You can put all actions (functions) in one file and include that file once. Just in loop you will call different functions according to their name. Variable function example.

As you are either editing or deleting when you go to that page, it seems probable that the table changes (almost...) every time you open it again so caching might not be the best option.

An alternative would be to use pagination and add a "show all" button so that the complete table only gets generated when you really need it.

It looks like there is something wrong with your action renderers.
They just cannot be that big to be placed into separate files. To render a simple hyperlink one need just a line of HTML code in the template.
If you have too much logic in these renderers, you have to move it into business logic part.

Pagination also surely helps. A real pagination I mean, when only 100 records being processed per call, not all 1000, of which 900 to be hidden.

include_once() might be what you're looking for. Here's a link: http://php.net/manual/en/function.include-once.php

One option is to go a "templating" route. Strip all the PHP from the file and replace the data parts with "tags". Then you load the file once (file_get_contents) and replace the tags with the data.

//template.html
Replace this template {variable} with {search} and {replace}.
{conditional_html}

//template.php
$tpl = file_get_contents('template.html');
$output = '';
foreach($record as $r) {
    $data = array('{variable}'=>$r['field1'], 
      '{search}'=>$r['field2'], 
      '{replace}'=>$r['field3'],
      '{contional_html}'=>($r['field4']='1A' ? 'Display Conditional' : '') };
    $output .= str_replace(array_keys($data), $data, $tpl);
}

That will eliminate file access, but you are creating your own templating markup language. Ideally your business logic is separated from your display/rendering.