BobRay / RefreshCache

Refreshes each page in the site cache by requesting it with cURL
https://bobsguides.com/refreshcache-tutorial
5 stars 1 forks source link

Configuration for limit execution #5

Open gravinos opened 3 years ago

gravinos commented 3 years ago

Hi Bob, do you think it would be possible to create a dedicated setting for the execution limitation only for the chosen templates? To avoid unnecessary executions, save time and machine resources. Thanks

BobRay commented 3 years ago

If you mean limiting the operation to templates chosen in the launch form, it would be fairly difficult. If you mean templates listed in a System Setting, it's more doable, but would mean creating a new System Setting in the install and rewriting the refreshcacheGetListProcessor.

I wouldn't have time to do that any time soon, but if it would work for you, you could rewrite the getList processor to select the templates by replacing the preparQueryBeforeCount() method with this (untested), and editing the template line (note also the addition of the template field in the field list in the first line of the method) :

public
function prepareQueryBeforeCount(xPDOQuery $c) {
  $c->select('id,pagetitle,template,uri,context_key');
  $fields = array(
    'template:IN' => array(1,3,5),
    'cacheable:=' => '1',
    'deleted:!=' => '1',
    'class_key:!=' => 'modWebLink',
    'published:!=' => '0',
    'AND:class_key:!=' => 'modSymLink',

  );
  $c->where($fields);

  return $c;
}

I recommend commenting out the original method and adding the code below it so you can revert if it doesn't work.

gravinos commented 3 years ago

Hi Bob,

the script works perfectly

public function prepareQueryBeforeCount(xPDOQuery $c) {
        $c->select('id,template,pagetitle,uri,context_key');
        $fields = array(
            'template:IN' => array(4,6),
            'cacheable:=' => '1',
            'deleted:!=' => '1',
            'class_key:!=' => 'modWebLink',
            'published:!=' => '0',
            'AND:class_key:!=' => 'modSymLink',

        );
        $c->where($fields);

        return $c;
    }

Now, how can I add the system setting already created named "refreshcache_templates_enables"?

Thanks

BobRay commented 3 years ago

I think this would do it (untested), assuming that the system setting is a plain text type, and contains a comma-separated list of Template IDs (not in quotes, no spaces).

public function prepareQueryBeforeCount(xPDOQuery $c) {
        $c->select('id,template,pagetitle,uri,context_key');
        $templates = $this->modx->getOption('refreshcache_templates_enables'); /* New */
        $templates = explode(',', $templates); /* New */
        $fields = array(
            'template:IN' =>  $templates,  /* Modified */
            'cacheable:=' => '1',
            'deleted:!=' => '1',
            'class_key:!=' => 'modWebLink',
            'published:!=' => '0',
            'AND:class_key:!=' => 'modSymLink',

        );
        $c->where($fields);

        return $c;
    }

Let me know if it works. ;)

gravinos commented 3 years ago

Hi Bob,

work prefecrtly, only a change to your code

$templates = $this-modx->getOption('refreshcache_templates_enables'); /* New */ missing ">" in $this->modx->getOption */

correct $templates = $this->modx->getOption('refreshcache_templates_enables'); /* New */

Thanks

BobRay commented 3 years ago

I'm glad that worked for you. Thanks for the idea!