KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Dashboard and cache package #2843

Closed GHSix opened this topic on on Oct 4, 2022 - 6 comments

GHSix commented on Oct 4, 2022

Where do I use the cache in the Dashboard?

Can I call it inside \koolreport\dashboard\Application or \koolreport\dashboard\Dashboard extending classes to get it working for all charts under it or it will need to be applyed chart by chart like in \koolreport\dashboard\widgets\d3\AreaChart?

KoolReport commented on Oct 4, 2022

Yes you can, you only need to provide a cache class to your datasource, for 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"
        ];
    }
}

Anyplace whether in App or Dashboard, when you use AutoMaker it will cache.

More information, you can view here.

GHSix commented on Oct 20, 2022

1- Where are stored the cache for FileCache?

2- Are they per user, per connection or per query?

3- I have noticed that when loading a Dashboard for the first time with some Metric widgets (Value, Trend, Category) and some other Charts (D3) widgets, even using the same query for everyone, I see the charts loading first (at once) them each Metric loading in sequency. Are they been forced to get separated from the rest in some way?

KoolReport commented on Oct 20, 2022

Hi,

For your questions:

  1. They are stored in files at sys_get_temp_dir()
  2. It is per query
  3. You may remove lazyLoading() feature of Metric, they will load all at once with other charts.

Let us know if you need further information.

GHSix commented on Oct 21, 2022

When I added cache with ttl in the AutoMaker.php, all the queries get cached, good.

But there is one it's not cool to cache, that is the login one, an AutoMaker::rawSQL call inside login function in App.php.

How can I disable caching for individual queries that are not part of a widget?

KoolReport commented on Oct 21, 2022

Let do some cool thing like this:

<?php
//AutoMaker.php

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

class AutoMaker extends MySQL
{
    protected function onCreated()
    {
        $this->props([
            "useCache"=>true,
        ]);
    }

    //Important that you provide cache object to datasource
    protected function cache()
    {
        return $this->_useCache()?
                    FileCache::create()->ttl("5mins"):
                    null;
    }

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

Now by default your AutoMaker will use cache for 5 mins, but if a query that you do not want to be cache, do this:

AutoMaker::table("abc")->select(...)->useCache(false);

AutoMaker::rawSQL("...")->params([...])->useCache(false);

Let me know if you need further assistance.

GHSix commented on Oct 24, 2022

Works like a charm.

Little change to my tate:

$this->props([
            'cacheTTL' => '1day',
        ]);

...

return $this->_cacheTTL()? FileCache::create()->ttl($this->_cacheTTL()) : null;

...

->params(':user' => filter_var($user)])->cacheTTL('')->run();

Now I can change the default TTL on the fly too.

Thank you.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

Dashboard