Skip to content

Commit

Permalink
Enhancing sections support and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aepton committed Sep 17, 2013
1 parent 3ab597c commit ae49783
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
7 changes: 5 additions & 2 deletions p2p/__init__.py
Expand Up @@ -99,7 +99,8 @@ def __init__(self, url, auth_token,
default_content_item_query=None,
content_item_defaults=None,
product_affiliate_code='chinews',
source_code='chicagotribune'):
source_code='chicagotribune',
webapp_name='tRibbit'):
self.config = {
'P2P_API_ROOT': url,
'P2P_API_KEY': auth_token,
Expand All @@ -109,6 +110,7 @@ def __init__(self, url, auth_token,
self.debug = debug
self.product_affiliate_code = product_affiliate_code
self.source_code = source_code
self.webapp_name = webapp_name

if default_content_item_query is None:
self.default_content_item_query = {'include': ['web_url']}
Expand Down Expand Up @@ -541,7 +543,8 @@ def get_section(self, path, force_update=False):
def get_section_configs(self, path, force_update=False):
query = {
'section_path': path,
'product_affiliate_code': self.product_affiliate_code
'product_affiliate_code': self.product_affiliate_code,
'webapp_name': self.webapp_name
}
if force_update:
data = self.get('/sections/show_configs.json', query)
Expand Down
26 changes: 24 additions & 2 deletions p2p/cache.py
Expand Up @@ -350,13 +350,35 @@ def remove_section(self, path):
"""
Remove all instances of this section from the cache
"""
pass
# construct a redis key query to get the keys for all copies of
# this section in the cache
key_query = self.make_key('section', path)
matching_keys = self.r.keys(key_query)

# if we don't have any keys, bail
if not matching_keys:
return False

# add the lookup key to our list of keys, then delete them all
self.r.delete(*matching_keys)
return True

def remove_section_configs(self, path):
"""
Remove all instances of the configs for this section from the cache
"""
pass
# construct a redis key query to get the keys for all copies of
# this section's configs in the cache
key_query = self.make_key('section_configs', path)
matching_keys = self.r.keys(key_query)

# if we don't have any keys, bail
if not matching_keys:
return False

# add the lookup key to our list of keys, then delete them all
self.r.delete(*matching_keys)
return True

def get(self, key):
ret = self.r.get(key)
Expand Down
26 changes: 26 additions & 0 deletions p2p/tests.py
Expand Up @@ -321,6 +321,32 @@ def test_redis_cache(self):
self.assertTrue(removed)
self.assertEqual(stats['content_item_gets'], 7)
self.assertEqual(stats['content_item_hits'], 1)

section_path = '/test/newsapps'
section = self.p2p.get_section(section_path)
self.p2p.cache.save_section(section_path, section)
section_configs = self.p2p.get_section_configs(section_path)
self.p2p.cache.save_section_configs(section_path, section_configs)
section = self.p2p.get_section(section_path)
section_configs = self.p2p.get_section_configs(section_path)
stats = self.p2p.cache.get_stats()
self.assertEqual(stats['sections_gets'], 2)
self.assertEqual(stats['sections_hits'], 1)
self.assertEqual(stats['section_configs_gets'], 2)
self.assertEqual(stats['section_configs_hits'], 1)

removed_section = self.p2p.cache.remove_section(section_path)
removed_section_configs = self.p2p.cache.remove_section_configs(
section_path)
section = self.p2p.get_section(section_path)
section_configs = self.p2p.get_section_configs(section_path)
stats = self.p2p.cache.get_stats()
self.assertTrue(removed_section)
self.assertTrue(removed_section_configs)
self.assertEqual(stats['sections_gets'], 3)
self.assertEqual(stats['sections_hits'], 1)
self.assertEqual(stats['section_configs_gets'], 3)
self.assertEqual(stats['section_configs_hits'], 1)


if __name__ == '__main__':
Expand Down

0 comments on commit ae49783

Please sign in to comment.