def home(request): """Home page.""" demos = (Submission.objects .all_sorted('upandcoming') .exclude(hidden=True))[:12] updates = [] for s in SECTION_USAGE: updates += Bundle.objects.recent_entries(s.updates)[:5] community_stats = memcache.get('community_stats') if not community_stats: community_stats = {'contributors': 5453, 'locales': 36} devderby_tag = str(constance.config .DEMOS_DEVDERBY_CURRENT_CHALLENGE_TAG).strip() context = { 'demos': demos, 'updates': updates, 'stats': community_stats, 'current_challenge_tag_name': devderby_tag, } return render(request, 'landing/homepage.html', context)
def clean_sessions(): """ Queue deleting expired session items without breaking poor MySQL """ now = timezone.now() logger = clean_sessions.get_logger() chunk_size = constance.config.SESSION_CLEANUP_CHUNK_SIZE if memcache.add(LOCK_ID, now.strftime('%c'), LOCK_EXPIRE): total_count = get_expired_sessions(now).count() delete_count = 0 logger.info('Deleting the %s of %s oldest expired sessions' % (chunk_size, total_count)) try: cursor = connection.cursor() delete_count = cursor.execute(""" DELETE FROM django_session WHERE expire_date < NOW() ORDER BY expire_date ASC LIMIT %s; """, [chunk_size]) transaction.commit_unless_managed() finally: logger.info('Deleted %s expired sessions' % delete_count) memcache.delete(LOCK_ID) expired_sessions = get_expired_sessions(now) if expired_sessions.exists(): clean_sessions.apply_async() else: logger.error('The clean_sessions task is already running since %s' % memcache.get(LOCK_ID))
def submission_listing_cache_key(*args, **kw): ns_key = memcache.get(DEMOS_CACHE_NS_KEY) if ns_key is None: ns_key = random.randint(1, 10000) memcache.set(DEMOS_CACHE_NS_KEY, ns_key) full_path = args[0].get_full_path() username = args[0].user.username return "demos_%s:%s" % (ns_key, hashlib.md5(full_path + username).hexdigest())
def submission_listing_cache_key(*args, **kw): ns_key = memcache.get(DEMOS_CACHE_NS_KEY) if ns_key is None: ns_key = random.randint(1, 10000) memcache.set(DEMOS_CACHE_NS_KEY, ns_key) full_path = args[0].get_full_path() username = args[0].user.username return 'demos_%s:%s' % (ns_key, hashlib.md5(full_path + username).hexdigest())
def _invalidate_submission_listing_helper_cache(): """Invalidate the cache for submission_listing helper used in templates""" # TODO: Does this belong in helpers.py? Better done with a model save event # subscription? ns_key = memcache.get(DEMOS_CACHE_NS_KEY) if ns_key is None: ns_key = random.randint(1, 10000) memcache.set(DEMOS_CACHE_NS_KEY, ns_key) else: memcache.incr(DEMOS_CACHE_NS_KEY)
def wrapper(*args, **kw): if type(key_fn) is str: cache_key = key_fn else: cache_key = key_fn(*args, **kw) out = memcache.get(cache_key) if out is None: context = f(*args, **kw) t = get_template(template).render(context) out = jinja2.Markup(t) memcache.set(cache_key, out, expires) return out
def bitly_shorten(url): """Attempt to shorten a given URL through bit.ly / mzl.la""" cache_key = "bitly:%s" % hashlib.md5(smart_str(url)).hexdigest() short_url = memcache.get(cache_key) if short_url is None: try: short_url = bitly.shorten(url)["url"] memcache.set(cache_key, short_url, 60 * 60 * 24 * 30 * 12) except (bitly_api.BitlyError, KeyError): # Just in case the bit.ly service fails or the API key isn't # configured, fall back to using the original URL. return url return short_url
def wrapper(*args, **kw): if type(key_fn) is str: cache_key = key_fn else: cache_key = key_fn(*args, **kw) out = memcache.get(cache_key) if out is None: context = f(*args, **kw) t = jingo.env.get_template(template).render(context) out = jinja2.Markup(t) memcache.set(cache_key, out, expires) return out
def bitly_shorten(url): """Attempt to shorten a given URL through bit.ly / mzl.la""" cache_key = 'bitly:%s' % hashlib.md5(smart_str(url)).hexdigest() short_url = memcache.get(cache_key) if short_url is None: try: short_url = bitly.shorten(url)['url'] memcache.set(cache_key, short_url, 60 * 60 * 24 * 30 * 12) except (bitly_api.BitlyError, KeyError): # Just in case the bit.ly service fails or the API key isn't # configured, fall back to using the original URL. return url return short_url
def get_url_remaps(self, locale): cache_key = URL_REMAPS_CACHE_KEY_TMPL % locale remaps = memcache.get(cache_key) if not remaps: qs = (self.filter(document__locale=locale, url_root__isnull=False).exclude(url_root='')) remaps = [{ 'original_path': '/docs/%s' % zone.document.slug, 'new_path': '/%s' % zone.url_root } for zone in qs] memcache.set(cache_key, remaps) return remaps
def home(request): """Home page.""" updates = list(Bundle.objects.recent_entries(SECTION_HACKS.updates)[:5]) community_stats = memcache.get('community_stats') if not community_stats: community_stats = {'contributors': 5453, 'locales': 36} default_filters = Filter.objects.default_filters() context = { 'updates': updates, 'stats': community_stats, 'default_filters': default_filters, } return render(request, 'landing/homepage.html', context)
def home(request): """Home page.""" demos = Submission.objects.all_sorted(sort="recentfeatured", max=12) updates = [] for s in SECTION_USAGE: updates += Bundle.objects.recent_entries(s.updates)[:5] community_stats = memcache.get("community_stats") if not community_stats: community_stats = {"contributors": 5453, "locales": 36} devderby_tag = str(config.DEMOS_DEVDERBY_CURRENT_CHALLENGE_TAG).strip() context = {"demos": demos, "updates": updates, "stats": community_stats, "current_challenge_tag_name": devderby_tag} return render(request, "landing/homepage.html", context)
def home(request): """Home page.""" updates = [] for s in SECTION_USAGE: updates += Bundle.objects.recent_entries(s.updates)[:5] community_stats = memcache.get('community_stats') if not community_stats: community_stats = {'contributors': 5453, 'locales': 36} default_filters = Filter.objects.default_filters() context = { 'updates': updates, 'stats': community_stats, 'default_filters': default_filters, } return render(request, 'landing/homepage.html', context)
def home(request): """Home page.""" updates = [] for s in SECTION_USAGE: updates += Bundle.objects.recent_entries(s.updates)[:5] community_stats = memcache.get('community_stats') if not community_stats: community_stats = {'contributors': 5453, 'locales': 36} groups = FilterGroup.objects.all() serializer = GroupWithFiltersSerializer(groups, many=True) context = { 'updates': updates, 'stats': community_stats, 'command_search_filters': serializer.data } return render(request, 'landing/homepage.html', context)
def document_last_modified(request, document_slug, document_locale): """ Utility function to derive the last modified timestamp of a document. Mainly for the @condition decorator. """ # build an adhoc natural cache key to not have to do DB query adhoc_natural_key = (document_locale, document_slug) natural_key_hash = Document.natural_key_hash(adhoc_natural_key) cache_key = DOCUMENT_LAST_MODIFIED_CACHE_KEY_TMPL % natural_key_hash try: last_mod = memcache.get(cache_key) if last_mod is None: doc = Document.objects.get(locale=document_locale, slug=document_slug) last_mod = doc.fill_last_modified_cache() # Convert the cached Unix epoch seconds back to Python datetime return datetime.fromtimestamp(float(last_mod)) except Document.DoesNotExist: return None
def home(request): """Home page.""" demos = Submission.objects.all_sorted(sort='recentfeatured', max=12) updates = [] for s in SECTION_USAGE: updates += Bundle.objects.recent_entries(s.updates)[:5] community_stats = memcache.get('community_stats') if not community_stats: community_stats = {'contributors': 5453, 'locales': 36} devderby_tag = str(config.DEMOS_DEVDERBY_CURRENT_CHALLENGE_TAG).strip() context = { 'demos': demos, 'updates': updates, 'stats': community_stats, 'current_challenge_tag_name': devderby_tag, } return render(request, 'landing/homepage.html', context)
def get_url_remaps(self, locale): cache_key = self.model.cache_key(locale) remaps = memcache.get(cache_key) if not remaps: remaps = self.reset_url_remaps(locale, cache_key) return remaps