From 0c882dfc44b845c0f8db6a88cedcfe4e1ffc2a14 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Wed, 5 Oct 2022 15:22:56 +0300 Subject: [PATCH] use-dict --- resolvers/zine.py | 26 +++++++++++++------------- services/zine/shoutscache.py | 10 ++++------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/resolvers/zine.py b/resolvers/zine.py index 5eb272f7..dc6eac8e 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -110,16 +110,16 @@ async def get_search_results(_, _info, searchtext, offset, limit): @query.field("shoutsByAuthors") -async def shouts_by_authors(_, _info, slugs, offset, limit): +async def shouts_by_authors(_, _info, slugs, offset=0, limit=100): async with ShoutsCache.lock: shouts = {} for author in slugs: - for shouts_by_author in ShoutsCache.by_author.get(author, []): - for s in shouts_by_author: - for a in s.authors: - a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) - if bool(s.publishedAt): - shouts[s.slug] = s + shouts_by_author = list(ShoutsCache.by_author.get(author, {}).values()) + for s in shouts_by_author: + for a in s.authors: + a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) + if bool(s.publishedAt): + shouts[s.slug] = s shouts_prepared = list(shouts.values()) shouts_prepared.sort(key=lambda s: s.publishedAt, reverse=True) return shouts_prepared[offset : offset + limit] @@ -130,12 +130,12 @@ async def shouts_by_topics(_, _info, slugs, offset=0, limit=100): async with ShoutsCache.lock: shouts = {} for topic in slugs: - for shouts_by_topic in ShoutsCache.by_topic.get(topic, []): - for s in shouts_by_topic: - for a in s.authors: - a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) - if bool(s.publishedAt): - shouts[s.slug] = s + shouts_by_topic = list(ShoutsCache.by_topic.get(topic, {}).values()) + for s in shouts_by_topic: + for a in s.authors: + a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) + if bool(s.publishedAt): + shouts[s.slug] = s shouts_prepared = list(shouts.values()) shouts_prepared.sort(key=lambda s: s.publishedAt, reverse=True) return shouts_prepared[offset : offset + limit] diff --git a/services/zine/shoutscache.py b/services/zine/shoutscache.py index 781a3a5b..e9c79214 100644 --- a/services/zine/shoutscache.py +++ b/services/zine/shoutscache.py @@ -65,13 +65,11 @@ class ShoutsCache: async with ShoutsCache.lock: for s in shouts: for a in s.authors: - ShoutsCache.by_author[a.slug] = ShoutsCache.by_author.get(a.slug, []) - if s not in ShoutsCache.by_author[a.slug]: - ShoutsCache.by_author[a.slug].append(s) + ShoutsCache.by_author[a.slug] = ShoutsCache.by_author.get(a.slug, {}) + ShoutsCache.by_author[a.slug][s.slug] = s for t in s.topics: - ShoutsCache.by_topic[t.slug] = ShoutsCache.by_topic.get(t.slug, []) - if s not in ShoutsCache.by_topic[t.slug]: - ShoutsCache.by_topic[t.slug].append(s) + ShoutsCache.by_topic[t.slug] = ShoutsCache.by_topic.get(t.slug, {}) + ShoutsCache.by_topic[t.slug][s.slug] = s print("[zine.cache] indexed by %d topics " % len(ShoutsCache.by_topic.keys())) print("[zine.cache] indexed by %d authors " % len(ShoutsCache.by_author.keys())) ShoutsCache.recent_published = shouts