繁体   English   中英

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

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

我们在 Apache 2.2 后面运行带有 plone.app.caching 的 Plone 4.1,带有 mod_cache 和 mod_disk_cache。

plone.app.caching 可用的预定义操作不太适合此配置,因为如果 max-age=0,Apache 不会缓存响应,无论您为 Expires 和 s-max- 设置了什么值年龄(我认为这与 HTTP 1.1 规范相反)。 使用 Plone 3.3 和 CacheFu,这是一个直接的配置更改来解决这个问题:为相关的 header 集设置 max-age=1。 看到这个CacheFu 问题

我正在寻找一些建议来实现与 plone.app.caching 相同的事情。 覆盖 plone.app.caching.moderateCaching 操作以使其 maxage 设置为 1 而不是 0 的最简单方法是什么?

我们目前不考虑将 Squid 或 Varnish 添加到我们的堆栈中。

您要求的实际上是 strongCaching (但过期时间很短),因此请改用它。 根据定义,moderateCaching 旨在“缓存在浏览器中但立即过期”——换句话说,max-age=0。

三个默认缓存操作——strongCaching、moderateCaching 和weakCaching——只是有用的抽象,旨在使分配缓存策略所涉及的选择更易于理解。 如果抽象对您没有帮助,您几乎可以通过使用 strongCaching 并更改设置来完成您需要的任何事情。

这在 plone.app.caching 文档http://pypi.python.org/pypi/plone.app.caching中有更详细的讨论

这都是可配置的策略。 您可以在控制面板中设置它,或者(如我所愿)在通用设置注册表中进行设置。xml。 请参阅 plone.app.caching 中的示例策略。

更新:

控制面板试图通过限制默认缓存策略上的设置来简化事情。 不过,仍然可以直接在注册表中更改这些,因此您可以在注册表中使用以下内容。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>

劳伦斯为我找到了相关的 Apache 错误https://issues.apache.org/bugzilla/show_bug.cgi?id=352

当我们自己构建 Apache 时,我们决定打补丁 Apache 并保留 Plone,尽管补丁是针对主干的,因此应用于 2.2 代码有点棘手。

经过深思熟虑,我们通过一些 Apache 配置解决了这个问题。 我们修改了响应头,如果 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

尽管 HTTP 1.0 客户端现在不知道是否有 Expires 以作为缓存的基础,但这可以解决问题。

我想这里的真正答案可能是目前没有简单的方法可以做到这一点。 我提出了一个Plone 错误以允许使用中等缓存覆盖 max-age

所以这是我的解决方案,它有效,但与能够覆盖适度缓存的默认最大年龄相比,似乎需要做很多工作。 我定义了我自己的缓存操作,它扩展了 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  

在 configure.zcml 中注册

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

然后在我的通用设置配置文件 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>          

您还可以定义一些特定于类型的覆盖。

如果有人可以建议一种更简单的方法来实现这一点,那么请告诉我

暂无
暂无

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

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