Changeset 9188


Ignore:
Timestamp:
May 12, 2011, 1:19:43 PM (14 years ago)
Author:
rick
Message:

Show-case cache generation to avoid re-generation the pictures all the time.

Location:
src/django_gheat
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/settings.py

    r9139 r9188  
    66DEBUG = True
    77TEMPLATE_DEBUG = DEBUG
     8
     9# Required for Etags Caching Implementation
     10USE_ETAGS = True
    811
    912ADMINS = (
     
    2326    }
    2427}
     28
     29CACHES = {
     30    'default': {
     31        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
     32        'LOCATION': 'unique-snowflake'
     33    }
     34}
     35
    2536
    2637# Local time zone for this installation. Choices can be found here:
     
    97108
    98109MIDDLEWARE_CLASSES = (
     110    'django.middleware.cache.UpdateCacheMiddleware',
    99111    'django.middleware.common.CommonMiddleware',
     112    'django.middleware.cache.FetchFromCacheMiddleware',
     113    'django.middleware.http.ConditionalGetMiddleware',
    100114    'django.contrib.sessions.middleware.SessionMiddleware',
    101115    'django.contrib.auth.middleware.AuthenticationMiddleware',
  • src/django_gheat/website/tile.py

    r9184 r9188  
    44#
    55# Rick van der Zwet <info@rickvanderzwet.nl>
     6from django.core.cache import cache
    67from django.core.management import setup_environ
    78from django.db.models import Max
     
    260261# Create your views here.
    261262def serve_tile(request,zoom,x,y):
     263  """
     264  Showcase on caching and tile generation:
     265
     266  To make it 'easy' we use two types of caching here, memory cache expiring
     267  at 30 seconds and database cache, always persistent storage on database.
     268  """
     269
    262270  hash_key = TileCache.make_key("%s %s %s %s" % (zoom, x ,y, request.GET.urlencode()))
    263   try:
    264     d = TileCache.objects.get(key=hash_key)
    265     data = d.data
    266   except TileCache.DoesNotExist:
    267     #log.setLevel(logging.DEBUG)
    268     filter = {}
    269     colour = (255,0,0)
    270     for key, value in request.GET.iteritems():
    271       if key == 'colour':
    272         colour = tuple(map(int,value.split(',')))
    273       else:
    274         filter[key] = value
    275     now = time.time()
    276     im = make_tile(int(x),int(y),int(zoom),filter=filter,colour=colour)
    277     log.info("Processing time: %s" % (time.time() - now))
    278     data = im.get_image('png')
    279     r = TileCache.objects.create(key=hash_key,data=data)
     271  data = cache.get(hash_key)
     272
     273  if not data:
     274    try:
     275      d = TileCache.objects.get(key=hash_key)
     276      data = d.data
     277      cache.set(hash_key,data, 30)
     278    except TileCache.DoesNotExist:
     279      #log.setLevel(logging.DEBUG)
     280      filter = {}
     281      colour = (255,0,0)
     282      for key, value in request.GET.iteritems():
     283        if key == 'colour':
     284          colour = tuple(map(int,value.split(',')))
     285        else:
     286          filter[key] = value
     287      now = time.time()
     288      im = make_tile(int(x),int(y),int(zoom),filter=filter,colour=colour)
     289      log.info("Processing time: %s" % (time.time() - now))
     290      data = im.get_image('png')
     291      cache.set(hash_key,data, 30)
     292      r = TileCache.objects.create(key=hash_key,data=data)
    280293
    281294  response = HttpResponse(mimetype="image/png")
Note: See TracChangeset for help on using the changeset viewer.