shoutscache, format
This commit is contained in:
@@ -4,7 +4,7 @@ from sqlalchemy import and_, desc, func, select
|
||||
from sqlalchemy.orm import selectinload
|
||||
from base.orm import local_session
|
||||
from orm.reaction import Reaction
|
||||
from orm.shout import Shout
|
||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||
from services.stat.reacted import ReactedStorage
|
||||
from services.stat.viewed import ViewedByDay
|
||||
|
||||
@@ -14,6 +14,16 @@ class ShoutsCache:
|
||||
period = 60 * 60 # 1 hour
|
||||
lock = asyncio.Lock()
|
||||
|
||||
recent_published = []
|
||||
recent_all = []
|
||||
recent_reacted = []
|
||||
top_month = []
|
||||
top_overall = []
|
||||
top_viewed = []
|
||||
|
||||
by_author = {}
|
||||
by_topic = {}
|
||||
|
||||
@staticmethod
|
||||
async def prepare_recent_published():
|
||||
with local_session() as session:
|
||||
@@ -151,6 +161,82 @@ class ShoutsCache:
|
||||
print("[zine.cache] %d top viewed shouts " % len(shouts))
|
||||
ShoutsCache.top_viewed = shouts
|
||||
|
||||
@staticmethod
|
||||
async def prepare_by_author():
|
||||
shouts_by_author = {}
|
||||
with local_session() as session:
|
||||
|
||||
for a in session.query(ShoutAuthor).all():
|
||||
|
||||
shout = session.query(Shout).filter(Shout.slug == a.shout).first()
|
||||
|
||||
if not shouts_by_author[a.author]:
|
||||
shouts_by_author[a.author] = []
|
||||
|
||||
if shout not in shouts_by_author[a.author]:
|
||||
shouts_by_author[a.author].push(shout)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache indexed by %d authors " % len(shouts_by_author.keys()))
|
||||
ShoutsCache.by_author = shouts_by_author
|
||||
|
||||
@staticmethod
|
||||
async def prepare_by_topic():
|
||||
shouts_by_topic = {}
|
||||
with local_session() as session:
|
||||
|
||||
for t in session.query(ShoutTopic).all():
|
||||
|
||||
shout = session.query(Shout).filter(Shout.slug == t.shout).first()
|
||||
|
||||
if not shouts_by_topic[t.topic]:
|
||||
shouts_by_topic[t.topic] = []
|
||||
|
||||
if shout not in shouts_by_topic[t.topic]:
|
||||
shouts_by_topic[t.topic].push(shout)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] indexed by %d topics " % len(shouts_by_topic.keys()))
|
||||
ShoutsCache.by_topic = shouts_by_topic
|
||||
|
||||
@staticmethod
|
||||
async def get_shouts_by_author():
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.by_author
|
||||
|
||||
@staticmethod
|
||||
async def get_shouts_by_topic():
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.by_topic
|
||||
|
||||
@staticmethod
|
||||
async def get_top_overall():
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.by_topic
|
||||
|
||||
@staticmethod
|
||||
async def get_top_month():
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.by_topic
|
||||
|
||||
@staticmethod
|
||||
async def get_top_viewed():
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.by_topic
|
||||
|
||||
@staticmethod
|
||||
async def get_recent_published():
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.recent_published
|
||||
|
||||
@staticmethod
|
||||
async def get_recent_all():
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.recent_all
|
||||
|
||||
@staticmethod
|
||||
async def get_recent_reacted():
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.recent_reacted
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
while True:
|
||||
@@ -158,9 +244,13 @@ class ShoutsCache:
|
||||
await ShoutsCache.prepare_top_month()
|
||||
await ShoutsCache.prepare_top_overall()
|
||||
await ShoutsCache.prepare_top_viewed()
|
||||
|
||||
await ShoutsCache.prepare_recent_published()
|
||||
await ShoutsCache.prepare_recent_all()
|
||||
await ShoutsCache.prepare_recent_reacted()
|
||||
|
||||
await ShoutsCache.prepare_by_author()
|
||||
await ShoutsCache.prepare_by_topic()
|
||||
print("[zine.cache] periodical update")
|
||||
except Exception as err:
|
||||
print("[zine.cache] error: %s" % (err))
|
||||
|
@@ -11,8 +11,8 @@ class TopicStorage:
|
||||
self = TopicStorage
|
||||
topics = session.query(Topic)
|
||||
self.topics = dict([(topic.slug, topic) for topic in topics])
|
||||
for topic in self.topics.values():
|
||||
self.load_parents(topic)
|
||||
for tpc in self.topics.values():
|
||||
self.load_parents(tpc)
|
||||
|
||||
print("[zine.topics] %d precached" % len(self.topics.keys()))
|
||||
|
||||
@@ -51,7 +51,16 @@ class TopicStorage:
|
||||
return list(topics)
|
||||
|
||||
@staticmethod
|
||||
async def add_topic(topic):
|
||||
async def get_topics_by_author(author):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
topics = filter(
|
||||
lambda topic: topic.community == author, self.topics.values()
|
||||
)
|
||||
return list(topics)
|
||||
|
||||
@staticmethod
|
||||
async def update_topic(topic):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
self.topics[topic.slug] = topic
|
||||
|
Reference in New Issue
Block a user