第一种方法:JSON JSON缓存变量的方式主要是使用json_encode和json_decode两个php函数。json_encode可以将变量变成文本格式,这样就可以存到文件里。 使用样例如下: // Store cache file_put_contents($cachePath, json_encode($myDataArray)); // Retrieve cache $myDataArray = json_decode(file_get_contents($cachePath)); 优势:
// Store cache file_put_contents($cachePath, serialize($myDataArray)); // Retrieve cache $myDataArray = unserialize(file_get_contents($cachePath));优势:
<?php return /*var_export的输出*/; ?>使用示例: // Store cache file_put_contents($cachePath, "<?php\nreturn " . var_export($myDataArray, true) . ";"); // Retrieve cache $myDataArray = include($cachePath);优势:
上面2个图表示100%是最佳的,var_export在编码和解码的性能上不佳。建议在数据量小的时候使用序列化的方法,如果数据量非常大,那就要考虑数据结构的问题了。 转载请保留固定链接: https://linuxeye.com/program/2105.html |