Skip to content

Instantly share code, notes, and snippets.

@toastdriven
Created March 12, 2011 06:19
Show Gist options
  • Save toastdriven/867087 to your computer and use it in GitHub Desktop.
Save toastdriven/867087 to your computer and use it in GitHub Desktop.
from tastypie.resources import ModelResource
from yourapp.api.cache import CustomCache
from yourapp.models import YourModel
class SampleResource(ModelResource):
class Meta:
queryset = YourModel.objects.all()
resource_name = 'sample'
def generate_cache_key(self, request_type, request, **kwargs):
# You need the GET kwargs here to make the key unique.
return "%s-%s" % (request_type, request.path)
def dispatch(self, request_type, request, **kwargs):
cache_key = self.generate_cache_key(request_type, request, **kwargs)
cached = cache.get(cache_key)
if cached:
# Probably need to handle what type of response/HTTP status code
# it was.
return HttpResponse(cached)
response = super(SampleResource, self).dispatch(request_type, request, **kwargs)
cache.set(cache_key, response.content)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment