import memcache
from urllib2 import urlopen

host = 'localhost:11211'

mc = memcache.Client([host], debug=0)
connected = mc.servers[0].connect() != 0
if not connected:
    raise Exception('%s seems down' % host)

def _get_metadata(url):
    """get url infos"""
    return urlopen(url).info()

def _get_content(url):
    """get url content"""
    return urlopen(url).read()

def get_content(url, control=False):
    """get cached content"""
    cached = mc.get(url)
    if cached is not None:
        infos, cached = cached
        # controls the metadata to see
        # if the source has changed
        if control:
            latest_infos = _get_metadata(url)
            if latest_infos.values() == infos.values():
                return infos, cached
        else:
            return infos, cached
    cached = _get_content(url)
    infos = _get_metadata(url)
    mc.set(url, (infos, cached))
    return infos, cached



