5-minute file cache in PHP

前端 未结 8 1632
醉梦人生
醉梦人生 2020-12-07 17:24

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 ca

8条回答
  •  渐次进展
    2020-12-07 17:51

    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.

提交回复
热议问题