Factory::getCache replacement

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
quasiman
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Nov 25, 2009 1:58 am
Location: Oregon
Contact:

Factory::getCache replacement

Post by quasiman » Thu Dec 08, 2022 11:57 pm

I'm trying to cache API responses from outside of Joomla with Factory::getCache, but I see that it's deprecated in version 4 and to be removed in version 5, with message:
Use the cache controller factory instead
But it's not clear *how* to do that, and I can't find any examples in the documentation. Where's a good place to start looking?

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2508
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Factory::getCache replacement

Post by SharkyKZ » Fri Dec 09, 2022 6:14 am

Inside extensions you would get it the same any as any other dependency, by getting it from the container when registering your services in the services/provider.php file. The class to look for is Joomla\CMS\Cache\CacheControllerFactoryInterface. You can inject the whole factory or just the cache controller created by the factory, depending on use case. If your extension hasn't been converted to a service provider, you can get the container using Joomla\CMS\Factory::getContainer(). Other options are to manually instantiate the cache controller factory (Joomla\CMS\Cache\CacheControllerFactory) or the specific cache controller.

quasiman
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Nov 25, 2009 1:58 am
Location: Oregon
Contact:

Re: Factory::getCache replacement

Post by quasiman » Fri Dec 09, 2022 8:20 pm

Thanks, that got me reading more documentation than I expected, but learning is always a good thing.


For other learning devs, or me if I forget what I did here ;)

I have a helper class that I'm using to make API queries, and I added the cache controller factory to store the ID as an MD5 of the API path, and the cache as the data.

Code: Select all

<?php

namespace Mysite\Component\Supersite\Administrator\Helper;

\defined('_JEXEC') or die();

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Cache\CacheControllerFactoryInterface;

class Supersite
{

    public function getData($api)
    {

        $params = ComponentHelper::getComponent('com_supersite')->getParams();

        $auth_user = $params->get('auth_user');
        $auth_key = $params->get('auth_key');
		
	//Params need to be updated
        if (is_null($auth_user) || is_null($auth_key)) return ["Auth Not Set"];
        $this->add("auth-userid", $auth_user);
        $this->add("api-key", $auth_key);

        // Build the API string and compose the cache key
        $buildstring = $this->buildstring();
        $cacheKey = md5($buildstring);

        $cache = Factory::getContainer()->get(CacheControllerFactoryInterface::class)
            ->createCacheController('output', ['defaultgroup' => 'com_supersite']);

        if ($cache->contains($cacheKey)) {
            $data = $cache->get($cacheKey);
        } else {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_URL, "https://APISERVER.com/api/" . $api . "?" . $buildstring);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($ch);
            curl_close($ch);
            $cache->store($data, $cacheKey);
        }
        return $data;
    }


    private $parts = array();

    public function add($key, $value)
    {
        $this->parts[] = [
            'key' => $key,
            'value' => $value
        ];
    }

    public function buildstring($separator = '&', $equals = '=')
    {
        $queryString = [];

        foreach ($this->parts as $part) {
            $queryString[] = urlencode($part['key']) . $equals . urlencode($part['value']);
        }
        return implode($separator, $queryString);
    }
}


Post Reply

Return to “Joomla! 4.x Coding”