I am looking for more detailed information on how I can get the following caching behavior in Drupal 7.
I want a block that renders information I’m retrieving from an external service. As the block is rendered for many users I do not want to continually request data from that service, but instead cache the result. However, this data is relatively frequent to change, so I’d like to retrieve the latest data every 5 or 10 minutes, then cache it again.
Does anyone know how to achieve such caching behavior without writing too much of the code oneself? I also haven’t found much in terms of good documentation on how to use caching in Drupal (7), so any pointers on that are appreciated as well.
The functions cache_set() and cache_get() are what you are looking for. cache_set() has an expire argument.
You can use them basically like this:
Note: You can also use a different cache bin (see documentation links) but you need to create a corresponding cache table yourself as part of your schema.