def test_cache_list(self): def key_extractor(o): return dict(id=o['id']) list_key = create_cache_key('test', {'1': 2, '3': 4}) self.cache_manager.set_list( list_key, (len(self.sample_data), self.sample_data), key_extractor=key_extractor) results_count, result = self.cache_manager.get_list(list_key) self.assertEqual(len(self.sample_data), results_count) self.assertEqual(len(self.sample_data), len(list(result))) self.cache_manager.invalidate_list(list_key) results_count, result = self.cache_manager.get_list( list_key, recover=lambda: (len(self.sample_data), self.sample_data), key_extractor=key_extractor) self.assertEqual(len(self.sample_data), results_count) self.assertEqual(len(self.sample_data), len(list(result))) self.assertEqual(self.cache_manager.get_item('test:id=1'), self.sample_data[0]) self.assertEqual(self.cache_manager.get_item('test:id=2'), self.sample_data[1]) self.assertEqual(self.cache_manager.get_item('test:id=3'), self.sample_data[2])
def list_worker(self): @self.cache_manager.decorate('test', list_=True, key_extractor=lambda x: dict(id=x['id'])) def get_list(key=None): self.call_count += 1 print("## get_list, call_count: %s" % self.call_count) return len(self.sample_lists[key]), self.sample_lists[key] for i in range(self.request_count_per_thread): if not self.invalidated and i == (self.request_count_per_thread // 2) and th() == 'th 01': self.invalidated = True print('Invalidating') self.cache_manager.invalidate_list(create_cache_key('test', dict(key='a'))) self.cache_manager.invalidate_list(create_cache_key('test', dict(key='b'))) self.assertListEqual(get_list(key='a')[1], self.sample_lists['a']) self.assertListEqual(get_list(key='b')[1], self.sample_lists['b'])
def item_worker(self): @self.cache_manager.decorate('test') def get_single_item(key=None): self.call_count += 1 print('%s: key=%s Calling backend' % (th(), key)) return self.sample_data_items[key] for i in range(self.request_count_per_thread): if not self.invalidated and i == (self.request_count_per_thread // 2) and th() == 'th 01': self.invalidated = True self.cache_manager.set_item(create_cache_key('test', dict(key='a')), 11) self.cache_manager.set_item(create_cache_key('test', dict(key='b')), 22) self.cache_manager.set_item(create_cache_key('test', dict(key='c')), 33) self.cache_manager.set_item(create_cache_key('test', dict(key='d')), 44) self.assertIsNotNone(get_single_item(key='a')) self.assertIsNotNone(get_single_item(key='b')) self.assertIsNotNone(get_single_item(key='c')) self.assertIsNotNone(get_single_item(key='d'))