0

我遇到了一个非常奇怪的错误,过去几天我一直试图解决但没有成功。我有一个用于缓存 API 调用的类,以及一个在 WordPress 插件中用于创建自定义 API 端点的类。简而言之,WordPress 插件使用外部 API 并使用我的缓存类缓存结果。使用 api 访问许多单独的项目,从而产生几十个本地缓存文件。

场景

在我的缓存类中,如果本地缓存已过期(它比实例化时设置的过期时间更早),API 会再次被命中,结果会被这样缓存:

file_put_contents($this->cache, $this->data, LOCK_EX);

在我的 WordPress 插件中,我想遍历缓存文件并删除任何 N 天未访问的文件。此方法使用 cron 命中。我正在检查访问时间(这仍在开发中,正在打印以进行调试):

print($file . ': ' . date('F jS Y - g:i:s A', fileatime(UPLOADS_DIR . '/' . $file)));

到目前为止,这是完整的方法:

public static function cleanup_old_caches($days = 30) {

    // Get the files
    $files = scandir(UPLOADS_DIR);

    // Save out .txt files
    $cache_files = array();

    // Loop through everything
    foreach ( $files as $file ) {
        if ( strstr($file, '.txt') ) {
            $cache_files[] = $file;
        }
    }

    // Loop through the cache files
    foreach ( $cache_files as $file ) {

        clearstatcache();
        print($file . ': ' . date('F jS Y - g:i:s A', fileatime(UPLOADS_DIR . '/' . $file)));
        echo "\n";
        clearstatcache();

    }

    return '';

}

你会注意到我现在有几个clearstatcache()电话。

问题

每当创建新的缓存文件时,fileatime()同一目录中许多其他文件报告的访问时间都会更新为当前时间。这些有时会在新的缓存文件之后说一秒钟。

这是我的完整方法:

private function hit_api() {

    // Set the return from the API as the data to use
    $this->data = $this->curl_get_contents($this->api_url);

    // Store the API's data for next time
    file_put_contents($this->cache, $this->data, LOCK_EX);

}

我可以找到另一种方法来编写我的清理逻辑,但我担心 PHP实际上会触及这些文件中的每一个(我已经看到一个新文件中有 18 个文件中有 12 个)。

我尝试过的事情

  • clearstatcache()绝对调用_everywhere)
  • 手动执行所有fopen(), fwrite(), fflush(),fclose()步骤
  • 写入在file_put_contents()调用点写入的文件名

如果有人知道这里发生了什么,我将不胜感激。

4

1 回答 1

1

经过一周的编写测试并通过简单的调用重新创建此问题后file_put_contents(),我找到了此问题的根源。准备好... Spotlight 正在索引这些文件。从 Spotlight 中排除,删除缓存文件,重新启动,没有问题。

在此处输入图像描述

于 2016-01-29T23:30:03.343 回答