4

I am using the PHP HTML DOM Parser to pull data from an external website. To reduce load and speed up page rendering time I want to cache data I pull for a certain period. How can I do this?

3 Answers 3

7

I wrote this file cache function which basically just replaces file_get_contents. You can specify the amount of time the cache should last for in $offset or completely override the cache with $override. If you don't want to use /tmp/, just change that directory to something you can read/write to.

function cache_get_contents($url, $offset = 600, $override = false) {
    $file = '/tmp/file_cache_' . md5($url);
    if (!$override && file_exists($file) && filemtime($file) > time() - $offset)
        return file_get_contents($file);

    $contents = file_get_contents($url);
    if ($contents === false)
        return false;

    file_put_contents($file, $contents);
    return $contents;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Right now I am using file_get_html which returns a DOM object. Right now yours returns a string
Use str_get_html() on the string.
Are you sure you have write permissions for /tmp? Is file_get_contents() failing? You can try to debug the value of $contents after the 6th line of the above code.
1

You could create local files with the HTML and then keep track of the file paths in the $SESSION. If you have the disk space and can run a database, you could use a database to do the same thing. A database connection and query on the URL you're looking for won't add much overhead at all.

Comments

1

One way would be to save the data to a database or local file. You could then use a timestamp column or file modification time to determine whether to continue using the cache or pull and save a fresh copy.

If you have access to some kind of memory caching (e.g. memcached) that would be ideal.

2 Comments

memcached is a good idea unless you have to store large amounts of data. It sounds like a file cache is more ideal in this situation.
@dtbame, fair enough. The OP didn't specify the amount of data. So I offered it as a secondary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.