This version of the Delicious website will be shutdown by April 2014.
We encourage users to switch to the new Delicious site at http://delicious.com , which features a responsive design for mobile and tablet users, offline access, faster loading, and more.

Simple caching proxy for Google Charts API - Ed Eliot

http://www.ejeliot.com/blog/121

Google recently released a fantastic API for generating dynamic charts. It's really simple and is well documented but does place a limit on the number of requests you can make per day. In most cases your chart data probably doesn't change often so it isn't really necessary to request a new chart from Google for each page view.

I've created a simple caching proxy which solves this issue. It requests charts from Google based on configuration files and caches the result. The cached image is then used for subsequent requests until it expires (configurable). Here's an example of how you might call the script:

The single parameter, "simple" refers to the filename of the configuration file you want to use (excluding path and file extension). Configuration files are simply sets of key/value pairs corresponding to the GET parameters passed to Google Charts plus a couple of special properties which define caching characteristics. Here's an example:

# simple chart

expiry = 300

cht = p3

chd = s:hW

chs = 250x100

chl = Hello|World

The configuration files can also contain PHP which will be interpreted before values are passed to Google.

# simple chart

cache = yes

expiry = 300 # time in seconds

cht = p3

chd = s:hW

chs = 250x100

chl = Hello|

It's important to note that configuration files aren't cached (at least for the moment) so any data you retrieve within them, from a database for example, should be cached separately. If you don't have code to achieve this already you can download a caching class I wrote a while ago.

Comments