Beispiel #1
0
class ImageSimpleCache(ImageCache):
    """Simple image cache."""

    def __init__(self):
        """Initialize the cache."""
        super(ImageSimpleCache, self).__init__()
        self.cache = SimpleCache()

    def get(self, key):
        """Return the key value.

        :param key: the object's key
        :return: the stored object
        :rtype: `BytesIO` object
        """
        return self.cache.get(key)

    def set(self, key, value, timeout=None):
        """Cache the object.

        :param key: the object's key
        :param value: the stored object
        :type value: `BytesIO` object
        :param timeout: the cache timeout in seconds
        """
        timeout = timeout if timeout else self.timeout
        self.cache.set(key, value, timeout)

    def delete(self, key):
        """Delete the specific key."""
        self.cache.delete(key)

    def flush(self):
        """Flush the cache."""
        self.cache.clear()
class RecacheTestCase(unittest.TestCase):
    def setUp(self):
        self.recached = False

        def dispatcher(salt):
            self.recached = True

        self.c = SimpleCache()
        cfg = Config(preemptive_recache_seconds=10,
                     preemptive_recache_callback=dispatcher)
        self.s = Store(self.c, cfg)
        self.r = Retrieval(self.c, cfg)

    def test_preemptive_recaching_predicate(self):
        m = Metadata(HeaderSet(('foo', 'bar')), 'qux')

        def mkretr(**kwargs):
            return Retrieval(self.c, Config(**kwargs))

        with a.test_request_context('/'):
            self.assertFalse(
                mkretr(
                    preemptive_recache_seconds=10).should_recache_preemptively(
                        10, m))
            self.assertFalse(
                mkretr(preemptive_recache_callback=lambda x: 0).
                should_recache_preemptively(10, m))
            self.assertFalse(self.r.should_recache_preemptively(11, m))
            self.assertTrue(self.r.should_recache_preemptively(10, m))
            self.assertFalse(self.r.should_recache_preemptively(10, m))
            self.c.clear()
            self.assertTrue(self.r.should_recache_preemptively(10, m))

    def test_preemptive_recaching_cache_bypass(self):
        fresh = Response('foo')
        with a.test_request_context('/foo'):
            self.s.cache_response(fresh)
            metadata = self.r.fetch_metadata()
        with a.test_request_context('/foo'):
            cached = self.r.fetch_response()
            self.assertEquals(cached.headers[self.r.X_CACHE_HEADER], 'hit')
        with a.test_request_context('/foo',
                                    headers={RECACHE_HEADER: metadata.salt}):
            self.assertRaises(RecacheRequested, self.r.fetch_response)
        with a.test_request_context('/foo',
                                    headers={RECACHE_HEADER:
                                             'incorrect-salt'}):
            try:
                self.r.fetch_response()
            except RecacheRequested:
                self.fail('unexpected RecacheRequested for incorrect salt')
Beispiel #3
0
class UberTestCase(unittest.TestCase):
    def setUp(self):
        self.cache = SimpleCache()

        self.orig_set = uber.cache.set
        self.orig_get_many = uber.cache.get_many

        uber.cache.set = Mock(wraps=self.cache.set)
        uber.cache.get_many = Mock(wraps=self.cache.get_many)

    def tearDown(self):
        self.cache.clear()

        uber.cache.set = self.orig_set
        uber.cache.get_many = self.orig_get_many
Beispiel #4
0
class RecacheTestCase(unittest.TestCase):

    def setUp(self):
        self.recached = False
        def dispatcher(salt):
            self.recached = True
        self.c = SimpleCache()
        cfg = Config(preemptive_recache_seconds=10, preemptive_recache_callback=dispatcher)
        self.s = Store(self.c, cfg)
        self.r = Retrieval(self.c, cfg)

    def test_preemptive_recaching_predicate(self):
        m = Metadata(HeaderSet(('foo', 'bar')), 'qux')
        def mkretr(**kwargs):
            return Retrieval(self.c, Config(**kwargs))
        with a.test_request_context('/'):
            self.assertFalse(mkretr(preemptive_recache_seconds=10).should_recache_preemptively(10, m))
            self.assertFalse(mkretr(preemptive_recache_callback=lambda x: 0).should_recache_preemptively(10, m))
            self.assertFalse(self.r.should_recache_preemptively(11, m))
            self.assertTrue(self.r.should_recache_preemptively(10, m))
            self.assertFalse(self.r.should_recache_preemptively(10, m))
            self.c.clear()
            self.assertTrue(self.r.should_recache_preemptively(10, m))

    def test_preemptive_recaching_cache_bypass(self):
        fresh = Response('foo')
        with a.test_request_context('/foo'):
            self.s.cache_response(fresh)
            metadata = self.r.fetch_metadata()
        with a.test_request_context('/foo'):
            cached = self.r.fetch_response()
            self.assertEquals(cached.headers[self.r.X_CACHE_HEADER], 'hit')
        with a.test_request_context('/foo', headers={RECACHE_HEADER: metadata.salt}):
            self.assertRaises(RecacheRequested, self.r.fetch_response)
        with a.test_request_context('/foo', headers={RECACHE_HEADER: 'incorrect-salt'}):
            try:
                self.r.fetch_response()
            except RecacheRequested:
                self.fail('unexpected RecacheRequested for incorrect salt')
Beispiel #5
0
    rq_job = Job(id=job_id, connection=redis_db)
    print(rq_job.get_status())
    print(rq_job)
    res = {'Job ID': job_id, 'Status': rq_job.get_status()}
    return json.dumps(res)


@app.route('/jobs', methods=['GET'])
def jobs():
    jobs = q.get_job_ids()
    print(jobs)
    #TODO
    return 'Send JSON'


@app.route('/clear/<key>', methods=['GET'])
def clear_cache(key):
    delete_local_cache_key(key)
    res = "{0} is cleared from local cache".format(key)
    return json.dumps(res)


@app.route('/', methods=['GET'])
def index():
    render_template('templates/home_page.html')


if __name__ == '__main__':
    cache.clear()
    app.run(host='0.0.0.0', debug=True)
Beispiel #6
0
class ImageSimpleCache(ImageCache):
    """Simple image cache."""
    def __init__(self):
        """Initialize the cache."""
        super(ImageSimpleCache, self).__init__()
        self.cache = SimpleCache()

    def get(self, key):
        """Return the key value.

        :param key: the object's key
        :return: the stored object
        :rtype: `BytesIO` object
        """
        return self.cache.get(key)

    def set(self, key, value, timeout=None):
        """Cache the object.

        :param key: the object's key
        :param value: the stored object
        :type value: `BytesIO` object
        :param timeout: the cache timeout in seconds
        """
        timeout = timeout if timeout else self.timeout
        self.cache.set(key, value, timeout)
        self.set_last_modification(key, timeout=timeout)

    def get_last_modification(self, key):
        """Get last modification of cached file.

        :param key: the file object's key
        """
        last = self.cache.get(self._last_modification_key_name(key))
        return last

    def set_last_modification(self, key, last_modification=None, timeout=None):
        """Set last modification of cached file.

        :param key: the file object's key
        :param last_modification: Last modification date of
            file represented by the key
        :type last_modification: datetime
        :param timeout: the cache timeout in seconds
        """
        if not key:
            return
        if not last_modification:
            last_modification = datetime.utcnow().replace(microsecond=0)
        timeout = timeout if timeout else self.timeout
        self.cache.set(self._last_modification_key_name(key),
                       last_modification, timeout)

    def delete(self, key):
        """Delete the specific key."""
        if key:
            self.cache.delete(key)
            self.cache.delete(self._last_modification_key_name(key))

    def flush(self):
        """Flush the cache."""
        self.cache.clear()