0

I have a file called api.php which is loaded using parameters, for example:

api.php?name=NAME&format=xml

which would return a xml page with some data, or

api.php?name=NAME&format=JSON would return the same data in json format.

The xml page is generated using this:

function generate_valid_xml_from_array($array, $node_block='xboxapi', $node_name='game') {
    $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";

    $xml .= '<' . $node_block . '>' . "\n";
    $xml .= generate_xml_from_array($array, $node_name);
    $xml .= '</' . $node_block . '>' . "\n";

    return $xml;
}
$xml = generate_valid_xml_from_array($array);
header('Content-type: text/xml');
print $xml;

and the json is returned using this:

header('Content-type: application/json');
echo json_encode($data);

How would i set the returned data to be cached for 1 hour, and then updated hourly on request?

I have been scratching my head with this one for a while now

2
  • 1
    Do you mean client- or server-side caching? Commented Oct 24, 2011 at 11:59
  • You basically want to do what was suggested below then. Or maybe consider using a caching engine like APC. Commented Oct 24, 2011 at 12:16

2 Answers 2

2

You can use a filesystem cache for your needs. Here is a link to an article describing the creation of a simple cache class:

http://devgrow.com/simple-cache-class/

Another possibility would be to use the Zend Cache classes from the Zend Framework, for more information look at the introduction chapter of the component in the Zend Framework manual:

http://framework.zend.com/manual/en/zend.cache.introduction.html

And if you Google for 'PHP cache class' you will get a lot of other possibilities you can check.

Sign up to request clarification or add additional context in comments.

Comments

0

You could store it in a file or in your database together with information about the creation time. If someone queries it you check if the creation time is more than 60 minutes in the past. If not return your cached value, if it is too old rebuild your value, save it and return it afterwards.

Comments

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.