| Line | |
|---|
| 1 | import memcache |
|---|
| 2 | from urllib2 import urlopen |
|---|
| 3 | |
|---|
| 4 | host = 'localhost:11211' |
|---|
| 5 | |
|---|
| 6 | mc = memcache.Client([host], debug=0) |
|---|
| 7 | connected = mc.servers[0].connect() != 0 |
|---|
| 8 | if not connected: |
|---|
| 9 | raise Exception('%s seems down' % host) |
|---|
| 10 | |
|---|
| 11 | def _get_metadata(url): |
|---|
| 12 | """get url infos""" |
|---|
| 13 | return urlopen(url).info() |
|---|
| 14 | |
|---|
| 15 | def _get_content(url): |
|---|
| 16 | """get url content""" |
|---|
| 17 | return urlopen(url).read() |
|---|
| 18 | |
|---|
| 19 | def 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 | |
|---|