The Image-Charts tool is very cool but it's not always free. If you make too many requests to the Image-Charts servers, you will need to pay. To resolve that problem, you can cache your charts. Fortunately, the LegGCharts provides a useful CacheEngine to do that for you.
The cache engine will require two directories to store the cache :
- the public one, accessible from users, to store charts images ;
- the internal one, inaccessible from users, to store metadata about charts ;
You can also provide an asset prefix that will be used to generate charts URL. By default, the internal directory is directly in the library directory.
For instance:
// Here we specify only the public cache directory
$cacheEngine = new CacheEngine('public/charts');
// Here we specify the public cache and the asset prefix
$cacheEngine = new CacheEngine('web/bundles/gcharts', 'bundles/gcharts/');
// Here we specify the public cache, the asset prefix and the internal cache
$cacheEngine = new CacheEngine('web/bundles/gcharts', 'bundles/gcharts/', 'app/cache/charts');
The usage is very simple:
$cacheEngine = new CacheEngine('web/gcharts', 'gcharts/');
echo '<img src="'. $cacheEngine->build($chart) .'" />';
What's happened here ?
- The cache engine is created with
web/bundles/gcharts
as public directory CacheEngine::build(ChartInterface $chart, $keepTime = 3600)
is called :- if the chart already exists in the cache (and is fresher than $keepTime), this version is returned ;
- if not, the Image-Charts servers are requested, and the result is stored in cache ;
- We no have a local URL, pointing to your assets directory (for instance, something like
gcharts/55852c88e2.png
)
The CacheEngine has five main methods :
put(ChartInterface $chart, $keepTime)
request the chart from Image-Charts servers even if it's in cache and store it ;build(ChartInterface $chart, $keepTime)
callput
if the chart is not in the cache and return a local URL for the charthas(ChartInterface $chart)
check if the cache contains the chartclear(ChartInterface $chart)
clear the cache for a given chartclearAll()
clear the cache for all charts