简体   繁体   English

如何覆盖 plone.app.caching 操作以使用 Apache mod_cache 和 Plone

[英]How to override plone.app.caching operations for using Apache mod_cache with Plone

We're running Plone 4.1 with plone.app.caching behind Apache 2.2 with mod_cache and mod_disk_cache.我们在 Apache 2.2 后面运行带有 plone.app.caching 的 Plone 4.1,带有 mod_cache 和 mod_disk_cache。

The pre-defined operations that are available with plone.app.caching aren't quite suitable for this configuration as Apache won't cache responses if max-age=0, no matter what values you have set for Expires and s-max-age (I think this is contrary to the HTTP 1.1 specification). plone.app.caching 可用的预定义操作不太适合此配置,因为如果 max-age=0,Apache 不会缓存响应,无论您为 Expires 和 s-max- 设置了什么值年龄(我认为这与 HTTP 1.1 规范相反)。 With Plone 3.3 and CacheFu it was a straight forward configuration change to get round this: set max-age=1 for the relevant header set.使用 Plone 3.3 和 CacheFu,这是一个直接的配置更改来解决这个问题:为相关的 header 集设置 max-age=1。 See this CacheFu issue看到这个CacheFu 问题

I'm looking for some advice to achieve the same thing with plone.app.caching.我正在寻找一些建议来实现与 plone.app.caching 相同的事情。 What's the simplest way to override the plone.app.caching.moderateCaching operation such that its maxage is set to 1 rather than 0?覆盖 plone.app.caching.moderateCaching 操作以使其 maxage 设置为 1 而不是 0 的最简单方法是什么?

We're not considering adding Squid or Varnish to our stack at this moment in time.我们目前不考虑将 Squid 或 Varnish 添加到我们的堆栈中。

What you are requesting is actually strongCaching (but with a short expire) so use that instead.您要求的实际上是 strongCaching (但过期时间很短),因此请改用它。 By definition, moderateCaching is designed to "cache in browser but expire immediately" -- in other words, max-age=0.根据定义,moderateCaching 旨在“缓存在浏览器中但立即过期”——换句话说,max-age=0。

The three default caching operations -- strongCaching, moderateCaching, and weakCaching -- are just useful abstractions designed to make the choices involved in assigning caching policies easier to understand.三个默认缓存操作——strongCaching、moderateCaching 和weakCaching——只是有用的抽象,旨在使分配缓存策略所涉及的选择更易于理解。 If the abstractions don't help you, you can pretty much do mostly anything you need by just using strongCaching and changing the settings.如果抽象对您没有帮助,您几乎可以通过使用 strongCaching 并更改设置来完成您需要的任何事情。

This is discussed in more detail in the plone.app.caching docs, http://pypi.python.org/pypi/plone.app.caching这在 plone.app.caching 文档http://pypi.python.org/pypi/plone.app.caching中有更详细的讨论

That's all configurable policy.这都是可配置的策略。 You can set it in the control panel, or (as I prefer) in a generic setup registry.xml.您可以在控制面板中设置它,或者(如我所愿)在通用设置注册表中进行设置。xml。 See the example policies in plone.app.caching.请参阅 plone.app.caching 中的示例策略。

Update:更新:

The control panel attempts to simplify things by restricting what is set on the default caching policies.控制面板试图通过限制默认缓存策略上的设置来简化事情。 It's still possible to change these directly in the registry though, so you could use the following in your registry.xml:不过,仍然可以直接在注册表中更改这些,因此您可以在注册表中使用以下内容。xml:

<record name="plone.app.caching.moderateCaching.maxage">
    <field type="plone.registry.field.Int">
        <title>Maximum age</title>
        <description>Time (in seconds) to cache the response in the browser or caching proxy</description>
        <required>False</required>
    </field>
    <value>1</value>
</record>

Laurence located the relevant Apache bug for me https://issues.apache.org/bugzilla/show_bug.cgi?id=35247劳伦斯为我找到了相关的 Apache 错误https://issues.apache.org/bugzilla/show_bug.cgi?id=352

As we build Apache ourselves we've decided to patch Apache instead and leave Plone alone although the patch is for trunk and so applying to the 2.2 code is a little tricky.当我们自己构建 Apache 时,我们决定打补丁 Apache 并保留 Plone,尽管补丁是针对主干的,因此应用于 2.2 代码有点棘手。

After much deliberation we've solved this with some Apache configuration.经过深思熟虑,我们通过一些 Apache 配置解决了这个问题。 We modified the response headers to set max-age to be 1 if max-age = 0 and s-maxage is positive and then remove the expires header:我们修改了响应头,如果 max-age = 0 并且 s-maxage 为正,则将 max-age 设置为 1,然后删除 expires header:

Header edit Cache-Control max-age=0(.*s-maxage=[1-9].*) max-age=1$1
Header unset Expires

This does the trick although HTTP 1.0 clients now won't know have Expires to base their caching on.尽管 HTTP 1.0 客户端现在不知道是否有 Expires 以作为缓存的基础,但这可以解决问题。

I guess the real answer here is probably that there's no easy way to do this at the moment.我想这里的真正答案可能是目前没有简单的方法可以做到这一点。 I've raised a Plone bug to allow max-age to be overriden with moderateCaching我提出了一个Plone 错误以允许使用中等缓存覆盖 max-age

So here's my solution, that works, but seems a lot of work compared to being able to override the default max age on moderate caching.所以这是我的解决方案,它有效,但与能够覆盖适度缓存的默认最大年龄相比,似乎需要做很多工作。 I define my own caching operation, that extends plone.app.caching.operations.default.moderateCaching:我定义了我自己的缓存操作,它扩展了 plone.app.caching.operations.default.moderateCaching:

class CacheInApache(ModerateCaching):
    """ Apache won't cache a response if max-age cache control is 0.  Override ModerateCaching
        and set it to 1 second.
    """
    classProvides(ICachingOperationType)

    title = _(u"Cache in Apache")
    description = _(u"Moderate caching for Plone behind Apache. max-age set to 1")
    prefix = 'cacheInApache'

    maxage = 1  

Register this in configure.zcml在 configure.zcml 中注册

<adapter factory=".operation.CacheInApache" 
        name="cacheInApache" />
<utility component=".operation.CacheInApache" 
        name="cacheInApache" />

Then in my generic setup profile registry.xml I configure my types to use it and define its fields.然后在我的通用设置配置文件 registry.xml 中配置我的类型以使用它并定义它的字段。

<record name="plone.caching.interfaces.ICacheSettings.operationMapping">
    <value purge="False">
        <element key="plone.resource">plone.app.caching.strongCaching</element>
        <element key="plone.stableResource">plone.app.caching.strongCaching</element>
        <element key="plone.content.itemView">cacheInApache</element>
        <element key="plone.content.feed">cacheInApache</element>
        <element key="plone.content.folderView">cacheInApache</element>
        <element key="plone.content.file">cacheInApache</element>
    </value>
</record>
<record name="cacheInApache.smaxage">
    <field type="plone.registry.field.Int">
        <title>Shared maximum age</title>
        <description>Time (in seconds) to cache the response in the caching proxy</description>
        <required>False</required>
    </field>
    <value>86400</value>
</record>
<record name="cacheInApache.etags">
    <field type="plone.registry.field.Tuple">
        <title>ETags</title>
        <description>A list of ETag component names to include</description>
        <value_type type="plone.registry.field.ASCIILine" />
        <required>False</required>
    </field>
    <value>
    </value>
</record>
<record name="cacheInApache.lastModified">
    <field type="plone.registry.field.Bool">
        <title>Last-modified validation</title>
        <description>Turn on Last-Modified headers</description>
        <required>False</required>
    </field>
    <value>False</value>
</record>
<record name="cacheInApache.ramCache">
    <field type="plone.registry.field.Bool">
        <title>RAM cache</title>
        <description>Turn on caching in Zope memory</description>
        <required>False</required>
    </field>
    <value>False</value>
</record>
<record name="cacheInApache.vary">
    <field type="plone.registry.field.ASCIILine">
        <title>Vary</title>
        <description>Name(s) of HTTP headers that must match for the caching proxy to return a cached response</description>
        <required>False</required>
    </field>
    <value></value>
</record>
<record name="cacheInApache.anonOnly">
    <field type="plone.registry.field.Bool">
        <title>Only cache for anonymous users</title>
        <description>Ensure logging users always get a fresh page. Note that if you are caching pages in a proxy cache, you'll still need to use a Vary response header to keep anonymous and authenticated content separate.</description>
        <required>False</required>
    </field>
    <value>False</value>
</record>          

You can also define some type specific overrides.您还可以定义一些特定于类型的覆盖。

If anyone can suggest a simpler way to achieve this then please let me know如果有人可以建议一种更简单的方法来实现这一点,那么请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM