← 返回历史归档
·历史归档1 分钟阅读

用error_log函数写缓存

愚者
愚者·布尔软件 (Bullsoft) 创始人

恩,没错,@伪造 在看ezSQL源代码的时候,总是看到作者用error_log函数写缓存,如:

error_log( serialize($result_cache), 3, $cache_file);

来看看error_log的原型:

bool error_log ( string $message [, 
    int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

error_log() log types

0

message is sent to PHP’s system logger, using the Operating System’s system logging mechanism or a file, depending on what the error_log configuration directive is set to. This is the default option.

1

message is sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used.

2

No longer an option.

3

message is appended to the file destination . A newline is not automatically added to the end of the message string.

4

message is sent directly to the SAPI logging handler.

当$message_type 值为3时,代表写文件,而且是以append的方式写文件。同类型的函数有file_put_contents,不过它是以覆盖方式写文件。而使用这两个函数,都能节约代码,因为file_put_conents和error_log不需要提前获得文件句柄,当然也不需要关闭它们。