Skip to content

Instantly share code, notes, and snippets.

@smacker
Created June 19, 2012 09:47
Show Gist options
  • Save smacker/2953300 to your computer and use it in GitHub Desktop.
Save smacker/2953300 to your computer and use it in GitHub Desktop.
from django.core.cache import cache
from django.db.models.signals import post_save
from django.dispatch import receiver
@register.simple_tag()
def last_items(num=10):
last_items = cache.get('last_items')
if not last_items:
ads = Ad.objects.filter(status='actual').order_by('-created_date', '-id')[:num]
last_items = render_to_string('blocks/inline/last_items.html', {'ads': ads})
cache.set('last_items', last_items)
return last_items
@receiver(post_save, sender=Ad)
def clear_last_items(sender, instance, created, *args, **kwargs):
cache.delete('last_items')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment