我在自定义模块中创建了一个自定义缓存,我可以通过观察者对其进行读写。但是,当我尝试在块文件中获取存储的数据时,我得到一个空响应。
这是我的代码:
块/日历.php
namespace Myextension\Functionality\Block;
use Magento\Framework\View\Element\Template;
use \Magento\Framework\App\Cache;
class Calendar extends \Magento\Framework\View\Element\Template
{
protected $cache;
public function __construct(Template\Context $context,\Magento\Framework\App\Cache $cache, array $data = array())
{
parent::__construct($context, $data);
// set constructor values
$this->cache = $cache;
}
/**
* @return array
*/
public function getCountriesList()
{
$stores_cache = unserialize($this->cache->load("STORES_CACHE_TAG"));
return $stores_cache;
}
}
这是能够成功读取/写入缓存的观察者。
namespace Myextension\Functionality\Observer\Adminhtml;
class CacheRefreshType implements \Magento\Framework\Event\ObserverInterface
{
protected $scopeConfig;
protected $cache;
protected $storeManager;
/**
* CacheRefreshType constructor.
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\App\Cache $cache,
\Magento\Store\Model\StoreManagerInterface $storeManager
)
{
// set constuct variables
$this->scopeConfig = $scopeConfig;
$this->cache = $cache;
$this->storeManager = $storeManager;
}
private function refreshStoresCache() {
// get list of stores
$storeManagerDataList = $this->storeManager->getWebsites();
// init empty array of stores
$stores = [];
// for each store
foreach ($storeManagerDataList as $key => $value) {
// append store information
$stores[$key] = [
"label" => $value["name"],
"code"=>$value["code"]
];
}
// save cache
$this->cache->save(serialize($stores), "stores_cache_tag",array("STORES_CACHE_TAG"), 99999);
$stores_cache = unserialize($this->cache->load("STORES_CACHE_TAG"));
}
我真的很感谢任何帮助!