Created
January 10, 2015 01:14
-
-
Save miraculixx/fb75ed5108db868e81bc to your computer and use it in GitHub Desktop.
Cacheable URL retriever for Django
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
""" | |
(c) 2015 github.com/miraculixx | |
MIT License | |
Cacheable URL getter for Django | |
Uses Django's cache framework to cache responses with the same URL. | |
Usage: | |
url = 'http://www.domain.com/static/content' | |
req = CachedRequest() | |
resp = req.get(url) | |
# 1st time resp.was_cached == False | |
resp = req.get(url) | |
# 2nd time resp.was_cached == True | |
req = CachedRequest(cache_timeout=15) # in seconds | |
resp = req.get(url) | |
# 1st time resp.was_cached == False | |
sleep(20) | |
resp = req.get(url) | |
# 2nd time resp.was_cached == False | |
""" | |
import logging | |
import urllib | |
import urllib2 | |
import urlparse | |
from django.core.cache import get_cache | |
logger = logging.getLogger(__name__) | |
class CachedRequest(object): | |
def __init__(self, url, cache_timeout=None): | |
self.url = url | |
self.was_cached = False | |
self.cache_timeout = cache_timeout or 24 * 3600 | |
try: | |
self.cache = get_cache(self.config['cache']) | |
logger.debug('GeoAddress using cache %s' % self.cache) | |
except KeyError: | |
self.cache = None | |
logger.debug('No cache enabled') | |
def get(self, url=None): | |
""" | |
will GET call the url. if a cache was set in GeoAddress.cache | |
(usually by 'cache' in GEOADDRESS_CONFIG), will get the response | |
that was previously returned for the same query | |
""" | |
url = url or self.url | |
if self.cache: | |
key = hashlib.md5(url).hexdigest() | |
response =self.cache.get(key) | |
if response: | |
self.was_cached = True | |
logger.debug('served url=%s from cache' % url) | |
return response | |
#opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1)) | |
opener = urllib2.build_opener(urllib2.HTTPHandler()) | |
response = opener.open(url).read() | |
logger.debug('retrieved url=%s' % url) | |
self.was_cached = False | |
if self.cache: | |
key = hashlib.md5(url).hexdigest() | |
# cache response for 24 hours | |
self.cache.set(key, response, self.cache_timeout) | |
logger.debug('cached url=%s' % url) | |
return response | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment