Example #1
0
    def test_cache(self):
        cache = ObjectCache(default_expiration=1)
        self.assertEqual(str(cache), "ObjectCache(n=0, default_expiration=1)")

        # Data
        cache["foo"] = "bar"
        self.assertIn("foo", cache)
        self.assertEqual(cache["foo"], "bar")
        self.assertEqual(cache.get("foo", "New"), "bar")

        # Expiration
        time.sleep(2)
        self.assertNotIn("foo", cache)

        # Get
        self.assertEqual(cache.get("foo", "New"), "New")
Example #2
0
    def test_cache_autoclean(self):
        cache = ObjectCache(default_expiration=1, auto_clean=True)
        self.assertEqual(str(cache), "ObjectCache(n=0, default_expiration=1)")

        # Data
        cache["foo"] = "bar"
        self.assertEqual(str(cache), "ObjectCache(n=1, default_expiration=1)")
        self.assertIn("foo", cache)
        self.assertEqual(cache["foo"], "bar")
        self.assertEqual(cache.get("foo", "New"), "bar")

        # Expiration
        time.sleep(2)
        self.assertNotIn("foo", cache)
        self.assertEqual(str(cache), "ObjectCache(n=0, default_expiration=1)")
        self.assertEqual(len(list(cache)), 0)

        # Get
        self.assertEqual(cache.get("foo", "New"), "New")