root / cachers / cached_url.py

Revision 124:7f0429989f5f, 0.9 kB (checked in by Tarek Ziad?? <tarek@…>, 13 months ago)

returning cachers

Line 
1import memcache
2from urllib2 import urlopen
3
4host = 'localhost:11211'
5
6mc = memcache.Client([host], debug=0)
7connected = mc.servers[0].connect() != 0
8if not connected:
9    raise Exception('%s seems down' % host)
10
11def _get_metadata(url):
12    """get url infos"""
13    return urlopen(url).info()
14
15def _get_content(url):
16    """get url content"""
17    return urlopen(url).read()
18
19def get_content(url, control=False):
20    """get cached content"""
21    cached = mc.get(url)
22    if cached is not None:
23        infos, cached = cached
24        # controls the metadata to see
25        # if the source has changed
26        if control:
27            latest_infos = _get_metadata(url)
28            if latest_infos.values() == infos.values():
29                return infos, cached
30        else:
31            return infos, cached
32    cached = _get_content(url)
33    infos = _get_metadata(url)
34    mc.set(url, (infos, cached))
35    return infos, cached
36
37
Note: See TracBrowser for help on using the browser.