APC 缓存文件与缓存变量
假设我有一个文件 a.php,它只包含一个大数组:
$client = array(
"a" => 123,
"b" => 124,
...
);
APC 缓存文件 a.php 但这到底意味着什么?它仅缓存分配的操作码?它实际上缓存映射本身吗?或者我是否需要明确地将其放入 APC 缓存中?
谢谢!
Say I have a file a.php which only contains a large array:
$client = array(
"a" => 123,
"b" => 124,
...
);
APC caches the file a.php but what does that mean exactly? It caches the opcode for the assignment only? Does it actually cache the mapping itself? Or do I need to explicitly put that in the APC cache?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
APC 以字节码形式缓存 PHP 文件。这是人类可读代码和解释器最终执行的代码之间更快的中介。这类似于将 C/C++ 文件编译为二进制文件。以这种方式编译的数组的加载速度比从文件中读取数组的速度要快。
在 Big-O 中,访问 php 中关联数组中的元素是常数时间 O(1)。添加一个元素也是 O(1)。总的来说,最好将此类数组存储在数据库中,因为它使用更少的内存并且更灵活。请记住,访问此应用程序的每个浏览器都将拥有该数组的自己的副本,就像您使用数据库一样,只有一个副本。例如,如果你想根据它的值查找一个键,你必须迭代它,这是 O(n) (这很慢),使用数据库会快得多。
APC caches a PHP file in byte code. This is a faster intermediary between human readable code and what the interpreter ends up executing. This is analogous to compiling a C/C++ file into a binary. An array compiled in this fashion will load quicker than if you read it form file.
Accessing an element in an associative array in php is constant time O(1) in Big-O. Adding an element is also O(1). By and large its best to store such arrays in the database because it uses less memory and is much more flexible. Keep in mind that every browser that visits this application is going to have its own copy of this array, where as if you use a database there would be only one copy. For instance if you want to look up a key based on its value you'll have to iterate over it which is O(n) (which is slow), the use of a database will be much faster.