Caching

Overview #

Cache is a good way to make your application faster as the recent result will be stored temporarily in a period of time. Dashboard provides you with FileCache, MemCache and ApcCache.

Example:

<?php
//AutoMaker.php

use \koolreport\dashboard\sources\MySQL;
use \koolreport\dashboard\caching\FileCache;

class AutoMaker extends MySQL
{
    //Important that you provide cache object to datasource
    protected function cache()
    {
        return FileCache::create();
    }

    protected function connection()
    {
        return [
            "connectionString"=>"mysql:host=localhost;dbname=automaker",
            "username"=>"root",
            "password"=>"",
            "charset"=>"utf8"
        ];
    }
}

Now you can cache in any widget, you can provide time to cache

<?php
use \koolreport\dashboard\google\ColumnChart;
class PaymentChart extends ColumnChart
{
    protected function onInit()
    {
        $this->cache("5min"); //"120secs", "10min", "1hour", "1day"
    }

    protected function dataSource()
    {
        return AutoMaker::table("payments")
                ->groupBy("customerNumber")
                ->select("customerNumber")
                ->sum("amount");
    }
}

You can set the cache() from outside of widget like following:

<?php

use \koolreport\dashboard\Dashboard;

class HomeBoard extends Dashboard
{
    protected function content()
    {
        return [
            PaymentChart::create("paymentChart")->cache("5min") //"120secs", "10min", "1hour", "1day"
        ];
    }
}

FileCache #

FileCache uses file to cache result, it will save data to temporary file in system temp folder.

Nametypedescription
ttlstringGet/get the Time To Alive

Note: Those above properties follows this code rules.

Example:

use \koolreport\dashboard\caching\FileCache;
...

// Cache on widget demand
protected function cache()
{
    return FileCache::create();
}

//Cache for all queries
protected function cache()
{
    return FileCache::create()->ttl("5min");
}

MemCache #

Nametypedescription
ttlstringGet/get the Time To Alive in string
serversarrayGet/get list of servers for memcahe

Note: Those above properties follows this code rules.

Example:

use \koolreport\dashboard\caching\MemCache;
...

// Cache on widget demand
protected function cache()
{
    return MemCache::create()
        ->servers([
            "localhost"=>3434,      //"host"=>port
            "1.233.222.24"=>1212
        ]);
}

//Cache for all queries
protected function cache()
{
    return MemCache::create()
        ->ttl("5min")
        ->servers([
            "localhost"=>3434,      //"host"=>port
            "1.233.222.24"=>1212
        ]);
}

ApcCache #

Nametypedescription
ttlstringGet/get the Time To Alive in string

Note: Those above properties follows this code rules.

Example:


use \koolreport\dashboard\caching\ApcCache;
...

// Cache on widget demand
protected function cache()
{
    return ApcCache::create();
}

//Cache for all queries
protected function cache()
{
    return ApcCache::create()->ttl("5min"); //"120secs", "10min", "1hour", "1day"
}

Get started with KoolReport

KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.