Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: User-defined URIs to purge with post cache #111

Closed
raamdev opened this issue Apr 22, 2014 · 9 comments
Closed

Feature Request: User-defined URIs to purge with post cache #111

raamdev opened this issue Apr 22, 2014 · 9 comments

Comments

@raamdev
Copy link
Contributor

raamdev commented Apr 22, 2014

It would be nice if a site-owner could specify a custom list of URIs that should be purged when a post is published/modified.

This would be similar to the URI Exclusion feature, except these URIs would be cached normally and cleared the same way the Home Page and Posts Page caches can be cleared.

Such a feature would be useful in scenarios where the site has pages with custom archive views that need to be cleared when a post is published/modified.

@jaswrks
Copy link

jaswrks commented Apr 22, 2014

Awesome idea! Agree 😺

@raamdev
Copy link
Contributor Author

raamdev commented Jan 12, 2015

Temporary Workaround

If you need to clear a specific Post / Page whenever the cache is automatically cleared, you can do this by adding some code to your theme's functions.php file (or to an MU-Plugin). For more information, please see Clearing the Cache Dynamically.

@martinperreault
Copy link

I also would like to see this feature added to the plugin. Thank you!

@raamdev
Copy link
Contributor Author

raamdev commented Aug 23, 2015

Noting here that this feature (User-defined URIs to purge with post cache) would also solve the scenario described below:

@bridgeport writes (in #553 (comment))...

There's an existing option to 'Auto-Clear Designated "Posts Page"', but I think another to clear all "post" types would be useful. An example use case:

Let's say single blogs posts have a sidebar of most recent posts. If a new post is published or modified, it would shift the ordering of these sidebar items. But since ZenCache doesn't automatically purge other posts, their sidebar content would be stale. The entire cache would have to be purged, or deleted via FTP in order for these recent items to be updated.

@raamdev writes (in #553 (comment))...

a better solution to this issue would be to add support for User-Defined URIs to clear whenever a Post is cleared (see #111), and to support wildcards with that feature so that a site owner could, for example, specify /blog/* to clear all cache files for URIs under /blog/ (i.e., all Posts) whenever any Post cache is cleared.

@clavaque
Copy link

+1 for this feature. I have a custom archive page with this https://wordpress.org/plugins/clean-my-archives/ and right now need to purge it manually after publishing a new post.

I'll use the dynamic purge workaround you suggested, but think that a box for custom URIs in the Automatic Cache Clearing settings would make a lot of sense.

Thanks!

@jaswrks
Copy link

jaswrks commented Oct 11, 2015

Next Actions

  • New feature branch: feature/111 in the websharks/zencache-pro repo.

  • After this line add:

    'cache_clear_urls',
  • After this line add:

    'cache_clear_urls' => '', // Line-delimited list of URLs.
  • In this directory create a new file with the name: WcpUrlUtils.php

    <?php
    /*[pro strip-from="lite"]*/
    namespace WebSharks\ZenCache\Pro;
    
    /*
    * Automatically clears cache files for a list of custom URLs.
    *
    * @since 15xxxx Adding support for a custom list of URLs.
    *
    * @throws \Exception If a clear failure occurs.
    *
    * @return int Total files cleared by this routine (if any).
    *
    * @note Unlike many of the other `auto_` methods, this one is NOT currently
    *    attached to any hooks. However, it is called upon by other routines attached to hooks.
    */
    $self->autoClearUrlsCache = function () use ($self) {
        $counter = 0; // Initialize.
    
        if (!is_null($done = &$self->cacheKey('autoClearUrlsCache'))) {
            return $counter; // Already did this.
        }
        $done = true; // Flag as having been done.
    
        if (!$self->options['enable']) {
            return $counter; // Nothing to do.
        }
        if (!$self->options['cache_clear_urls']) {
            return $counter; // Nothing to do.
        }
        if (!is_dir($cache_dir = $self->cacheDir())) {
            return $counter; // Nothing to do.
        }
        foreach (preg_split('/['."\r\n".']+/', $self->options['cache_clear_urls'], null, PREG_SPLIT_NO_EMPTY) as $_url) {
            if (stripos($_url, 'http') === 0) {
                $_regex = $self->buildHostCachePathRegex($_url);
                $counter += $self->clearFilesFromHostCacheDir($_regex);
            }
        } unset($_url, $_regex); // Housekeeping.
    
        if ($counter && is_admin() && (!IS_PRO || $self->options['change_notifications_enable'])) {
            $self->enqueueNotice('<img src="'.esc_attr($self->url('/src/client-s/images/clear.png')).'" style="float:left; margin:0 10px 0 0; border:0;" />'.
                                  sprintf(__('<strong>%1$s:</strong> detected changes. Found %2$s in the cache matching a custom list of URLs; auto-clearing.', SLUG_TD), esc_html(NAME), esc_html($self->i18nFiles($counter))));
        }
        return $counter;
    };
    /*[/pro]*/
  • After this line add the following:

    /*[pro strip-from="lite"]*/
    $counter += $self->autoClearUrlsCache();
    /*[/pro]*/
  • After this line add the following:

    if (IS_PRO || $this->plugin->isProPreview()) {
        echo '<h4 style="margin-bottom:0;">'.__('Auto-Clear a List of Custom URLs Too?', SLUG_TD).'</h4>'."\n";
        echo '<p style="margin-top:2px;">'.sprintf(__('When you update a Post/Page, approve a Comment, or make other changes where %1$s can detect that a Post/Page cache should be cleared to keep your site up-to-date; then %1$s will also clear a list of custom URLs that you list here. <strong>Please list one URL per line.</strong>', SLUG_TD), esc_html(NAME)).'</p>'."\n";
        echo '<p><textarea name="'.esc_attr(GLOBAL_NS).'[saveOptions][cache_clear_urls]" spellcheck="false" wrap="off" rows="5">'.format_to_edit($this->plugin->options['cache_clear_urls']).'</textarea></p>'."\n";
    }
  • Submit PR.

@jaswrks jaswrks added this to the Next Release (Pro) milestone Oct 11, 2015
@jaswrks
Copy link

jaswrks commented Oct 11, 2015

@kristineds While working on this issue, you can take note of the fact that ZenCache uses a lot of PHP Closures. You can learn more about Closures here: http://php.net/manual/en/functions.anonymous.php

In the code above, $self->autoClearUrlsCache = function(){}; is a Closure.

@raamdev
Copy link
Contributor Author

raamdev commented Oct 19, 2015

Next Pro Release Changelog:

  • New Feature! It's now possible to specify a list of Custom URLs whose cache files should be cleared whenever you update a Post/Page, approve a Comment, or make other changes where ZenCache detects that a Post/Page cache should be cleared to keep your site up-to-date. See ZenCache → Plugin Options → Automatic Cache Clearing → Misc. Auto-Clear Options → Auto-Clear a List of Custom URLs Too? Props @kristineds. See Issue #111.

@raamdev raamdev closed this as completed Oct 19, 2015
@raamdev
Copy link
Contributor Author

raamdev commented Nov 14, 2015

ZenCache v151114 has been released and includes changes from this GitHub Issue. See the v151114 announcement for further details.


This issue will now be locked to further updates. If you have something to add related to this GitHub Issue, please open a new GitHub Issue and reference this one (#111).

@wpsharks wpsharks locked and limited conversation to collaborators Nov 14, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants