コード例 #1
0
ファイル: simple.py プロジェクト: ShaldonHe/flask-iiif
class ImageSimpleCache(ImageCache):
    """Simple image cache."""
    def __init__(self, app=None):
        """Initialize the cache."""
        super(ImageSimpleCache, self).__init__(app=app)
        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 or 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
        """
        return self.get(self._last_modification_key_name(key))

    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.datetime
        :param timeout: the cache timeout in seconds
        """
        if not last_modification:
            last_modification = datetime.utcnow().replace(microsecond=0)
        timeout = timeout or self.timeout
        self.cache.set(self._last_modification_key_name(key),
                       last_modification, timeout)

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

    def flush(self):
        """Flush the cache."""
        self.cache.clear()
コード例 #2
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')