#26463: Allowing Callbacks/Handlers to be called on Cache entry Expiration ------------------------------+------------------------ Reporter: oneTimePad | Owner: oneTimePad Type: New feature | Status: new Component: Core (Other) | Version: master Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 1 Easy pickings: 0 | UI/UX: 0 ------------------------------+------------------------ I read a suggestion about this here: [http://stackoverflow.com/questions/20866460/django-cache-backend-how-to- implement-callback-when-cache-timeout] .
I have written up an implementation of it with some tests for FileBasedCache (filebased.py) and want to add it to locmem.py too (not done yet). Here are code snippets from filebased.py that have been tested: The edits are in set and _is_expired {{{ def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None,handler=None): self._createdir() # Cache dir can be deleted at any time. fname = self._key_to_file(key, version) self._cull() # make some room if necessary fd, tmp_path = tempfile.mkstemp(dir=self._dir) renamed = False try: with io.open(fd, 'wb') as f: expiry = self.get_backend_timeout(timeout) f.write(pickle.dumps(expiry, pickle.HIGHEST_PROTOCOL)) #check if handler exists if handler: #write handler to pickle f.write(pickle.dumps(handler,pickle.HIGHEST_PROTOCOL)) f.write(zlib.compress(pickle.dumps(value, pickle.HIGHEST_PROTOCOL))) file_move_safe(tmp_path, fname, allow_overwrite=True) renamed = True finally: if not renamed: os.remove(tmp_path) def _is_expired(self, f): """ Takes an open cache file and determines if it has expired, deletes the file if it is has passed its expiry time. """ exp = pickle.load(f) if exp is not None and exp < time.time(): #if there is a handler, call it try: handler = pickle.load(f) handler(exp) except pickle.UnpicklingError: #handler not added(no handler specified), so pickling error occurs pass f.close() # On Windows a file has to be closed before deleting self._delete(f.name) return True return False }}} The handler has the following format {{{ class Handler(object): def __call__(self,exp): ... }}} -- Ticket URL: <https://code.djangoproject.com/ticket/26463> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/053.f4e20def94b5d7d2660b0c87a67ac88a%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.