class ISettings(model.Schema): """ Cache settings """ memcache = schema.Bool(title=_(u"Memcache"), description=_(u"Invalidate Memcache cache."), required=False, default=False) if VARNISH: varnish = schema.Bool(title=_(u"Varnish"), description=_(u"Invalidate Varnish cache."), required=False, default=False) relatedItems = schema.Bool( title=_(u"Related items"), description=_(u"Also invalidate cache for context's related items."), required=False, default=False) backRefs = schema.Bool( title=_(u"Back references"), description=_(u"Also invalidate cache for context's back references."), required=False, default=False) redirectURL = schema.URI( title=_(u"Redirect URL"), description=_(u"Redirect URL when request parameter is present."), required=False)
def invalidate(self, action): """ Invalidate cache """ self.status = u"" msg_invalidated = _(u"Cache invalidated") portal_url = getToolByName(self.context, 'portal_url')() redirectURL = self.widgets['redirectURL'].value data, errors = self.extractData() if not redirectURL.startswith(portal_url): redirectURL = '' if errors: self.status = self.formErrorsMessage return content = self.getContent() changes = self.applyChanges(content, data) if redirectURL and changes: IStatusMessage(self.request).addStatusMessage(msg_invalidated, type='info') self.request.response.redirect(redirectURL) elif changes: self.status = msg_invalidated else: self.status = _(u"Nothing selected to invalidate")
def invalidate_cache(self, context, **kwargs): """ Invalidate Varnish """ if not VARNISH: return _(u"Varnish invalidated.") try: if VARNISH.purge.isPurged(context): event.notify(InvalidateVarnishEvent(context)) except Exception as err: logger.exception(err) return _(u"Varnish invalidated.")
def invalidate(self, action): """ Invalidate cache """ self.status = u"" data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return content = self.getContent() changes = self.applyChanges(content, data) if changes: self.status = _(u"Cache invalidated") else: self.status = _(u"Nothing selected to invalidate")
def __call__(self, **kwargs): if not VARNISH: return _(u"Varnish invalidated.") try: if VARNISH.purge.isPurged(self.context): event.notify(InvalidateVarnishEvent(self.context)) except Exception, err: logger.exception(err)
def backRefs(self, **kwargs): """ Invalidate back references """ getBRefs = getattr(self.context, 'getBRefs', lambda: []) for item in getBRefs(): invalidate_cache = queryMultiAdapter( (item, self.request), name='varnish.invalidate', default=lambda: None) invalidate_cache() return _(u"Varnish invalidated for back references.")
def backRefs(self, **kwargs): """ Invalidate back references """ getBRefs = getattr(self.context, 'getBRefs', lambda: []) for item in getBRefs(): uid = queryAdapter(item, IUUID) if not uid: continue event.notify(InvalidateMemCacheEvent(raw=True, dependencies=[uid])) return _(u"Memcache invalidated for back references.")
def relatedItems(self, **kwargs): """ Invalidate related Items """ getRelatedItems = getattr(self.context, 'getRelatedItems', lambda: []) for item in getRelatedItems(): invalidate_cache = queryMultiAdapter( (item, self.request), name='cache.invalidate', default=lambda: None) invalidate_cache() return _(u"Cache invalidated for relatedItems.")
def invalidate_cache(self, context, **kwargs): """ Invalidate Varnish """ if not VARNISH: return _(u"Varnish invalidated.") try: if VARNISH.purge.isPurged(context): event.notify(InvalidateVarnishEvent(context)) except Exception, err: logger.exception(err)
def __call__(self, **kwargs): # Memcache invalidate_memcache = queryMultiAdapter((self.context, self.request), name='memcache.invalidate') invalidate_memcache() # Varnish invalidate_varnish = queryMultiAdapter((self.context, self.request), name='varnish.invalidate') invalidate_varnish() return _(u"Varnish and Memcache invalidated.")
def back_refs(self, context, **kwargs): """ Invalidate back references """ getBRefs = getattr(context, 'getBRefs', lambda r: []) for item in getBRefs('relatesTo'): try: invalidate_cache = queryMultiAdapter( (item, self.request), name='cache.invalidate', default=lambda parent: None) invalidate_cache(parent="ignore") except TypeError as err: logger.exception(err) return _(u"Cache invalidated for back references.")
def back_refs(self, context, **kwargs): """ Invalidate back references """ getBRefs = getattr(context, 'getBRefs', lambda r: []) for item in getBRefs('relatesTo'): try: uid = queryAdapter(item, IUUID) if not uid: continue event.notify(InvalidateMemCacheEvent(raw=True, dependencies=[uid])) except TypeError as err: logger.exception(err) return _(u"Memcache invalidated for back references.")
def related_items(self, context, **kwargs): """ Invalidate related Items """ getRelatedItems = getattr(context, 'getRelatedItems', lambda: []) for item in getRelatedItems(): try: uid = queryAdapter(item, IUUID) if not uid: continue event.notify(InvalidateMemCacheEvent(raw=True, dependencies=[uid])) except TypeError as err: logger.exception(err) return _(u"Memcache invalidated for relatedItems.")
def related_items(self, context, **kwargs): """ Invalidate related Items """ getRelatedItems = getattr(context, 'getRelatedItems', lambda: []) for item in getRelatedItems(): try: invalidate_cache = queryMultiAdapter( (item, self.request), name='cache.invalidate', default=lambda parent: None) invalidate_cache(parent="ignore") except TypeError as err: logger.exception(err) return _(u"Cache invalidated for relatedItems.")
def invalidate_cache(self, context, **kwargs): """ Invalidate Varnish and Memcache """ # Memcache invalidate_memcache = queryMultiAdapter((context, self.request), name='memcache.invalidate') invalidate_memcache(parent="ignore") # Varnish invalidate_varnish = queryMultiAdapter((context, self.request), name='varnish.invalidate') invalidate_varnish(parent="ignore") return _(u"Varnish and Memcache invalidated.")
class SettingsForm(AutoExtensibleForm, form.EditForm): """ Cache settings """ schema = ISettings def updateWidgets(self): """ Update widgets """ super(SettingsForm, self).updateWidgets() self.widgets['redirectURL'].mode = interfaces.HIDDEN_MODE def update(self): """ Update form """ super(SettingsForm, self).update() redirect_url = self.request.get('redirect', '') if redirect_url: for name, widget in self.widgets.items(): if name != 'redirectURL': # Select everything by default widget.items[0]['checked'] = True else: # Add the redirect URL widget.value = redirect_url def applyChanges(self, content, data): """ Apply changes """ data['request'] = self.request changes = {} for name, field in self.fields.items(): # If the field is not in the data, then go on to the next one try: newValue = data[name] except KeyError: continue # If the value is NOT_CHANGED, ignore it, since the widget/converter # sent a strong message not to do so. if newValue is interfaces.NOT_CHANGED: continue if util.changedField(field.field, newValue, context=content): # Only update the data, if it is different dm = queryMultiAdapter((content, field.field), interfaces.IDataManager) # Custom behaviour. Send data instead of newValue for more # flexibility dm.set(data) # Record the change using information required later changes.setdefault(dm.field.interface, []).append(name) return changes @button.buttonAndHandler(_('Invalidate'), name='invalidate') def invalidate(self, action): """ Invalidate cache """ self.status = u"" msg_invalidated = _(u"Cache invalidated") portal_url = getToolByName(self.context, 'portal_url')() redirectURL = self.widgets['redirectURL'].value data, errors = self.extractData() if not redirectURL.startswith(portal_url): redirectURL = '' if errors: self.status = self.formErrorsMessage return content = self.getContent() changes = self.applyChanges(content, data) if redirectURL and changes: IStatusMessage(self.request).addStatusMessage(msg_invalidated, type='info') self.request.response.redirect(redirectURL) elif changes: self.status = msg_invalidated else: self.status = _(u"Nothing selected to invalidate")
def invalidate_cache(self, context, **kwargs): uid = queryAdapter(context, IUUID) if not uid: return _(u"Can't invalidate memcache. Missing uid adapter.") event.notify(InvalidateMemCacheEvent(raw=True, dependencies=[uid])) return _(u"Memcache invalidated.")
logger.exception(err) return _(u"Memcache invalidated for relatedItems.") def back_refs(self, context, **kwargs): """ Invalidate back references """ getBRefs = getattr(context, 'getBRefs', lambda r: []) for item in getBRefs('relatesTo'): try: uid = queryAdapter(item, IUUID) if not uid: continue event.notify(InvalidateMemCacheEvent(raw=True, dependencies=[uid])) except TypeError, err: logger.exception(err) return _(u"Memcache invalidated for back references.") def invalidate_cache(self, context, **kwargs): uid = queryAdapter(context, IUUID) if not uid: return _(u"Can't invalidate memcache. Missing uid adapter.") event.notify(InvalidateMemCacheEvent(raw=True, dependencies=[uid])) return _(u"Memcache invalidated.") def __call__(self, **kwargs): msg = self.invalidate_cache(self.context) # Invalidate cache for a default view parent if self.is_default_page and not kwargs.get("parent", None): msg = self.invalidate_cache(self.parent)