Exemplo n.º 1
0
    def test_collisions(self):
        def collision_hash(key):
            if key in {'key-1', 'key-2'}:
                return 'the same hashed value'
            else:
                return self.cache_hash_for(key)
        cache._hash_for = collision_hash

        cache.set('key-1', 'value-1')
        cache.set('key-2', 'value-2')

        self.assertEqual(cache.get('key-1'), 'value-1')
        self.assertEqual(cache.get('key-2'), 'value-2')
Exemplo n.º 2
0
def get_and_cache(url, use_cache=True, **kwargs):
    """
    Perform an HTTP GET request to the given url and optionally cache the
    result somewhere in the file system. The cached content will be used
    in the subsequent requests.
    Raises all HTTP errors

    :param url: URL of the page to retrieve
    :param use_cache: Whether to use cache
    :param \*\*kwargs: keyword arguments to pass to `requests.get`
    :return: The content page at the given URL, unicode
    """
    if not use_cache:
        r = requests.get(url, **kwargs)
        r.raise_for_status()
        content = r.text
    else:
        key = url + json.dumps(kwargs)
        content = cache.get(key)
        if content is None:
            content = get_and_cache(url, use_cache=False, **kwargs)
            cache.set(key, content)
    return content
Exemplo n.º 3
0
def get_and_cache(url, use_cache=True, **kwargs):
    """
    Perform an HTTP GET request to the given url and optionally cache the
    result somewhere in the file system. The cached content will be used
    in the subsequent requests.
    Raises all HTTP errors

    :param url: URL of the page to retrieve
    :param use_cache: Whether to use cache
    :param \*\*kwargs: keyword arguments to pass to `requests.get`
    :return: The content page at the given URL, unicode
    """
    if not use_cache:
        r = requests.get(url, **kwargs)
        r.raise_for_status()
        content = r.text
    else:
        key = url + json.dumps(kwargs)
        content = cache.get(key)
        if content is None:
            content = get_and_cache(url, use_cache=False, **kwargs)
            cache.set(key, content)
    return content
Exemplo n.º 4
0
 def test_objects(self):
     obj = {'this': ['is', ['a']], 'complex': 'object'}
     cache.set('obj', obj)
     self.assertEqual(obj, cache.get('obj'))
Exemplo n.º 5
0
 def test_enabled(self):
     cache.ENABLED = False
     cache.set('key', 'value')
     self.assertEqual(cache.get('key', 'something else'), 'something else')
     cache.ENABLED = True
Exemplo n.º 6
0
 def test_unicode(self):
     cache.set(u'\u84c4\u3048\u3066 \u304f\u3060\u3055\u3044',
               u'\u304a\u75b2\u308c\u3055\u307e')
     self.assertEqual(cache.get(u'\u84c4\u3048\u3066 \u304f\u3060\u3055\u3044'),
                      u'\u304a\u75b2\u308c\u3055\u307e')
Exemplo n.º 7
0
 def test_set_overwrite(self):
     cache.set('key', 'value')
     cache.set('key', 'another value', overwrite=True)
     self.assertEqual(cache.get('key'), 'another value')
     cache.set('key', 'something else', overwrite=False)
     self.assertEqual(cache.get('key'), 'another value')
Exemplo n.º 8
0
 def test_simple_set(self):
     cache.set('key', 'value')
     self.assertEqual(cache.get('key'), 'value')
Exemplo n.º 9
0
 def test_simple_get(self):
     self.assertIsNone(cache.get('non existing'))
     self.assertEqual(cache.get('non existing', 'default'), 'default')