def test_default_factory(self): lru_dict = LRUDict(1, default_factory=list) self.assertEqual(lru_dict['foo'], []) lru_dict['bar'].append('bar') self.assertEqual(lru_dict, {'bar': ['bar']})
def get_cache(self): from trytond.cache import LRUDict keys = tuple(((key, self.context[key]) for key in sorted(self.cache_keys) if key in self.context)) return self.cache.setdefault((self.user, keys), LRUDict(config.getint('cache', 'model')))
def test_setdefault(self): lru_dict = LRUDict(1) lru_dict['foo'] = 'foo' self.assertEqual(len(lru_dict), 1) lru_dict.setdefault('bar', 'value') self.assertEqual(len(lru_dict), 1) self.assertEqual(lru_dict, {'bar': 'value'})
def test_setitem(self): lru_dict = LRUDict(1) lru_dict['foo'] = 'foo' self.assertEqual(len(lru_dict), 1) lru_dict['bar'] = 'bar' self.assertEqual(len(lru_dict), 1) self.assertEqual(lru_dict, {'bar': 'bar'})
def get_cache(self): from trytond.cache import LRUDict from trytond.transaction import Transaction user = Transaction().user context = Transaction().context keys = tuple(((key, context[key]) for key in sorted(self.cache_keys) if key in context)) return self.cache.setdefault((user, keys), LRUDict(config.getint('cache', 'model')))
def __new__(cls, new=False): from trytond.pool import Pool from trytond.cache import LRUDict transactions = cls._local.transactions if new or not transactions: instance = super(Transaction, cls).__new__(cls) instance.cache = LRUDict( _cache_transaction, lambda: LRUDict(_cache_model, lambda name: LRUDict( record_cache_size(instance), Pool().get(name)._record), default_factory_with_key=True)) instance._atexit = [] transactions.append(instance) else: instance = transactions[-1] return instance
def test_update(self): lru_dict = LRUDict(1) lru_dict['foo'] = 'foo' self.assertEqual(len(lru_dict), 1) lru_dict.update(bar='bar') lru_dict.update(baz='baz') self.assertEqual(len(lru_dict), 1) self.assertEqual(lru_dict, {'baz': 'baz'})
def test_default_factory_with_key(self): lru_dict = LRUDict( 1, default_factory=lambda k: k, default_factory_with_key=True) self.assertEqual(lru_dict['foo'], 'foo')