def testCacheGetOK(self): one = [1,2,3,4] caching.cache_set('ok', value=one, length=2) two = caching.cache_get('ok') self.assertEqual(one, two) time.sleep(5) try: three = caching.cache_get('ok') self.fail('should have raised NotCachedError, got %s' % three) except caching.NotCachedError: pass
def testDisable(self): caching.cache_set('disabled', value=False) v = caching.cache_get('disabled') self.assertEqual(v, False) caching.cache_enable(False) caching.cache_set('disabled', value=True) try: caching.cache_get('disabled') self.fail('should have raised NotCachedError') except caching.NotCachedError, nce: key = caching.cache_key('disabled') self.assertEqual(nce.key, key)
def testDelete(self): caching.cache_set('del', value=True) for x in range(0,10): caching.cache_set('del', 'x', x, value=True) for y in range(0,5): caching.cache_set('del', 'x', x, 'y', y, value=True) # check to make sure all the values are in the cache self.assert_(caching.cache_get('del', default=False)) for x in range(0,10): self.assert_(caching.cache_get('del', 'x', x, default=False)) for y in range(0,5): self.assert_(caching.cache_get('del', 'x', x, 'y', y, default=False)) # try to delete just one killed = caching.cache_delete('del','x',1) self.assertEqual([caching.CACHE_PREFIX + "::del::x::1"], killed) self.assertFalse(caching.cache_get('del', 'x', 1, default=False)) # but the others are still there self.assert_(caching.cache_get('del', 'x', 2, default=False)) # now kill all of del::x::1 killed = caching.cache_delete('del','x', 1, children=True) for y in range(0,5): self.assertFalse(caching.cache_get('del', 'x', 1, 'y', y, default=False)) # but del::x::2 and children are there self.assert_(caching.cache_get('del','x',2,'y',1, default=False)) # kill the rest killed = caching.cache_delete('del', children=True) self.assertFalse(caching.cache_get('del',default=False)) for x in range(0,10): self.assertFalse(caching.cache_get('del', 'x', x, default=False)) for y in range(0,5): self.assertFalse(caching.cache_get('del', 'x', x, 'y', y, default=False))
def find_by_slug(cls, groupkey, slug, raises=False): """A helper function to look up an object by slug""" ob = None try: ob = caching.cache_get(groupkey, slug) except caching.NotCachedError, e: try: ob = cls.objects.get(slug__exact=slug) caching.cache_set(e.key, value=ob) except cls.DoesNotExist: log.debug("No such %s: %s", groupkey, slug) if raises: raise
def find_by_id(cls, groupkey, objectid, raises=False): """A helper function to look up an object by id""" ob = None try: ob = caching.cache_get(groupkey, objectid) except caching.NotCachedError, e: try: ob = cls.objects.get(pk=objectid) caching.cache_set(e.key, value=ob) except cls.DoesNotExist: log.debug("No such %s: %s", groupkey, objectid) if raises: raise cls.DoesNotExist
def cache_get(self, *args, **kwargs): key = self.cache_key(*args, **kwargs) return caching.cache_get(key)
def testCacheGetDefault(self): chk = caching.cache_get('default',default='-') self.assertEqual(chk, '-')
def testCacheGetFail(self): try: caching.cache_get('x') self.fail('should have raised NotCachedError') except caching.NotCachedError: pass