October: [feature_request] Cachable component

Created on 23 Nov 2016  路  7Comments  路  Source: octobercms/october

Hi OctoberCMS team,

I would like to have a feature that allows me to configure the caching behavior of a partial view in a component. To support that feature, I would like to add this class into the core of OctoberCMS (if you don't mind).

Please check my code at: https://gist.github.com/khoatran/f6fadc3a70b577910fc55408d66ed8a6 for the CachableComponent class and the example of how to use it.

Cachable components/widget actually is a very popular concept in other CMS. So, it's great if OctoberCMS can support this.

Best regards,

Low Archived Review Needed Conceptual Enhancement

Most helpful comment

@Samuell1 That could work, activated with $this->cacheTTL being not null (also doubling as the expiration timing in seconds (or datetime) for the call to Cache) and a new protected method getCacheKey() which defaults to some generic namespaced key suffixed with the alias of the component.

All 7 comments

Class contents

<?php
use Cache;
use Cms\Classes\ComponentBase;
abstract class CachableComponentBase extends ComponentBase
{
    /**
     * Build cache configuration which is an array: ['cachedKey' => 'The cache key value', 'cachedTimeInMinutes' => 'Cached time in minutes']
     * @return mixed
     */
    public abstract function cacheConfig();
    public function onRender()
    {
        $cachedConfig = $this->cacheConfig();
        $key = $cachedConfig['key'];
        $minutes = $cachedConfig['cachedTimeInMinutes'];
        $result = Cache::remember($key, $minutes, function()  {
            $output = $this->renderPartial('@default');
            return $output;
        });
        return $result;
    }
}

Usage example

<?php 
use \ExampleComponent;
class ExampleComponent extends CachableComponentBase
{
    public function cacheConfig()
    {
        return ['key' => '_example_component_key',
                'cachedTimeInMinutes' => 10];
    }
}

Cool concept. We will consider this addition when we get time. For now you can certainly include this as part of your plugin structure and use it internally.

Thanks!

Why not implement this as a trait or behaviour?

Hi @LukeTowers

Trait behaviour implementation is a good idea. I haven't tried it yet for this case.

However, there's a concern if we implement this as a trait. As we need to override the method onRender of the ComponentBase, I feel it's not good if we implement it as a trait. IMHO, I would prefer using class inheritance to override methods than using trait for this case.

Cheers,

Maybe implement this in ComponentBase and can be activated with variable?

@Samuell1 That could work, activated with $this->cacheTTL being not null (also doubling as the expiration timing in seconds (or datetime) for the call to Cache) and a new protected method getCacheKey() which defaults to some generic namespaced key suffixed with the alias of the component.

See reference implementation in #4471. We will not be implementing this at present due to the varied requirements of individual components and a lack of performance testing for commonly used components to be able to decide if this is a useful addition.

Was this page helpful?
0 / 5 - 0 ratings