Exemplo n.º 1
0
    def test_method_simple(self):
        manager = queryUtility(ICacheManager)
        content = TestContent()
        self.assertEqual(content.add(4), 4)
        # The method is now cached, so add is not called.
        self.assertEqual(content.add(4), 4)

        cache = manager.get_cache(
            'silva.core.cache.tests.test_descriptors.add')
        self.assertEqual(cache.get(_verify_key(('4',))), 4)
        self.assertRaises(KeyError, cache.get, _verify_key(('5',)))

        self.assertEqual(content.add(5), 9)
        # The method is now cached, so add is not called.
        self.assertEqual(content.add(5), 9)

        self.assertEqual(cache.get(_verify_key(('4',))), 4)
        self.assertEqual(cache.get(_verify_key(('5',))), 9)
Exemplo n.º 2
0
    def test_method_region(self):
        manager = queryUtility(ICacheManager)
        content = TestContent()
        self.assertEqual(content.remove(4), -4)
        # The method is now cached, so add is not called.
        self.assertEqual(content.remove(4), -4)

        self.assertTrue('test_descriptors' in beaker.cache.cache_regions)
        cache = manager.get_cache_from_region(
            'silva.core.cache.tests.test_descriptors.remove',
            'test_descriptors')
        self.assertEqual(cache.get(_verify_key(('4',))), -4)
        self.assertRaises(KeyError, cache.get, _verify_key(('-5',)))

        self.assertEqual(content.remove(-5), 1)
        # The method is now cached, so add is not called.
        self.assertEqual(content.remove(-5), 1)

        self.assertEqual(cache.get(_verify_key(('4',))), -4)
        self.assertEqual(cache.get(_verify_key(('-5',))), 1)
Exemplo n.º 3
0
 def cached_property(self):
     utility = getUtility(ICacheManager)
     if region is not None:
         cache = utility.get_cache_from_region(cache_ns, region)
     else:
         cache = utility.get_cache(cache_ns, **cache_options)
     cache_key = 'property'
     if IPersistent.providedBy(self):
         cache_key = str(self._p_oid)
     def solves():
         return func(self)
     return cache.get_value(_verify_key(cache_key), createfunc=solves,
         expiretime=expire)
Exemplo n.º 4
0
 def cached_method(self, *args, **kwargs):
     utility = getUtility(ICacheManager)
     if region is not None:
         cache = utility.get_cache_from_region(cache_ns, region)
     else:
         cache = utility.get_cache(cache_ns, **cache_options)
     cache_key = None
     if key is not None:
         cache_key = key(self, *args, **kwargs)
     if cache_key is None:
         cache_key = tuple()
         if IPersistent.providedBy(self):
             cache_key += tuple([str(self._p_oid)])
         cache_key += tuple(map(str, args))
         cache_key += tuple(map(
                 lambda kwarg: "=".join(map(str, kwarg)),
                 sorted(kwargs.items(), key=operator.itemgetter(0))))
     def solves():
         return func(self, *args, **kwargs)
     return cache.get_value(_verify_key(cache_key), createfunc=solves,
         expiretime=expire)
Exemplo n.º 5
0
 def _real_key(self, key):
     real_key = self.__keys.get(key)
     if real_key is None:
         real_key = _verify_key(key)
         self.__keys[key] = real_key
     return real_key