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?Use the cache controller factory instead
Factory::getCache replacement
Moderators: ooffick, General Support Moderators
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
-
- Joomla! Apprentice
- Posts: 17
- Joined: Wed Nov 25, 2009 1:58 am
- Location: Oregon
- Contact:
Factory::getCache replacement
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:
-
- Joomla! Hero
- Posts: 2897
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Factory::getCache replacement
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.
-
- Joomla! Apprentice
- Posts: 17
- Joined: Wed Nov 25, 2009 1:58 am
- Location: Oregon
- Contact:
Re: Factory::getCache replacement
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.
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);
}
}