基于php数组生成php源代码
我有一个不可修改的函数,需要几秒钟才能完成。 函数返回一个对象数组。结果每天只改变一次 为了加快速度,我想使用APC缓存结果,但托管提供商(共享托管环境)不提供任何内存缓存解决方案(APC、memcache等) 我找到的唯一解决方案是使用serialize()将数据存储到文件中,然后再次反序列化数据 从数组中生成php源代码怎么样?后来我可以打个简单的电话基于php数组生成php源代码,php,performance,code-generation,shared-hosting,Php,Performance,Code Generation,Shared Hosting,我有一个不可修改的函数,需要几秒钟才能完成。 函数返回一个对象数组。结果每天只改变一次 为了加快速度,我想使用APC缓存结果,但托管提供商(共享托管环境)不提供任何内存缓存解决方案(APC、memcache等) 我找到的唯一解决方案是使用serialize()将数据存储到文件中,然后再次反序列化数据 从数组中生成php源代码怎么样?后来我可以打个简单的电话 require data.php 将数据放入预定义的变量中 谢谢 更新:存储生成的.html不是选项,因为输出取决于用户。您可以将答案写入
require data.php
将数据放入预定义的变量中
谢谢
更新:存储生成的.html不是选项,因为输出取决于用户。您可以将答案写入数据库,并使用函数参数作为键。您的意思是这样的吗
// File: data.php
<?php
return array(
32,
42
);
// Another file
$result = include 'data.php';
var_dump($result);
//文件:data.php
为什么不缓存生成的html页面呢?你可以很简单地做到:
// Check to see if cached file exists
// You could run a crob job to delete this at a certain time
// or have the cache file expire after a set amount of time
if(file_exists('cache.html')) {
include('cache.html');
exit;
}
ob_start(); // start capturing output buffer
// do output
$output = ob_get_contents();
$handle = fopen('cache.html', 'w');
fwrite($handle, $output);
fclose($handle);
ob_end_flush();
如果您未登录,您的提供商是否允许您的程序在运行时修改其脚本?如果我是提供程序,我会试图不允许这样做,因为这样可以防止病毒问题。@Ira:PHP脚本是ascii文件,因此它们被视为数据,而不是可执行文件。服务器无法区分您写入文件的数据(比如配置文件,或者类似于非常简单的数据库或其他东西)和您写入文件的数据,这些数据稍后将由PHP解释器解析。据我所知,没有任何限制。动态生成php文件应该可以!基于我在问题中提供的信息,完全有效且性能良好的解决方案-但输出取决于用户-我更新了问题。谢谢
// Check to see if cached file exists
// You could run a crob job to delete this at a certain time
// or have the cache file expire after a set amount of time
if(file_exists('cache.html')) {
include('cache.html');
exit;
}
ob_start(); // start capturing output buffer
// do output
$output = ob_get_contents();
$handle = fopen('cache.html', 'w');
fwrite($handle, $output);
fclose($handle);
ob_end_flush();