Beispiel #1
0
 def testPutDoesntCopyByDefault(self):
     cache = MaybeCacher()
     cache.make_cache("cache1", 5, 5)
     obj = [1, 2, 3]
     # We put this into the cache manually to avoid a false pass from something .get does
     cache.put("cache1", "foo", obj)
     cached_obj = cache.caches["cache1"].data["foo"][1]
     self.assertEqual(id(obj), id(cached_obj))
Beispiel #2
0
 def testNoCaching(self):
     with mock.patch("auslib.util.cache.ExpiringLRUCache") as lru:
         cache = MaybeCacher(maxsize=0, timeout=0)
         cache.put("cache1", "foo", "bar")
         # Nothing should be in the cache, because there _isn't_ one.
         self.assertEquals(cache.get("cache1", "foo"), None)
         # And the underlying cache object should not have been touched.
         self.assertFalse(lru.put.called)
         self.assertFalse(lru.get.called)
Beispiel #3
0
 def testNoCaching(self):
     with mock.patch("auslib.util.cache.ExpiringLRUCache") as lru:
         cache = MaybeCacher(maxsize=0, timeout=0)
         cache.put("cache1", "foo", "bar")
         # Nothing should be in the cache, because there _isn't_ one.
         self.assertEquals(cache.get("cache1", "foo"), None)
         # And the underlying cache object should not have been touched.
         self.assertFalse(lru.put.called)
         self.assertFalse(lru.get.called)
Beispiel #4
0
 def testCopyOnGet(self):
     cache = MaybeCacher()
     cache.make_cache("cache1", 5, 5)
     cache.make_copies = True
     obj = [1, 2, 3]
     # We put this into the cache manually to avoid a false pass from something .put does
     cache.caches["cache1"].data["foo"] = obj
     cached_obj = cache.get("cache1", "foo")
     self.assertNotEqual(id(obj), id(cached_obj))
Beispiel #5
0
 def testGetDoesntCopyByDefault(self):
     cache = MaybeCacher()
     cache.make_cache("cache1", 5, 5)
     obj = [1, 2, 3]
     # We put this into the cache manually to avoid a false pass from something .put does
     # cache entry format is (pos, value, expiration)
     cache.caches["cache1"].data["foo"] = (0, obj, 9999999999999999)
     cached_obj = cache.get("cache1", "foo")
     self.assertEqual(id(obj), id(cached_obj))
Beispiel #6
0
 def testGetDoesntCopyByDefault(self):
     cache = MaybeCacher()
     cache.make_cache("cache1", 5, 5)
     obj = [1, 2, 3]
     # We put this into the cache manually to avoid a false pass from something .put does
     # cache entry format is (pos, value, expiration)
     cache.caches["cache1"].data["foo"] = (0, obj, 9999999999999999)
     cached_obj = cache.get("cache1", "foo")
     self.assertEqual(id(obj), id(cached_obj))
Beispiel #7
0
 def testCacheExpired(self):
     cache = MaybeCacher(maxsize=5, timeout=5)
     # In order to avoid tests failing due to clock skew or other
     # issues with system clocks we can mock time.time() and make sure
     # it always returns a difference large enough to force a cache expiry
     with mock.patch("time.time") as t:
         t.return_value = 100
         cache.put("cache1", "foo", "bar")
         t.return_value = 200
         self.assertEquals(cache.get("cache1", "foo"), None)
Beispiel #8
0
 def testCacheExpired(self):
     cache = MaybeCacher(maxsize=5, timeout=5)
     # In order to avoid tests failing due to clock skew or other
     # issues with system clocks we can mock time.time() and make sure
     # it always returns a difference large enough to force a cache expiry
     with mock.patch("time.time") as t:
         t.return_value = 100
         cache.put("cache1", "foo", "bar")
         t.return_value = 200
         self.assertEquals(cache.get("cache1", "foo"), None)
Beispiel #9
0
 def testCopyOnPut(self):
     cache = MaybeCacher()
     cache.make_cache("cache1", 5, 5)
     cache.make_copies = True
     obj = [1, 2, 3]
     # We put this into the cache manually to avoid a false pass from something .get does
     cache.put("cache1", "foo", obj)
     cached_obj = cache.caches["cache1"].data["foo"]
     self.assertNotEquals(id(obj), id(cached_obj))
Beispiel #10
0
 def testPutDoesntCopyByDefault(self):
     cache = MaybeCacher()
     cache.make_cache("cache1", 5, 5)
     obj = [1, 2, 3]
     # We put this into the cache manually to avoid a false pass from something .get does
     cache.put("cache1", "foo", obj)
     cached_obj = cache.caches["cache1"].data["foo"][1]
     self.assertEquals(id(obj), id(cached_obj))
Beispiel #11
0
 def testSimpleCache(self):
     cache = MaybeCacher(maxsize=5, timeout=5)
     cache.put("cache1", "foo", "bar")
     self.assertEquals(cache.get("cache1", "foo"), "bar")
Beispiel #12
0
 def testSimpleCache(self):
     cache = MaybeCacher()
     cache.make_cache("cache1", 5, 5)
     cache.put("cache1", "foo", "bar")
     self.assertEquals(cache.get("cache1", "foo"), "bar")
Beispiel #13
0
# convenient to have the database object accessible in single place for both
# apps, which is why the "dbo" variable below exists. This DbWrapper class
# exists solely to defer the AUSDatabase object import to work around circular
# dependency issues that would occur if it were imported at parse time instead
# of runtime.


class DbWrapper(object):
    def __init__(self):
        self.db = None

    def setDb(self, dburi):
        from auslib.db import AUSDatabase
        self.db = AUSDatabase(dburi)

    def __getattr__(self, name):
        if not self.db:
            raise RuntimeError("No database configured")
        return getattr(self.db, name)


dbo = DbWrapper()

# Similar to the above, we have a complication around having two separate
# applications existing in the same library. This cache class is a simple
# wrapper that does nothing if caching is disabled, and uses a 3rd party
# caching library if it is enabled.
from auslib.util.cache import MaybeCacher

cache = MaybeCacher()
Beispiel #14
0
 def testSimpleCache(self):
     cache = MaybeCacher(maxsize=5, timeout=5)
     cache.put("cache1", "foo", "bar")
     self.assertEquals(cache.get("cache1", "foo"), "bar")
Beispiel #15
0
 def testSimpleCache(self):
     cache = MaybeCacher()
     cache.make_cache("cache1", 5, 5)
     cache.put("cache1", "foo", "bar")
     self.assertEqual(cache.get("cache1", "foo"), "bar")