def get_data_from_cache(): "verify data in cache" widgets_with_data = [] for w in Widget.objects.all(): ck = get_data_cache_key(widget=w) data = storage.get(ck) if data: widgets_with_data.append(w) return widgets_with_data
def get_data(self, config, allow_fetch=True): """ Returns loaded (+ modified) data from storage backend. If there are no data and allow_fetch are True data will be fetched, otherwise returns None. """ cache_key = get_data_cache_key(widget=self) data = storage.get(cache_key) if data is None and allow_fetch: data = self.fetch_data() if data is not None: return self.modify_data(data, config)
def handle(self, *args, **options): """ TODO: until some clever cron or external queue is used there is simple cache lock mechanism """ from os import getpid, kill, path, remove import time from mypage.widgets.storage import storage if self.cachelock is not None: # cache lock - to ensure this command is run only once at a time l = storage.get(self.cachelock) if l is not None: return storage.set(self.cachelock, self.cachelock, self.cachelock_timeout) if self.pid_file is not None: if path.exists(self.pid_file): # kill my predecessor f = open(self.pid_file) pid = f.read() f.close() try: pid = int(pid) kill(pid, 15) time.sleep(2) kill(pid, 9) except (ValueError, OSError), e: pass # write my pid file f = open(self.pid_file, 'w') f.write(str(getpid())) f.close()