5-minute file cache in PHP

2019-01-13 11:02发布

I have a very simple question: what is the best way to download a file in PHP but only if a local version has been downloaded more than 5 minute ago?

In my actual case I would like to get data from a remotely hosted csv file, for which I currently use

$file = file_get_contents($url);

without any local copy or caching. What is the simplest way to convert this into a cached version, where the end result doesn't change ($file stays the same), but it uses a local copy if it’s been fetched not so long ago (say 5 minute)?

8条回答
forever°为你锁心
2楼-- · 2019-01-13 11:20

Try phpFastCache , it support files caching, and you don't need to code your cache class. easy to use on shared hosting and VPS

Here is example:

<?php

// change files to memcached, wincache, xcache, apc, files, sqlite
$cache = phpFastCache("files");

$content = $cache->get($url);

if($content == null) {
     $content = file_get_contents($url);
     // 300 = 5 minutes 
     $cache->set($url, $content, 300);
}

// use ur $content here
echo $content;
查看更多
该账号已被封号
3楼-- · 2019-01-13 11:21

Here is a simple version which also passes a windows User-Agent string to the remote host so you don't look like a trouble-maker without proper headers.

<?php

function getCacheContent($cachefile, $remotepath, $cachetime = 120){

    // Generate the cache version if it doesn't exist or it's too old!
    if( ! file_exists($cachefile) OR (filemtime($cachefile) < (time() - $cachetime))) {

        $options = array(
            'method' => "GET",
            'header' => "Accept-language: en\r\n" .
            "User-Agent: Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)\r\n"
        );

        $context = stream_context_create(array('http' => $options));
        $contents = file_get_contents($remotepath, false, $context);

        file_put_contents($cachefile, $contents, LOCK_EX);
        return $contents;

    }

    return file_get_contents($cachefile);
}
查看更多
走好不送
4楼-- · 2019-01-13 11:22

If you are using a database system of any type, you could cache this file there. Create a table for cached information, and give it at minimum the following fields:

  • An identifier; something you can use to retrieve the file the next time you need it. Probably something like a file name.
  • A timestamp from the last time you downloaded the file from the URL.
  • Either a path to the file, where it's stored in your local file system, or use a BLOB type field to just store the contents of the file itself in the database. I would recommend just storing the path, personally. If the file was very large, you definitely wouldn't want to put it in the database.

Now, when you run the script above next time, first check in the database for the identifier, and pull the time stamp. If the difference between the current time and the stored timestamp is greater than 5 minutes pull from the URL and update the database. Otherwise, load the file from the database.

If you don't have a database setup, you could do the same thing just using files, wherein one file, or field in a file, would contain the timestamp from when you last downloaded the file.

查看更多
放我归山
5楼-- · 2019-01-13 11:24

First, you might want to check the design pattern: Lazy loading.

The implementation should change to always load the file from local cache. If the local cache is not existed or file time jitter longer than 5 minute, you fetch the file from server.

Pseudo code is like following:

$time = filetime($local_cache)
if ($time == false || (now() - $time) > 300000)
     fetch_localcache($url)  #You have to do it yourself
$file = fopen($local_cache)
查看更多
甜甜的少女心
6楼-- · 2019-01-13 11:28

You would warp it into a cache like method:

function getFile($name) {
    // code stolen from @Peter M
    if ($file exists) {
      if ($file time stamp older than 5 minutes) {
         $file = file_get_contents($url)
      }
    } else {
         $file = file_get_contents($url)
    }
    return $file;
}
查看更多
相关推荐>>
7楼-- · 2019-01-13 11:29

I think you want some (psuedo code) logic like:

if ($file exists) {
  if ($file time stamp older than 5 minutes) {
     $file = file_get_contents($url)
  }
} else {
     $file = file_get_contents($url)
}

use $file
查看更多
登录 后发表回答