Created
March 12, 2011 06:19
-
-
Save toastdriven/867087 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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