Concrete5(5.7)-不要在出现块错误时缓存页面或当前块 [英] Concrete5 (5.7) - Don't cache page or current block on block error

查看:109
本文介绍了Concrete5(5.7)-不要在出现块错误时缓存页面或当前块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个块依赖于相当脆弱的第三方服务来获取要渲染的数据,因此,当它确实遇到问题时,我想显示一条错误消息,而不是抛出异常而不渲染页面.

I've got a block that relies on a fairly flaky third party service to get data to render so, when it does encounter a problem, I'd like to display an error message, rather than throw an exception and not render the page.

在开始块/页面缓存之前,这很容易做.数据的寿命很长,因此,一旦找到,就可以缓存所有内容.但是,如果不是这样,则会在页面上缓存错误消息.因此,我需要告诉CMS不要将块或页面输出保存到缓存中.

Easy enough to do until you come to block/page caching. The data has a long lifetime so, when found, it's fine to cache everything. When it isn't, though, the page is cached with the error message in place. As such, I need to tell the CMS not to save the block or page output to the cache.

示例代码(在块控制器内):

Example code (within block controller):

public function view() {
    try {
        $this->set ('data', $this->getData());

    } catch (\Exception $e) {
        \Log::addError ('Blockname Error: '.$e->getMessage(), [$e]);
        $this->render ('error');
    }
}

在catch块中,我已经尝试了$this->btCacheBlockOutput = true;\Cache::disableAll();,但是都没有用.有没有办法告诉C5不要在当前请求上缓存任何内容?

Within the catch block I've tried both $this->btCacheBlockOutput = true; and \Cache::disableAll(); but neither works. Is there a way to tell C5 not to cache anything on the current request?

推荐答案

具体文件夹中的BlockController将这些受保护变量设置为标准变量:

The BlockController in the concrete folder has these protected variables set as standard :

protected $btCacheBlockRecord = true;
protected $btCacheBlockOutput = false;
protected $btCacheBlockOutputOnPost = false;
protected $btCacheBlockOutputForRegisteredUsers = false;

因此,如果您在块controller.php上将所有这些设置为false,则不应缓存您的块.

So if you set all these on your block controller.php on false, it should not cache your block.

class Controller extends BlockController
{
  protected $btCacheBlockRecord = false;
  protected $btCacheBlockOutput = false;
  protected $btCacheBlockOutputOnPost = false;
  protected $btCacheBlockOutputForRegisteredUsers = false;
  public function view(){
    .....

这将禁用块的缓存(即使第三方连接成功).

This will disable the caching of the block (even if the third party connection succeeds).

另一种解决方案是将从第三方接收的数据保存在数据库中(例如,作为json字符串),如果第三方连接失败,则从数据库加载数据...如果第三方连接成功,您可以更新数据库中的记录.

A different solution is to save the data received from the third party in the database (for example as a json string) and load the data from the database if the third party connection fails... if the third party connection succeeds, you can update the record in the database.

根据第三方服务的答案,您可以设置条件. 示例:

Depending of the answer of the third party service you can set the conditions. Example:

//service returns on OK:
//array('status' => 'ok')
//service returns on NOT OK:
//array('status' => 'nok')

public function view() {
    $data = $this->getData();
    if($data['status'] == 'ok'){
       //save data to database in a config value using concrete Config class
       $configDatabase = \Core::make('config/database');
       $configDatabase->save('thirdpartyanswer', $data);
       $this->set('data', $data);
    }else{
       //getData function returned error or nok status
       //get data from concrete Config class
       $configDatabase = \Core::make('config/database');
       if($configDatabase->get('thirdpartyanswer')) != ''){
          //set data as "old" data from config
          $this->set('data', $configDatabase->get('thirdpartyanswer'));
       }else{
          //no data in config -> set error and nothing else
          $this->set('error', 'An error occured contacting the third party');
       }
    }
}

(上面的代码尚未经过测试)
有关存储配置值的更多信息:
https://documentation.concrete5.org/developers/packages/storing-configuration-值

(The code above has not been tested)
More information on storing config values :
https://documentation.concrete5.org/developers/packages/storing-configuration-values

*编辑*

第三个选择是,如果调用失败,则清除该特定页面的缓存.

A third option is to purge the cache for that specific page if the call fails.

在您的块控制器顶部:

use \Concrete\Core\Cache\Page\PageCache;
use Page;

在"if"中,当API调用失败时:

In the 'if' when the call to the API fails :

$currentPage = Page::getCurrentPage();
$cache = PageCache::getLibrary();
$cache->purge($currentPage);

位于: https://www.concrete5.org/community/forums/5-7-discussion/programmatically-expiring-pages-from-cache

这篇关于Concrete5(5.7)-不要在出现块错误时缓存页面或当前块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆