I have created a simple module to explore the custom cache in Magento 2 but my code isn't working fine. May someone please check my code below and let me know what am I doing wrong?
1) app/code/Training/CacheType/registration.php
<?php
/**
* Created by Sublime
* User: mohammad
* Date: 19/11/19
* Time: 11:34 AM
*/
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Training_CacheType',
__DIR__
);
2) app/code/Training/CacheType/etc/module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Training_CacheType">
<sequence>
<module name="Magento_Store"/>
</sequence>
</module>
</config>
3) app/code/Training/CacheType/etc/cache.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
<type name="training_cache_tag" translate="label, description" instance="Training\CacheType\Model\Cache\Type">
<label>Training Cache Type</label>
<description>Training cache description.</description>
</type>
</config>
4) app/code/Training/CacheType/Model/Cache/Type.php
<?php
/**
* Created by PhpStorm.
* User: mohammad
* Date: 19/11/19
* Time: 12:32 PM
*/
namespace Training\CacheType\Model\Cache;
class Type extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
{
const TYPE_IDENTIFIER = 'training_cache_type';
const CACHE_TAG = 'TRAINING_CACHE_TAG';
public function __construct(\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool)
{
parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
}
}
5) app/code/Training/CacheType/etc/di.xml
<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandListInterface">
<arguments>
<argument name="commands" xsi:type="array">
<item name="training_cachetype" xsi:type="object">Training\CacheType\Console\CLI\Testbed</item>
</argument>
</arguments>
</type>
</config>
6) app/code/Training/CacheType/Console/CLI/Testbed.php
<?php
/**
* Created by PhpStorm.
* User: mohammad
* Date: 20/11/19
* Time: 12:20 PM
*/
namespace Training\CacheType\Console\CLI;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Training\CacheType\Model\Cache\Manage;
class Testbed extends Command
{
const COMMAND_CACHE_TYPE = 'training:test-cache-type';
const COMMAND_DESC = 'This command has been created to test a custom cache type';
protected $cacheManage;
public function __construct(Manage $manage, string $name = null)
{
$this->cacheManage = $manage;
parent::__construct($name);
}
protected function configure()
{
$this->setName(self::COMMAND_CACHE_TYPE);
$this->setDescription(self::COMMAND_DESC);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln($this->cacheManage->setCache('Hello This is my first cache test code....'));
$output->writeln($this->cacheManage->getTest());
$output->writeln($this->cacheManage->getLoad());
}
}
7) app/code/Training/CacheType/Model/Cache/Manage.php
<?php
/**
* Created by PhpStorm.
* User: mohammad
* Date: 20/11/19
* Time: 5:53 PM
*/
namespace Training\CacheType\Model\Cache;
class Manage
{
protected $cacheType;
private $cacheKey = 'cacheType_test_id';
/**
* Manage constructor.
* @param Type $type
*/
public function __construct(\Training\CacheType\Model\Cache\Type $type)
{
$this->cacheType = $type;
}
/**
* @param $cacheData
* @return string
*/
public function setCache($cacheData) {
if($this->cacheType->save(serialize($cacheData), $this->cacheKey, [\Training\CacheType\Model\Cache\Type::CACHE_TAG], 86400)) {
return "Cache saved Done";
} else {
return "Cache save failed";
}
}
/**
* @return bool|string
*/
public function getLoad()
{
$this->cacheKey;
$loaded = $this->cacheType->load($this->cacheKey);
var_dump($loaded);
return $loaded;
}
/**
* @return bool|int
*/
public function getTest()
{
$this->cacheKey;
$testCache = $this->cacheType->test($this->cacheKey);
var_dump($testCache);
return $testCache;
}
}
8) Output on CLI:
>php bin/magento training:test-cache-type
Cache saved Done
/var/www/html/trainingm2.com/public_html/app/code/Training/CacheType/Model/Cache/Manage.php:61:
bool(false)
/var/www/html/trainingm2.com/public_html/app/code/Training/CacheType/Model/Cache/Manage.php:49:
bool(false)
False returns for test() and load(). If cachable data saved successfully through save() and returns 'Cache saved Done', then why test() and load() giving false. Please help me out. Thanks in advance