在RUnit测试中,我有这样的代码片段:
checkTrue(!file.exists(fname))
doSomething()
#cat(fname);print(file.info(fname))
checkTrue(file.exists(fname))
对checkTrue()的第二次调用失败,即使doSomething()
创建了一个文件。我已经确认该文件存在,上面显示了注释掉的行。
所以,我想知道第二次调用file.exists
是否使用缓存数据?在PHP中,有一个叫做clearstatcache
的函数可以阻止这种情况的发生。有R的等价物吗?(或者,也许有人知道R从不缓存stat调用的结果?)
file.exists
不缓存stat调用的结果,因此没有等同于PHP的clearstatcache
。(旁白:这也意味着过度使用调用file.exists
,或任何stat
函数,可能会降低性能。)
从R 3.0.1开始,file.exists
使用了一个内部方法。如果您在源代码中跟踪它,您将最终在sysutils.c:
R_FileExists
。Rboolean R_FileExists(const char *path)
{
struct stat sb;
return stat(R_ExpandFileName(path), &sb) == 0;
}
(在Windows上,它使用调用_stat64,我刚刚确认也不做缓存。)