什么是在PHP中生成和打印整个div一次的好方法?

I have a complex menu where i have to generate three new menus for every menu item in the first column of the menu (something like amazon has), and the part where I generate the menu is rather simple, but because I'm dealing with a lot of data when I generate the menu, it's very slow to do it, and that menu appears on every page so it has a huge impact on the page load.

What I want to do is generate that div only once and then only print it, rather than generate it upon every refresh of the page. What is the best way to approach this?

Use a caching mechanism to cache the menu generation.

Assuming that you have a function generateMenu() and you want to cache using apc:

if (!apc_exists('menu')) {
    $menuData = generateMenu();
    apc_store('menu', $data); // cache store only once
} else {
    $menuData = apc_fetch('menu'); //loads menu from cache
}
var_dump($menuData);