def test_update(self): from zope.app.cache.ram import RAMCache c = RAMCache() c.update(1, 2, 3) s = c._getStorage() self.assertEqual(s.maxEntries, 1, "maxEntries not set") self.assertEqual(s.maxAge, 2, "maxAge not set") self.assertEqual(s.cleanupInterval, 3, "cleanupInterval not set")
def test_getStatistics(self): from zope.app.cache.ram import RAMCache c = RAMCache() c.set(42, "object", key={'foo': 'bar'}) c.set(43, "object", key={'foo': 'bar'}) c.query("object") c.query("object", key={'foo': 'bar'}) r1 = c._getStorage().getStatistics() r2 = c.getStatistics() self.assertEqual(r1, r2, "see Storage.getStatistics() tests")
def test_buildKey(self): from zope.app.cache.ram import RAMCache kw = {'foo': 1, 'bar': 2, 'baz': 3} key = RAMCache._buildKey(kw) self.assertEqual(key, (('bar',2), ('baz',3), ('foo',1)), "wrong key")
def test_getStorage(self): from zope.app.cache.ram import RAMCache c = RAMCache() c.maxAge = 123 c.maxEntries = 2002 c.cleanupInterval = 42 storage1 = c._getStorage() storage2 = c._getStorage() self.assertEqual(storage1, storage2, "_getStorage returns different storages") self.assertEqual(storage1.maxAge, 123, "maxAge not set (expected 123, got %s)" % storage1.maxAge) self.assertEqual(storage1.maxEntries, 2002, "maxEntries not set (expected 2002, got %s)" % storage1.maxEntries) self.assertEqual(storage1.cleanupInterval, 42, "cleanupInterval not set (expected 42, got %s)" % storage1.cleanupInterval) # Simulate persisting and restoring the RamCache which removes # all _v_ attributes. for k in c.__dict__.keys(): if k.startswith('_v_'): del c.__dict__[k] storage2 = c._getStorage() self.assertEqual(storage1, storage2, "_getStorage returns different storages")
def test_set(self): from zope.app.cache.ram import RAMCache ob = ('path',) keywords = {"answer": 42} value = "true" c = RAMCache() c.requestVars = ('foo', 'bar') key = RAMCache._buildKey(keywords) c.set(value, ob, keywords) self.assertEqual(c._getStorage().getEntry(ob, key), value, "Not stored correctly")
def test_query(self): from zope.app.cache.ram import RAMCache ob = ('aaa',) keywords = {"answer": 42} value = "true" c = RAMCache() key = RAMCache._buildKey(keywords) c._getStorage().setEntry(ob, key, value) self.assertEqual(c.query(ob, keywords), value, "incorrect value") self.assertEqual(c.query(ob, None), None, "defaults incorrect") self.assertEqual(c.query(ob, {"answer": 2}, default="bummer"), "bummer", "default doesn't work")
def test_invalidate(self): from zope.app.cache.ram import RAMCache ob1 = ("loc1",) ob2 = ("loc2",) keywords = {"answer": 42} keywords2 = {"answer": 41} value = "true" c = RAMCache() key1 = RAMCache._buildKey(keywords) key2 = RAMCache._buildKey(keywords) key3 = RAMCache._buildKey(keywords2) # Test invalidating entries with a keyword c._getStorage().setEntry(ob1, key1, value) c._getStorage().setEntry(ob2, key2, value) c._getStorage().setEntry(ob2, key3, value) c.invalidate(ob2, keywords) c._getStorage().getEntry(ob1, key1) self.assertRaises(KeyError, c._getStorage().getEntry, ob2, key2) c._getStorage().getEntry(ob2, key3) # Test deleting the whole object c._getStorage().setEntry(ob1, key1, value) c._getStorage().setEntry(ob2, key2, value) c._getStorage().setEntry(ob2, key3, value) c.invalidate(ob2) self.assertRaises(KeyError, c._getStorage().getEntry, ob2, key2) self.assertRaises(KeyError, c._getStorage().getEntry, ob2, key3) c._getStorage().getEntry(ob1, key1) # Try something that's not there c.invalidate(('yadda',))
# Unfortunately we needed more information, like the username that the ticket # belongs to so that content can be created with the correct ownership. And # the username retrieval is quite Zope2-specific. from Acquisition import aq_inner from collective.quickupload import logger from zope.security.interfaces import Unauthorized import AccessControl import random try: from zope.app.cache.ram import RAMCache except ImportError: from zope.ramcache.ram import RAMCache ticketCache = RAMCache() def issueTicket(ident): """ issues a timelimit ticket >>> type(issueTicket(object()))== type('') True """ ticket = str(random.random()) sm = AccessControl.getSecurityManager() user = sm.getUser() if user is None: raise Unauthorized('No currently authenticated user') ticketCache.set(user.getId(), ident, key=dict(ticket=ticket)) return ticket