Re: New method to manage generation of PURGE URLs
from
wichert
on Aug 14, 2007 03:53 AM
Ricardo Newbery wrote:
>
> On Aug 13, 2007, at 11:42 AM, Wichert Akkerman wrote:
>
>> I was working on a project today which needed a special caching setup:
>> content in http://domain/folder/* was also available as
>> http://folder.domain/*. Adding a script to each rule did not really
>> appeal to me so I went for a different approach which may also help
>> clean up the codebase a bit.
>>
>> This is what I did: I added a subscription adapter which can be asked
>> for URLs that should be purged. It has two methods:
>>
>> getRelativeUrls
>> this method returns a list of relative URLs to purge. This could
>> be used to replace all the hardcoded tests that are currently found
>> in the content rule
>>
>> getAbsoluteUrls
>> I added this because I needed it and I suspected that it can be
>> useful for others as well: it takes the list of all relative URLs
>> that are going to be purged and can return a list of absolute URLs
>> that should also be purged. These URLs will not be post-processed by
>> the cache tool
>>
>> I'm quite happy with the result: the amount of code needed to support
>> this is really tiny but it adds a lot of flexibility.
>>
>> Wichert.
>
>
> Thanks, this looks like a fantastic addition. Two questions:
>
>
> #1 - Could there be any issues with this code in legacy Plone 2.5
> installations. I haven't checked yet but is
> "zope.component.subscribers" available on all Zope versions allowed by
> the Plone 2.5 series. If not, perhaps we need to wrap this in a test.
> Maybe something like:
>
> if getattr(zope.component, 'subscribers', None) is not None:
No, I wrote this code for a Plone 2.5 site so I know it works.
> #2 - Could you write up a short paragraph or two on how to extend a
> content type to use these adapters? I would like to include it in the
> manual's chapter on customizing CacheFu.
To use this you only need to do two things: create a class with those
two methods and register that with the Zope component architecture. For
example if you have a class which exposes a 'summary' URL as well you
can use this code:
clas MyTypePurger(object):
adapts(IMyType)
implement(IPurgeUrls)
def __init__(self, context):
self.context=context
def getRelativeUrls(self):
context=self.context
portal=getToolByName(context, "portal_url").getPortalObject()
relpath=context.getPhysicalPath()[len(portal.getPhysicalPath()):]
relpath="/".join(relpath)+"/"
return [relpath+"/summary"]
def getAbsoluteUrls(self, relativeUrls):
return []
To register this with the component architecture add this to your
configure.zcml:
<subscriber
provides="Products.CacheSetup.interfaces.IPurgeUrls"
factory=".purgers.MyTypePurger"
/>
Wichert.
--
Wichert Akkerman <wichert@...> It is simple to make things.
http://www.wiggy.net/ It is hard to make things simple.