Skip to content

Instantly share code, notes, and snippets.

@fitoria
Created January 18, 2013 03:33
Show Gist options
  • Save fitoria/4562143 to your computer and use it in GitHub Desktop.
Save fitoria/4562143 to your computer and use it in GitHub Desktop.
#context manager para el menú.
from django.core.cache import cache
from models import Menu
def menu_context_manager(request):
#probamos si esta el elemento en caché
valor = cache.get('menu')
#si no está leemos de la base de datos
if not valor:
valor = Menu.objects.all()
#lo cacheamos
cache.set('menu', valor, 0)
return {'menu': valor}
from django.db import models
from django.core.cache import cache
class Menu(models.Model):
'''Osea el menu'''
titulo = models.CharField(max_length=50)
url = models.URLField()
peso = models.PositiveIntegerField(default=0,
help_text="peso del elemento del menú, entre mayor sea el número mas hacia el fondo estará el elemento")
def save(self, *args, **kwargs):
#primero guardamos
return_value = super(ElementoNavegacion, self).save(*args, **kwargs)
#invalidamos el cache antiguo de menu
cache.delete('menu')
#creamos el nuevo cache refrescado
cache.set('menu', Menu.objects.all(), 0)
return return_value
def __unicode__(self):
return "%s (%s)" % (self.titulo, self.url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment