plone.cachepurging


Nameplone.cachepurging JSON
Version 3.0.2 PyPI version JSON
download
home_pagehttps://pypi.org/project/plone.cachepurging
SummaryCache purging support for Zope 2 applications
upload_time2023-10-06 22:36:26
maintainer
docs_urlNone
authorPlone Foundation
requires_python>=3.8
licenseGPL version 2
keywords plone cache purge
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            plone.cachepurging
==================

.. contents:: Table of Contents


Introduction
------------

The ``plone.cachepurging`` package provides cache purging for Zope 2 applications.
It is inspired by (and borrows some code from) `Products.CMFSquidTool`_, but it
is not tied to Squid. In fact, it is tested mainly with `Varnish`_, though it
should also work with `Squid`_ and `Enfold Proxy`_.

This package is not tied to Plone. However, if you intend to use it with
Plone, you probably want to install `plone.app.caching`_, which provides
Plone-specific configuration and a user interface in Plone's control panel.

``plone.cachepurging`` works with Zope 2.12 and later. If you want to use it
with Zope 2.10, you may be able to do so by installing
`ZPublisherEventsBackport`_, although this is not a tested configuration.


Installation
------------

To use this package, you must do the following:

* Install it into your Zope instance. This normally means depending on it
  via ``install_requires`` in the ``setup.py`` file of your package.

* Load its configuration by adding a ZCML line like the following (or a slug)::

    <include package="plone.cachepurging" />

* Install a `plone.registry`_ ``IRegistry`` local utility and create records
  for the interface ``plone.cachepurging.interfaces.ICachePurgingSettings``.
  See the ``plone.registry`` documentation for details.

If you use ``plone.app.caching`` in Plone, it will do all of this for you.

To enable cache purging after installation, you must:

* Set up a caching proxy that supports PURGE requests, such as Varnish, Squid
  or Enfold Proxy.

* Configure the proxy and your application so that resources are cached in the
  proxy.

* Set the registry option ``plone.cachepurging.interfaces.ICachePurgingSettings.enabled``
  to ``True``. See the ``plone.registry`` documentation for details.

* Add the URL of at least one caching proxy server capable of receiving PURGE
  requests to the registry option ``plone.cachepurging.interfaces.ICachePurgingSettings.cachingProxies``.
  This should be a URL that is reachable from the Zope server. It does not
  need to be accessible from Zope's clients.

* Make your application send purge notifications - see below.

Initiating a purge in code
--------------------------

The simplest way to initiate a purge is to raise a ``Purge`` event::

    from z3c.caching.purge import Purge
    from zope.event import notify

    notify(Purge(context))

Notice how we are actually importing from ``z3c.caching`` here. That package
defines the event type and a few of the interfaces that ``plone.cachepurging``
uses. In most cases, you should be able to define how your own packages'
behave in relation to a caching proxy by depending on ``z3c.caching`` only.
This is a safer dependency, as it in turn depends only on a small set of
core Zope Toolkit packages.

Presuming ``plone.cachepurging`` is installed, firing the event above will:

* Check whether caching is enabled and configured. If not, it will do nothing.
* Look up paths to purge for the given object. This is done via zero or more
  ``IPurgePaths`` adapters. See "Which URLs get purged?" below.
* Convert the purge paths to URLs by combining them with the URLs of the
  configured caching proxies.
* Queue these for purging.

It doesn't matter if a particular object or URLs is queued more than once.
It will only be executed once.

This operation is relatively quick, and does not involve communication with
the caching proxy. At the end of the request, after the Zope transaction has
been closed (and presuming the transaction was successful - purging is by
default not performed for requests resulting in an error), the following will
take place:

* The queued URLs are retrieved from the request.
* A worker thread is established for each caching proxy, allowing asynchronous
  processing and freeing up Zope to handle the next request.
* The worker thread establishes a connection to the caching proxy and sends
  a PURGE request.
* Any errors are logged at error level to the logger ``plone.cachepurging``.

If you need more control, you can perform the purging directly. Here is a
snippet adapted from the ``plone.cachepurging.purge`` view::

        from io import StringIO

        from zope.component import getUtility

        from plone.registry.interfaces import IRegistry

        from plone.cachepurging.interfaces import IPurger
        from plone.cachepurging.interfaces import ICachePurgingSettings

        from plone.cachepurging.utils import getPathsToPurge
        from plone.cachepurging.utils import getURLsToPurge
        from plone.cachepurging.utils import isCachePurgingEnabled

        ...

        if not isCachePurgingEnabled():
            return 'Caching not enabled'

        registry = getUtility(IRegistry)
        settings = registry.forInterface(ICachePurgingSettings)

        purger = getUtility(IPurger)

        out = StringIO()

        for path in getPathsToPurge(self.context, self.request):
            for url in getURLsToPurge(path, settings.cachingProxies):
                status, xcache, xerror = purger.purgeSync(url)
                print("Purged", url, "Status", status, "X-Cache", xcache, "Error:", xerror, file=out)

        return out.getvalue()

Here, we:

* Check whether caching is enabled. This checks the ``enabled`` and
  ``cachingProxies`` properties in the registry.

* Look up the registry and cache purging settings to find the list of
  caching proxies.

* Obtain an ``IPurger`` utility. This has three main methods::

    def purgeAsync(url, httpVerb='PURGE'):
        """Send a PURGE request to a particular URL asynchronously in a
        worker thread.
        """

    def purgeSync(url, httpVerb='PURGE'):
        """Send a PURGE request to a particular URL synchronosly.

        Returns a triple ``(status, xcache, xerror)`` where ``status`` is
        the HTTP status of the purge request, ``xcache`` is the contents of
        the ``x-cache`` response header, and ``x-error`` is the contents
        of the first header found from the list of headers in
        ``errorHeaders``.
        """

    def stopThreads(wait=False):
        """Attempts to stop all threads.  Threads stop immediately after
        the current item is being processed.

        Returns True if successful, or False if threads are still running
        after waiting 5 seconds for each one.
        """

* Get all paths to purge for the current context using the helper function
  ``getPathsToPurge()``. Paths are relative to the domain root, i.e. they
  start with a '/'.

* Obtain a full PURGE URL for each caching proxy, using the helper function
  ``getURLsToPurge()``

* Send a synchronous caching request. This blocks until the caching proxy
  has responded (or timed out).


Purging an object manually
--------------------------

The code above illustrates how to initiate asynchronous and synchronous
purges. If you simply want to do this through the web, you can invoke one
of the following views, registered for any type of context:

``@@plone.cachepurging.purge``
  Performs an immediate purge of the context, using code similar to that
  shown above.
``@@plone.cachepurging.queue``
  Queues the context for purging.

Both of these views require the permission ``plone.cachepurging.InitiatePurge``,
which by default is granted to the ``Manager`` role only.


Purging objects automatically
-----------------------------

Quite commonly, you will want to purge objects in three scenarios:

* When the object is modified
* When the object is moved or renamed
* When the object is removed

These are of course all described by standard Zope event types from the
`zope.lifecycleevent`_ package. If the standard ``IObjectModifiedEvent``,
``IObjectMovedEvent`` and ``IObjectRemovedEvent`` event types are fired for
your context, you can mark it with the ``IPurgeable`` interface to
automatically purge the object.

One way to do this without changing the code of your content object is to do
this in ZCML, e.g. with::

    <class class=".content.MyContent">
        <implements interface="z3c.caching.interfaces.IPurgeable" />
    </class>

(Again notice how we are using a generic interface from ``z3c.caching``).

This is equivalent to registering an event handler for each of the events
above and doing ``notify(Purge(object))`` in each one. That is, a
``z3c.caching.interfaces.IPurgeEvent`` will be raised in a handler for the
lifecycle events, which in turn will cause purging to take place.


Purging dependencies
--------------------

Sometimes, purging one object implies that other objects should be purged
as well. One way to do this is to register an event handler for the
``IPurgeEvent`` event type, and dispatch further purge events in response. For
example, here is some code to purge the parent of the purged object::

    from zope.component import adapter
    from z3c.caching.interfaces import IPurgeEvent
    from z3c.caching.purge import Purge

    @adapter(IMyContent, IPurgeEvent)
    def purgeParent(object, IPurgeEvent):
        parent = object.__parent__
        if parent is not None:
            notify(Purge(parent))

This could be registered in ZCML like so::

    <subscriber handler=".events.purgeParent" />

If the parent is also of type ``IMyContent`` (or you replace that interface
with a more generic one), then its parent will be purged too, recursively.


Which URLs get purged?
----------------------

The ``Purge`` event handler calculates the URLs to purge for the object being
passed via named ``z3c.caching.interfaces.IPurgePaths`` adapters. Any number
of such adapters may be registered. ``plone.cachepurging`` ships with one, for
``OFS.interfaces.ITraversable`` (i.e. most objects that you can find through
the ZMI), which purges the object's ``absolute_url_path()``.

The ``IPurgePaths`` interface looks like this::

    class IPurgePaths(Interface):
        """Return paths to send as PURGE requests for a given object.

        The purging hook will look up named adapters from the objects sent to
        the purge queue (usually by an IPurgeEvent being fired) to this interface.
        The name is not significant, but is used to allow multiple implementations
        whilst still permitting per-type overrides. The names should therefore
        normally be unique, prefixed with the dotted name of the package to which
        they belong.
        """

        def getRelativePaths():
            """Return a list of paths that should be purged. The paths should be
            relative to the virtual hosting root, i.e. they should start with a
            '/'.

            These paths will be rewritten to incorporate virtual hosting if
            necessary.
            """

        def getAbsolutePaths():
            """Return a list of paths that should be purged. The paths should be
            relative to the domain root, i.e. they should start with a '/'.

            These paths will *not* be rewritten to incorporate virtual hosting.
            """

Most implementations will use ``getRelativePaths()`` to return a path relative
to the virtual hosting root (i.e. what the ``absolute_url_path()`` method
returns). This is subject to rewriting for virtual hosting (see below).

``getAbsolutePaths()`` is useful if you have a path that is not subject to
change no matter how Zope is configured. For example, you could use this if
your caching proxy supports "special" URLs to invoke a particular type of
purge. (Such behaviour can be implemented in Varnish using VCL, for example.)
This is *not* subject to rewriting for virtual hosting.

Let's say you wanted to always purge the URL ``${object_url}/view`` for any
object providing ``IContentish`` from CMF. A simple implementation may look
like this::

    from zope.interface import implementer
    from zope.component import adapts

    from z3c.caching.interfaces import IPurgePaths

    from Products.CMFCore.interfaces import IContentish

    @implementer(IPurgePaths)
    class ObjectViewPurgePaths(object):
        """Purge /view for any content object with the content object's
        default URL
        """

        adapts(IContentish)

        def __init__(self, context):
            self.context = context

        def getRelativePaths(self):
            return [self.context.absolute_url_path() + '/view']

        def getAbsolutePaths(self):
            return []

This adapter could be registered with a ZCML statement like::

    <adapter factory=".paths.ObjectViewPurgePaths" name="my.package.objectview" />

The name is not significant, but should be unique unless it is intended to
override an existing adapter. By convention, you should prefix the name with
your package's dotted name unless you have a reason not to.

The default adapter that simply returns ``absolute_url_path()`` is called
``default``.


Virtual hosting and URL rewriting
----------------------------------

Zope 2 uses "magic" URLs for virtual hosting. A common scenario is to set
the virtual host root to a Plone site object at the root of the Zope instance.
This is usually done through URL rewriting. The user sees a URL like
``http://example.com/front-page``. A web server like Apache (or a proxy like
Squid or Varnish) changes this into a URL like this::

    http://localhost:8080/VirtualHostBase/http/example.com:80/Plone/VirtualHostRoot/front-page

Here, the Zope server is running on ``http://localhost:8080``, the external
domain is ``http://example.com:80`` (the ``:80`` part is normally not shown
by web browsers, since that is the default protocol for the ``http`` URL
scheme), and the virtual hosting root is ``/Plone``.

Zope sees these tokens in the URL and understands how to incorporate the
external domain and virtual host root into the results of methods like
``absolute_url()`` and ``absolute_url_path()``, thus allowing URLs generated
in the site to show the correct external URL.

So far so good. The challenge comes when you put a caching proxy into the mix.
There are two scenarios:

1. The caching proxy is "behind" whatever performs the URL rewrite. In this
   case, the inbound URL (which the proxy may choose to cache, and which may
   therefore need to be purged) contains the virtual hosting tokens.
2. The caching proxy is "in front of" whatever performs the URL rewrite, or
   performs the rewrite before passing the request off to the Zope backend.
   In this case, the inbound URL does not contain the virtual hosting tokens.

Purging works by sending the proxy server a ``PURGE`` request with the same
path as that of a cached resource. Thus, in scenario 1, that URL needs to
contain the virtual hosting tokens. Since these are not part of any URL
generated by Zope (though they are retained in the ``PATH_INFO`` request
variable), the paths returned by ``getRelativePaths()`` of the ``IPurgePaths``
adapters need to be rewritten (in reverse, as it were) to include them.

This is done using an ``IPurgePathRewriter`` adapter on the request. The
default implementation will deal with any valid VirtualHostMonster URL,
including setups using "inside-out" hosting (with ``_vh_`` type path
segments), although you can write your own adapter if you have truly unique
needs.

If you perform URL rewriting in front of the caching proxy (scenario 1 above),
you need to configure two registry options, since there is no way for
``plone.cachepurging`` to know how the web and/or proxy cache server(s) in
front of Zope are configured:

``plone.cachepurging.interfaces.ICachePurgingSettings.virtualHosting``
    Set this to ``True`` to incorporate virtual hosting tokens in the
    PURGE paths. This is applicable in scenario 1 above.
``plone.cachepurging.interfaces.ICachePurgingSettings.domains``
    Set this to a tuple of domains `including` ports (e.g.
    ``('http://example.com:80`, 'http://www.example.com:80',)``) if your site
    is served on multiple domains. This is useful because the virtual hosting
    URL contains the "external" domain name. If your site is hosted such
    that it can be reached via multiple domains (e.g. ``http://example.com``
    vs. ``http://www.example.com``), the virtual hosting path will be
    different depending on which one the user happened to use. Most likely,
    you will want to purge *both* variants.

    Note that it is probably better to normalise your paths in the fronting
    web server, so that Zope only ever sees a single external domain. If you
    only have one domain, or if the ``virtualHosting`` option is false, you do
    not need to set this option.

.. _Products.CMFSquidTool: http://pypi.python.org/pypi/Products.CMFSquidTool
.. _Squid: http://squid-cache.org
.. _Varnish: http://varnish-cache.org
.. _Enfold Proxy: http://enfoldsystems.com/software/proxy/
.. _plone.app.caching: http://pypi.python.org/pypi/plone.app.caching
.. _ZPublisherEventsBackport: http://pypi.python.org/pypi/ZPublisherEventsBackport
.. _plone.registry: http://pypi.python.org/pypi/plone.registry
.. _zope.lifecycleevent: http://pypi.python.org/pypi/zope.lifecycleevent

Changelog
=========

.. You should *NOT* be adding new change log entries to this file.
   You should create a file in the news directory instead.
   For helpful instructions, please see:
   https://github.com/plone/plone.releaser/blob/master/ADD-A-NEWS-ITEM.rst

.. towncrier release notes start

3.0.2 (2023-10-07)
------------------

Internal:


- Update configuration files.
  [plone devs] (cfffba8c)


3.0.1 (2023-03-14)
------------------

Internal:


- Update configuration files.
  [plone devs] (359b1152)


3.0.0 (2022-11-30)
------------------

Bug fixes:


- Final release.
  [gforcada] (#600)


3.0.0a1 (2022-05-15)
--------------------

Breaking changes:


- Drop Python 2 support. Code style. 
  [jensens] (#26)


2.0.4 (2021-12-29)
------------------

Bug fixes:


- Better debugging capabilities by enhancing the output of the purge views
  `@@plone.cachepurging.purge` and `@@plone.cachepurging.queue`.
  [jensens] (#21)


2.0.3 (2021-03-02)
------------------

Bug fixes:


- Replaced deprecated Thread.isAlive by is_alive.
  The old name no longer works in Python 3.9.
  The new name already works in Python 2.7. (#22)


2.0.2 (2020-04-20)
------------------

Bug fixes:


- Minor packaging updates. (#1)


2.0.1 (2018-12-11)
------------------

Breaking changes:

- Remove five.globalrequest dependency.
  It has been deprecated upstream (Zope 4).
  [gforcada]

Bug fixes:

- Avoid a ResourceWarning: unclosed socket.
  [gforcada]

2.0 (2018-10-31)
----------------

Breaking changes:

- Use `requests <http://docs.python-requests.org/>`_ library instead of handcrafting connection and requests on our own.
  This avoids strange problems in real-world customers environments.
  We do not need to reinvent the wheel here.
  Requests always uses HTTP 1.1 and drops support for HTTP 1.0 only caches.
  [jensens]

New features:

- Try to avoid port collisions when running tests.
  [gforcada]

Bug fixes:

- Set default purger backlog size to 0 (infinity) in order to fully invalidate Varnish cache
  [avoinea refs #11]

- Tests and Code are Python 3 compatible
  [pbauer, ale-rt, jensens]


1.0.15 (2018-04-24)
-------------------

Bug fixes:

- consider purging to be enabled when it's enabled (even if no servers are listed)
  [skurfer]


1.0.14 (2018-01-30)
-------------------

Bug fixes:

- Add Python 2 / 3 compatibility
  [pbauer]


1.0.13 (2016-10-04)
-------------------

Bug fixes:

- Code-Style: isort, utf8-headers, zca-decorators, manual cleanup.
  [jensens]


1.0.12 (2016-08-08)
-------------------

New features:

- Use zope.interface decorator.
  [gforcada]


1.0.11 (2016-01-08)
-------------------

Fixes:

- Fixed typo.
  [ale-rt]


1.0.10 (2015-11-28)
-------------------

Fixes:

- Changed i18n_domain to "plone".
  [staeff]


1.0.9 (2015-07-18)
------------------

- Do not iterate on settings.cachingProxies when there are no.
  [gotcha]


1.0.8 (2015-06-09)
------------------

- correctly be able to purge empty path(root of site). Previously, /
  was always appended to url so one potential path of the resource
  in varnish would never get purged--sometimes the most important, the homepage.
  [vangheem]


1.0.7 (2014-09-11)
------------------

- Fix installation issues due to missing commas in setup.py
  [esteele]


1.0.6 (2014-09-08)
------------------

- Add undeclared dependencies
  [gforcada]


1.0.5 (2013-12-07)
------------------

- Replace deprecated test assert statements.
  [timo]


1.0.4 (2012-12-09)
------------------

- Fixed purge paths for virtual hosting scenarios using virtual path components.
  [dokai]


1.0.3 (2011-09-16)
------------------

- Only import ssl module when purging an https url, closes #12190.
  [elro]

1.0.2 (2011-08-31)
------------------

- Cast wait_time to int before calling xrange. This fixes
  "TypeError: integer argument expected, got float" error.
  [vincentfretin]


1.0.1 - 2011-05-21
------------------

- Register a `zope.testing.cleanup.addCleanUp` function to stop all purge
  threads. Also make the default purger available as a module global, so the
  cleanup function can get to it after the ZCA has been torn down.
  [hannosch]

- Register an atexit handler to stop the purge thread on process shutdown.
  [hannosch]

- Change the reconnect strategy for the purge thread to retry fewer times and
  assume a permanent connection failure after one minute and stop the thread.
  This allows the application process to shutdown cleanly without the purge
  thread being stuck forever.
  [hannosch]

- Update socket connection code for the purge thread to use Python 2.6 support
  for passing in a timeout to the create_connection call.
  [hannosch]

- Disable `purge queue is full` warning in debug mode, where it spammed the
  console.
  [hannosch]

- Correct license and update distribution metadata.
  [hannosch]


1.0 - 2011-05-13
----------------

- Release 1.0 Final.
  [esteele]

- Add MANIFEST.in.
  [WouterVH]


1.0b2 - 2011-04-06
------------------

- Fix package requirements to pull in plone.app.testing as part of the [test]
  extra.
  [esteele]


1.0b1 - 2010-12-14
-------------------

- Fix rewriting of paths in a virtual hosting environment, so that the path passed
  to the rewriter is actually used instead of always the current request path.
  [davisagli]


1.0a1 - 2010-04-22
------------------

- Initial release
  [optilude, newbery]

            

Raw data

            {
    "_id": null,
    "home_page": "https://pypi.org/project/plone.cachepurging",
    "name": "plone.cachepurging",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "plone cache purge",
    "author": "Plone Foundation",
    "author_email": "plone-developers@lists.sourceforge.net",
    "download_url": "https://files.pythonhosted.org/packages/b5/be/7fe78f064e76b8eb6efe234d3debcd80ef0bcf67f53e14fcf747a3bd66e6/plone.cachepurging-3.0.2.tar.gz",
    "platform": null,
    "description": "plone.cachepurging\n==================\n\n.. contents:: Table of Contents\n\n\nIntroduction\n------------\n\nThe ``plone.cachepurging`` package provides cache purging for Zope 2 applications.\nIt is inspired by (and borrows some code from) `Products.CMFSquidTool`_, but it\nis not tied to Squid. In fact, it is tested mainly with `Varnish`_, though it\nshould also work with `Squid`_ and `Enfold Proxy`_.\n\nThis package is not tied to Plone. However, if you intend to use it with\nPlone, you probably want to install `plone.app.caching`_, which provides\nPlone-specific configuration and a user interface in Plone's control panel.\n\n``plone.cachepurging`` works with Zope 2.12 and later. If you want to use it\nwith Zope 2.10, you may be able to do so by installing\n`ZPublisherEventsBackport`_, although this is not a tested configuration.\n\n\nInstallation\n------------\n\nTo use this package, you must do the following:\n\n* Install it into your Zope instance. This normally means depending on it\n  via ``install_requires`` in the ``setup.py`` file of your package.\n\n* Load its configuration by adding a ZCML line like the following (or a slug)::\n\n    <include package=\"plone.cachepurging\" />\n\n* Install a `plone.registry`_ ``IRegistry`` local utility and create records\n  for the interface ``plone.cachepurging.interfaces.ICachePurgingSettings``.\n  See the ``plone.registry`` documentation for details.\n\nIf you use ``plone.app.caching`` in Plone, it will do all of this for you.\n\nTo enable cache purging after installation, you must:\n\n* Set up a caching proxy that supports PURGE requests, such as Varnish, Squid\n  or Enfold Proxy.\n\n* Configure the proxy and your application so that resources are cached in the\n  proxy.\n\n* Set the registry option ``plone.cachepurging.interfaces.ICachePurgingSettings.enabled``\n  to ``True``. See the ``plone.registry`` documentation for details.\n\n* Add the URL of at least one caching proxy server capable of receiving PURGE\n  requests to the registry option ``plone.cachepurging.interfaces.ICachePurgingSettings.cachingProxies``.\n  This should be a URL that is reachable from the Zope server. It does not\n  need to be accessible from Zope's clients.\n\n* Make your application send purge notifications - see below.\n\nInitiating a purge in code\n--------------------------\n\nThe simplest way to initiate a purge is to raise a ``Purge`` event::\n\n    from z3c.caching.purge import Purge\n    from zope.event import notify\n\n    notify(Purge(context))\n\nNotice how we are actually importing from ``z3c.caching`` here. That package\ndefines the event type and a few of the interfaces that ``plone.cachepurging``\nuses. In most cases, you should be able to define how your own packages'\nbehave in relation to a caching proxy by depending on ``z3c.caching`` only.\nThis is a safer dependency, as it in turn depends only on a small set of\ncore Zope Toolkit packages.\n\nPresuming ``plone.cachepurging`` is installed, firing the event above will:\n\n* Check whether caching is enabled and configured. If not, it will do nothing.\n* Look up paths to purge for the given object. This is done via zero or more\n  ``IPurgePaths`` adapters. See \"Which URLs get purged?\" below.\n* Convert the purge paths to URLs by combining them with the URLs of the\n  configured caching proxies.\n* Queue these for purging.\n\nIt doesn't matter if a particular object or URLs is queued more than once.\nIt will only be executed once.\n\nThis operation is relatively quick, and does not involve communication with\nthe caching proxy. At the end of the request, after the Zope transaction has\nbeen closed (and presuming the transaction was successful - purging is by\ndefault not performed for requests resulting in an error), the following will\ntake place:\n\n* The queued URLs are retrieved from the request.\n* A worker thread is established for each caching proxy, allowing asynchronous\n  processing and freeing up Zope to handle the next request.\n* The worker thread establishes a connection to the caching proxy and sends\n  a PURGE request.\n* Any errors are logged at error level to the logger ``plone.cachepurging``.\n\nIf you need more control, you can perform the purging directly. Here is a\nsnippet adapted from the ``plone.cachepurging.purge`` view::\n\n        from io import StringIO\n\n        from zope.component import getUtility\n\n        from plone.registry.interfaces import IRegistry\n\n        from plone.cachepurging.interfaces import IPurger\n        from plone.cachepurging.interfaces import ICachePurgingSettings\n\n        from plone.cachepurging.utils import getPathsToPurge\n        from plone.cachepurging.utils import getURLsToPurge\n        from plone.cachepurging.utils import isCachePurgingEnabled\n\n        ...\n\n        if not isCachePurgingEnabled():\n            return 'Caching not enabled'\n\n        registry = getUtility(IRegistry)\n        settings = registry.forInterface(ICachePurgingSettings)\n\n        purger = getUtility(IPurger)\n\n        out = StringIO()\n\n        for path in getPathsToPurge(self.context, self.request):\n            for url in getURLsToPurge(path, settings.cachingProxies):\n                status, xcache, xerror = purger.purgeSync(url)\n                print(\"Purged\", url, \"Status\", status, \"X-Cache\", xcache, \"Error:\", xerror, file=out)\n\n        return out.getvalue()\n\nHere, we:\n\n* Check whether caching is enabled. This checks the ``enabled`` and\n  ``cachingProxies`` properties in the registry.\n\n* Look up the registry and cache purging settings to find the list of\n  caching proxies.\n\n* Obtain an ``IPurger`` utility. This has three main methods::\n\n    def purgeAsync(url, httpVerb='PURGE'):\n        \"\"\"Send a PURGE request to a particular URL asynchronously in a\n        worker thread.\n        \"\"\"\n\n    def purgeSync(url, httpVerb='PURGE'):\n        \"\"\"Send a PURGE request to a particular URL synchronosly.\n\n        Returns a triple ``(status, xcache, xerror)`` where ``status`` is\n        the HTTP status of the purge request, ``xcache`` is the contents of\n        the ``x-cache`` response header, and ``x-error`` is the contents\n        of the first header found from the list of headers in\n        ``errorHeaders``.\n        \"\"\"\n\n    def stopThreads(wait=False):\n        \"\"\"Attempts to stop all threads.  Threads stop immediately after\n        the current item is being processed.\n\n        Returns True if successful, or False if threads are still running\n        after waiting 5 seconds for each one.\n        \"\"\"\n\n* Get all paths to purge for the current context using the helper function\n  ``getPathsToPurge()``. Paths are relative to the domain root, i.e. they\n  start with a '/'.\n\n* Obtain a full PURGE URL for each caching proxy, using the helper function\n  ``getURLsToPurge()``\n\n* Send a synchronous caching request. This blocks until the caching proxy\n  has responded (or timed out).\n\n\nPurging an object manually\n--------------------------\n\nThe code above illustrates how to initiate asynchronous and synchronous\npurges. If you simply want to do this through the web, you can invoke one\nof the following views, registered for any type of context:\n\n``@@plone.cachepurging.purge``\n  Performs an immediate purge of the context, using code similar to that\n  shown above.\n``@@plone.cachepurging.queue``\n  Queues the context for purging.\n\nBoth of these views require the permission ``plone.cachepurging.InitiatePurge``,\nwhich by default is granted to the ``Manager`` role only.\n\n\nPurging objects automatically\n-----------------------------\n\nQuite commonly, you will want to purge objects in three scenarios:\n\n* When the object is modified\n* When the object is moved or renamed\n* When the object is removed\n\nThese are of course all described by standard Zope event types from the\n`zope.lifecycleevent`_ package. If the standard ``IObjectModifiedEvent``,\n``IObjectMovedEvent`` and ``IObjectRemovedEvent`` event types are fired for\nyour context, you can mark it with the ``IPurgeable`` interface to\nautomatically purge the object.\n\nOne way to do this without changing the code of your content object is to do\nthis in ZCML, e.g. with::\n\n    <class class=\".content.MyContent\">\n        <implements interface=\"z3c.caching.interfaces.IPurgeable\" />\n    </class>\n\n(Again notice how we are using a generic interface from ``z3c.caching``).\n\nThis is equivalent to registering an event handler for each of the events\nabove and doing ``notify(Purge(object))`` in each one. That is, a\n``z3c.caching.interfaces.IPurgeEvent`` will be raised in a handler for the\nlifecycle events, which in turn will cause purging to take place.\n\n\nPurging dependencies\n--------------------\n\nSometimes, purging one object implies that other objects should be purged\nas well. One way to do this is to register an event handler for the\n``IPurgeEvent`` event type, and dispatch further purge events in response. For\nexample, here is some code to purge the parent of the purged object::\n\n    from zope.component import adapter\n    from z3c.caching.interfaces import IPurgeEvent\n    from z3c.caching.purge import Purge\n\n    @adapter(IMyContent, IPurgeEvent)\n    def purgeParent(object, IPurgeEvent):\n        parent = object.__parent__\n        if parent is not None:\n            notify(Purge(parent))\n\nThis could be registered in ZCML like so::\n\n    <subscriber handler=\".events.purgeParent\" />\n\nIf the parent is also of type ``IMyContent`` (or you replace that interface\nwith a more generic one), then its parent will be purged too, recursively.\n\n\nWhich URLs get purged?\n----------------------\n\nThe ``Purge`` event handler calculates the URLs to purge for the object being\npassed via named ``z3c.caching.interfaces.IPurgePaths`` adapters. Any number\nof such adapters may be registered. ``plone.cachepurging`` ships with one, for\n``OFS.interfaces.ITraversable`` (i.e. most objects that you can find through\nthe ZMI), which purges the object's ``absolute_url_path()``.\n\nThe ``IPurgePaths`` interface looks like this::\n\n    class IPurgePaths(Interface):\n        \"\"\"Return paths to send as PURGE requests for a given object.\n\n        The purging hook will look up named adapters from the objects sent to\n        the purge queue (usually by an IPurgeEvent being fired) to this interface.\n        The name is not significant, but is used to allow multiple implementations\n        whilst still permitting per-type overrides. The names should therefore\n        normally be unique, prefixed with the dotted name of the package to which\n        they belong.\n        \"\"\"\n\n        def getRelativePaths():\n            \"\"\"Return a list of paths that should be purged. The paths should be\n            relative to the virtual hosting root, i.e. they should start with a\n            '/'.\n\n            These paths will be rewritten to incorporate virtual hosting if\n            necessary.\n            \"\"\"\n\n        def getAbsolutePaths():\n            \"\"\"Return a list of paths that should be purged. The paths should be\n            relative to the domain root, i.e. they should start with a '/'.\n\n            These paths will *not* be rewritten to incorporate virtual hosting.\n            \"\"\"\n\nMost implementations will use ``getRelativePaths()`` to return a path relative\nto the virtual hosting root (i.e. what the ``absolute_url_path()`` method\nreturns). This is subject to rewriting for virtual hosting (see below).\n\n``getAbsolutePaths()`` is useful if you have a path that is not subject to\nchange no matter how Zope is configured. For example, you could use this if\nyour caching proxy supports \"special\" URLs to invoke a particular type of\npurge. (Such behaviour can be implemented in Varnish using VCL, for example.)\nThis is *not* subject to rewriting for virtual hosting.\n\nLet's say you wanted to always purge the URL ``${object_url}/view`` for any\nobject providing ``IContentish`` from CMF. A simple implementation may look\nlike this::\n\n    from zope.interface import implementer\n    from zope.component import adapts\n\n    from z3c.caching.interfaces import IPurgePaths\n\n    from Products.CMFCore.interfaces import IContentish\n\n    @implementer(IPurgePaths)\n    class ObjectViewPurgePaths(object):\n        \"\"\"Purge /view for any content object with the content object's\n        default URL\n        \"\"\"\n\n        adapts(IContentish)\n\n        def __init__(self, context):\n            self.context = context\n\n        def getRelativePaths(self):\n            return [self.context.absolute_url_path() + '/view']\n\n        def getAbsolutePaths(self):\n            return []\n\nThis adapter could be registered with a ZCML statement like::\n\n    <adapter factory=\".paths.ObjectViewPurgePaths\" name=\"my.package.objectview\" />\n\nThe name is not significant, but should be unique unless it is intended to\noverride an existing adapter. By convention, you should prefix the name with\nyour package's dotted name unless you have a reason not to.\n\nThe default adapter that simply returns ``absolute_url_path()`` is called\n``default``.\n\n\nVirtual hosting and URL rewriting\n----------------------------------\n\nZope 2 uses \"magic\" URLs for virtual hosting. A common scenario is to set\nthe virtual host root to a Plone site object at the root of the Zope instance.\nThis is usually done through URL rewriting. The user sees a URL like\n``http://example.com/front-page``. A web server like Apache (or a proxy like\nSquid or Varnish) changes this into a URL like this::\n\n    http://localhost:8080/VirtualHostBase/http/example.com:80/Plone/VirtualHostRoot/front-page\n\nHere, the Zope server is running on ``http://localhost:8080``, the external\ndomain is ``http://example.com:80`` (the ``:80`` part is normally not shown\nby web browsers, since that is the default protocol for the ``http`` URL\nscheme), and the virtual hosting root is ``/Plone``.\n\nZope sees these tokens in the URL and understands how to incorporate the\nexternal domain and virtual host root into the results of methods like\n``absolute_url()`` and ``absolute_url_path()``, thus allowing URLs generated\nin the site to show the correct external URL.\n\nSo far so good. The challenge comes when you put a caching proxy into the mix.\nThere are two scenarios:\n\n1. The caching proxy is \"behind\" whatever performs the URL rewrite. In this\n   case, the inbound URL (which the proxy may choose to cache, and which may\n   therefore need to be purged) contains the virtual hosting tokens.\n2. The caching proxy is \"in front of\" whatever performs the URL rewrite, or\n   performs the rewrite before passing the request off to the Zope backend.\n   In this case, the inbound URL does not contain the virtual hosting tokens.\n\nPurging works by sending the proxy server a ``PURGE`` request with the same\npath as that of a cached resource. Thus, in scenario 1, that URL needs to\ncontain the virtual hosting tokens. Since these are not part of any URL\ngenerated by Zope (though they are retained in the ``PATH_INFO`` request\nvariable), the paths returned by ``getRelativePaths()`` of the ``IPurgePaths``\nadapters need to be rewritten (in reverse, as it were) to include them.\n\nThis is done using an ``IPurgePathRewriter`` adapter on the request. The\ndefault implementation will deal with any valid VirtualHostMonster URL,\nincluding setups using \"inside-out\" hosting (with ``_vh_`` type path\nsegments), although you can write your own adapter if you have truly unique\nneeds.\n\nIf you perform URL rewriting in front of the caching proxy (scenario 1 above),\nyou need to configure two registry options, since there is no way for\n``plone.cachepurging`` to know how the web and/or proxy cache server(s) in\nfront of Zope are configured:\n\n``plone.cachepurging.interfaces.ICachePurgingSettings.virtualHosting``\n    Set this to ``True`` to incorporate virtual hosting tokens in the\n    PURGE paths. This is applicable in scenario 1 above.\n``plone.cachepurging.interfaces.ICachePurgingSettings.domains``\n    Set this to a tuple of domains `including` ports (e.g.\n    ``('http://example.com:80`, 'http://www.example.com:80',)``) if your site\n    is served on multiple domains. This is useful because the virtual hosting\n    URL contains the \"external\" domain name. If your site is hosted such\n    that it can be reached via multiple domains (e.g. ``http://example.com``\n    vs. ``http://www.example.com``), the virtual hosting path will be\n    different depending on which one the user happened to use. Most likely,\n    you will want to purge *both* variants.\n\n    Note that it is probably better to normalise your paths in the fronting\n    web server, so that Zope only ever sees a single external domain. If you\n    only have one domain, or if the ``virtualHosting`` option is false, you do\n    not need to set this option.\n\n.. _Products.CMFSquidTool: http://pypi.python.org/pypi/Products.CMFSquidTool\n.. _Squid: http://squid-cache.org\n.. _Varnish: http://varnish-cache.org\n.. _Enfold Proxy: http://enfoldsystems.com/software/proxy/\n.. _plone.app.caching: http://pypi.python.org/pypi/plone.app.caching\n.. _ZPublisherEventsBackport: http://pypi.python.org/pypi/ZPublisherEventsBackport\n.. _plone.registry: http://pypi.python.org/pypi/plone.registry\n.. _zope.lifecycleevent: http://pypi.python.org/pypi/zope.lifecycleevent\n\nChangelog\n=========\n\n.. You should *NOT* be adding new change log entries to this file.\n   You should create a file in the news directory instead.\n   For helpful instructions, please see:\n   https://github.com/plone/plone.releaser/blob/master/ADD-A-NEWS-ITEM.rst\n\n.. towncrier release notes start\n\n3.0.2 (2023-10-07)\n------------------\n\nInternal:\n\n\n- Update configuration files.\n  [plone devs] (cfffba8c)\n\n\n3.0.1 (2023-03-14)\n------------------\n\nInternal:\n\n\n- Update configuration files.\n  [plone devs] (359b1152)\n\n\n3.0.0 (2022-11-30)\n------------------\n\nBug fixes:\n\n\n- Final release.\n  [gforcada] (#600)\n\n\n3.0.0a1 (2022-05-15)\n--------------------\n\nBreaking changes:\n\n\n- Drop Python 2 support. Code style. \n  [jensens] (#26)\n\n\n2.0.4 (2021-12-29)\n------------------\n\nBug fixes:\n\n\n- Better debugging capabilities by enhancing the output of the purge views\n  `@@plone.cachepurging.purge` and `@@plone.cachepurging.queue`.\n  [jensens] (#21)\n\n\n2.0.3 (2021-03-02)\n------------------\n\nBug fixes:\n\n\n- Replaced deprecated Thread.isAlive by is_alive.\n  The old name no longer works in Python 3.9.\n  The new name already works in Python 2.7. (#22)\n\n\n2.0.2 (2020-04-20)\n------------------\n\nBug fixes:\n\n\n- Minor packaging updates. (#1)\n\n\n2.0.1 (2018-12-11)\n------------------\n\nBreaking changes:\n\n- Remove five.globalrequest dependency.\n  It has been deprecated upstream (Zope 4).\n  [gforcada]\n\nBug fixes:\n\n- Avoid a ResourceWarning: unclosed socket.\n  [gforcada]\n\n2.0 (2018-10-31)\n----------------\n\nBreaking changes:\n\n- Use `requests <http://docs.python-requests.org/>`_ library instead of handcrafting connection and requests on our own.\n  This avoids strange problems in real-world customers environments.\n  We do not need to reinvent the wheel here.\n  Requests always uses HTTP 1.1 and drops support for HTTP 1.0 only caches.\n  [jensens]\n\nNew features:\n\n- Try to avoid port collisions when running tests.\n  [gforcada]\n\nBug fixes:\n\n- Set default purger backlog size to 0 (infinity) in order to fully invalidate Varnish cache\n  [avoinea refs #11]\n\n- Tests and Code are Python 3 compatible\n  [pbauer, ale-rt, jensens]\n\n\n1.0.15 (2018-04-24)\n-------------------\n\nBug fixes:\n\n- consider purging to be enabled when it's enabled (even if no servers are listed)\n  [skurfer]\n\n\n1.0.14 (2018-01-30)\n-------------------\n\nBug fixes:\n\n- Add Python 2 / 3 compatibility\n  [pbauer]\n\n\n1.0.13 (2016-10-04)\n-------------------\n\nBug fixes:\n\n- Code-Style: isort, utf8-headers, zca-decorators, manual cleanup.\n  [jensens]\n\n\n1.0.12 (2016-08-08)\n-------------------\n\nNew features:\n\n- Use zope.interface decorator.\n  [gforcada]\n\n\n1.0.11 (2016-01-08)\n-------------------\n\nFixes:\n\n- Fixed typo.\n  [ale-rt]\n\n\n1.0.10 (2015-11-28)\n-------------------\n\nFixes:\n\n- Changed i18n_domain to \"plone\".\n  [staeff]\n\n\n1.0.9 (2015-07-18)\n------------------\n\n- Do not iterate on settings.cachingProxies when there are no.\n  [gotcha]\n\n\n1.0.8 (2015-06-09)\n------------------\n\n- correctly be able to purge empty path(root of site). Previously, /\n  was always appended to url so one potential path of the resource\n  in varnish would never get purged--sometimes the most important, the homepage.\n  [vangheem]\n\n\n1.0.7 (2014-09-11)\n------------------\n\n- Fix installation issues due to missing commas in setup.py\n  [esteele]\n\n\n1.0.6 (2014-09-08)\n------------------\n\n- Add undeclared dependencies\n  [gforcada]\n\n\n1.0.5 (2013-12-07)\n------------------\n\n- Replace deprecated test assert statements.\n  [timo]\n\n\n1.0.4 (2012-12-09)\n------------------\n\n- Fixed purge paths for virtual hosting scenarios using virtual path components.\n  [dokai]\n\n\n1.0.3 (2011-09-16)\n------------------\n\n- Only import ssl module when purging an https url, closes #12190.\n  [elro]\n\n1.0.2 (2011-08-31)\n------------------\n\n- Cast wait_time to int before calling xrange. This fixes\n  \"TypeError: integer argument expected, got float\" error.\n  [vincentfretin]\n\n\n1.0.1 - 2011-05-21\n------------------\n\n- Register a `zope.testing.cleanup.addCleanUp` function to stop all purge\n  threads. Also make the default purger available as a module global, so the\n  cleanup function can get to it after the ZCA has been torn down.\n  [hannosch]\n\n- Register an atexit handler to stop the purge thread on process shutdown.\n  [hannosch]\n\n- Change the reconnect strategy for the purge thread to retry fewer times and\n  assume a permanent connection failure after one minute and stop the thread.\n  This allows the application process to shutdown cleanly without the purge\n  thread being stuck forever.\n  [hannosch]\n\n- Update socket connection code for the purge thread to use Python 2.6 support\n  for passing in a timeout to the create_connection call.\n  [hannosch]\n\n- Disable `purge queue is full` warning in debug mode, where it spammed the\n  console.\n  [hannosch]\n\n- Correct license and update distribution metadata.\n  [hannosch]\n\n\n1.0 - 2011-05-13\n----------------\n\n- Release 1.0 Final.\n  [esteele]\n\n- Add MANIFEST.in.\n  [WouterVH]\n\n\n1.0b2 - 2011-04-06\n------------------\n\n- Fix package requirements to pull in plone.app.testing as part of the [test]\n  extra.\n  [esteele]\n\n\n1.0b1 - 2010-12-14\n-------------------\n\n- Fix rewriting of paths in a virtual hosting environment, so that the path passed\n  to the rewriter is actually used instead of always the current request path.\n  [davisagli]\n\n\n1.0a1 - 2010-04-22\n------------------\n\n- Initial release\n  [optilude, newbery]\n",
    "bugtrack_url": null,
    "license": "GPL version 2",
    "summary": "Cache purging support for Zope 2 applications",
    "version": "3.0.2",
    "project_urls": {
        "Homepage": "https://pypi.org/project/plone.cachepurging"
    },
    "split_keywords": [
        "plone",
        "cache",
        "purge"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2494a454d4eb50a8dfc99a6026204c47e65624222808c9597b361f355816a114",
                "md5": "5ff41fb3697404e343eca0e530befe3e",
                "sha256": "ae27366d1170fa71d676fecfa790c99bed34f308cb2b047b356bea2a70387e42"
            },
            "downloads": -1,
            "filename": "plone.cachepurging-3.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5ff41fb3697404e343eca0e530befe3e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 28087,
            "upload_time": "2023-10-06T22:36:24",
            "upload_time_iso_8601": "2023-10-06T22:36:24.046393Z",
            "url": "https://files.pythonhosted.org/packages/24/94/a454d4eb50a8dfc99a6026204c47e65624222808c9597b361f355816a114/plone.cachepurging-3.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b5be7fe78f064e76b8eb6efe234d3debcd80ef0bcf67f53e14fcf747a3bd66e6",
                "md5": "a20a3ca8716f42c8cf29d9156fc9d2e7",
                "sha256": "9f770e2861a57f9a70af1699bef6a970d15e99ba5293034b93bb0bdb5f783782"
            },
            "downloads": -1,
            "filename": "plone.cachepurging-3.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "a20a3ca8716f42c8cf29d9156fc9d2e7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 38892,
            "upload_time": "2023-10-06T22:36:26",
            "upload_time_iso_8601": "2023-10-06T22:36:26.028069Z",
            "url": "https://files.pythonhosted.org/packages/b5/be/7fe78f064e76b8eb6efe234d3debcd80ef0bcf67f53e14fcf747a3bd66e6/plone.cachepurging-3.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-06 22:36:26",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "plone.cachepurging"
}
        
Elapsed time: 0.11922s