Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Remove spaces from cache keys for get_live_promotions.
Browse files Browse the repository at this point in the history
This is needed for ascii protocol.
  • Loading branch information
bsimpson63 committed Mar 11, 2015
1 parent 4f91d09 commit cf49c83
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions r2/r2/lib/promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,23 +860,36 @@ def srnames_with_live_promos(user, site):
return promo_srnames.intersection(site_srnames)


def _get_live_promotions(sr_names):
# special handling for memcache ascii protocol
SPECIAL_NAMES = {" reddit.com": "_reddit.com"}
REVERSED_NAMES = {v: k for k, v in SPECIAL_NAMES.iteritems()}


def _get_live_promotions(sanitized_names):
now = promo_datetime_now()
ret = {sr_name: [] for sr_name in sr_names}
sr_names = [REVERSED_NAMES.get(name, name) for name in sanitized_names]
ret = {sr_name: [] for sr_name in sanitized_names}
for camp, link in get_promos(now, sr_names=sr_names):
if is_live_promo(link, camp):
weight = (camp.bid / camp.ndays)
pt = PromoTuple(link=link._fullname, weight=weight,
campaign=camp._fullname)
for sr_name in camp.target.subreddit_names:
if sr_name in sr_names:
ret[sr_name].append(pt)
sanitized_name = SPECIAL_NAMES.get(sr_name, sr_name)
ret[sanitized_name].append(pt)
return ret


def get_live_promotions(sr_names):
promos_by_srname = sgm(g.cache, sr_names, miss_fn=_get_live_promotions,
prefix='live_promotions', time=60, stale=True)
sanitized_names = [SPECIAL_NAMES.get(name, name) for name in sr_names]
promos_by_sanitized_name = sgm(
g.cache, sanitized_names, miss_fn=_get_live_promotions,
prefix='live_promotions', time=60, stale=True)
promos_by_srname = {
REVERSED_NAMES.get(name, name): val
for name, val in promos_by_sanitized_name.iteritems()
}
return itertools.chain.from_iterable(promos_by_srname.itervalues())


Expand Down

0 comments on commit cf49c83

Please sign in to comment.