Skip to content

Commit

Permalink
adds cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mekarpeles authored and cdrini committed Jan 28, 2022
1 parent be634be commit d8c9f87
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions openlibrary/plugins/openlibrary/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,13 @@ def GET(self, key):
data = self.get_exports(lst, raw=True)
web.header("Content-Type", "application/json")
return delegate.RawText(json.dumps(data))
elif format == "yaml":
elif format == "yaml":
data = self.get_exports(lst, raw=True)
web.header("Content-Type", "application/yaml")
return delegate.RawText(formats.dump_yaml(data))
else:
raise web.notfound()

def get_exports(self, lst: list, raw: bool = False) -> Dict[str, list]:
export_data = lst.get_export_list()
if "editions" in export_data:
Expand Down
7 changes: 4 additions & 3 deletions openlibrary/templates/trending.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div id="contentBody">
$ pages = {'now': 'Now', 'daily': 'Today', 'weekly': 'This Week', 'monthly': 'This Month', 'yearly': 'This Year', 'forever': 'All Time'}
<h1>$_('Trending Books'): $_(pages[mode])</h1>
<p>Books patrons in the community have added to their shelves</p>
<p>$_("See what readers from the community are adding to their bookshelves")</p>
<p>
$for p in pages:
<a style="$('font-weight: bold;' if p==mode else '')" href="/trending/$(p)">$pages[p]</a>
Expand All @@ -15,10 +15,11 @@ <h1>$_('Trending Books'): $_(pages[mode])</h1>
$for entry in logged_books:
$if 'bookshelf_id' in entry:
$ shelf = {1: "Want to Read", 2: "Currently Reading", 3: "Read"}[entry['bookshelf_id']]
$ extra = "Anonymous " + "marked as " + shelf + " at " + entry['created'].strftime("%m/%d/%Y at %H:%M:%S")
$ extra = "Someone marked as " + shelf + ", " + entry['created'].strftime("%m/%d/%Y at %H:%M:%S")
$else:
$ extra = 'Logged %i times %s' % (entry['cnt'], pages[mode])
$:macros.SearchResultsWork(entry['work'], extra=extra, availability=entry['work'].get('availability'))
$if entry.get('work'):
$:macros.SearchResultsWork(entry['work'], extra=extra, availability=entry['work'].get('availability'))
</ul>
</div>
</div>
26 changes: 18 additions & 8 deletions openlibrary/views/loanstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ def reading_log_summary():
reading_log_summary, 'stats.readling_log_summary', timeout=dateutil.HOUR_SECS
)

def cached_get_most_logged_books(shelf_id=None, since_days=1, limit=20):
return cache.memcache_memoize(
get_most_logged_books, 'stats.trending', timeout=dateutil.HOUR_SECS
)(shelf_id=shelf_id, since_days=since_days, limit=limit)

def get_most_logged_books(shelf_id=None, since=dateutil.DATE_ONE_DAY_AGO, limit=20):
def get_most_logged_books(shelf_id=None, since_days=1, limit=20):
"""
shelf_id: Bookshelves.PRESET_BOOKSHELVES['Want to Read'|'Already Read'|'Currently Reading']
since: DATE_ONE_YEAR_AGO, DATE_ONE_MONTH_AGO, DATE_ONE_WEEK_AGO, DATE_ONE_DAY_AGO
Expand All @@ -42,7 +46,12 @@ def get_most_logged_books(shelf_id=None, since=dateutil.DATE_ONE_DAY_AGO, limit=
if 'env' not in web.ctx:
delegate.fakeload()

return Bookshelves.most_logged_books(shelf_id=shelf_id, since=since, limit=limit)
# Return as dict to enable cache serialization
return [dict(book) for book in
Bookshelves.most_logged_books(
shelf_id=shelf_id,
since=dateutil.date_n_days_ago(since_days),
limit=limit)]


def reading_log_leaderboard(limit=None):
Expand Down Expand Up @@ -124,11 +133,11 @@ def GET(self, page=''):
logged_books = get_activity_stream(limit=limit)
else:
shelf_id = None # optional; get from web.input()?
logged_books = get_most_logged_books(since={
'daily': dateutil.DATE_ONE_DAY_AGO,
'weekly': dateutil.DATE_ONE_WEEK_AGO,
'monthly': dateutil.DATE_ONE_MONTH_AGO,
'yearly': dateutil.DATE_ONE_YEAR_AGO,
logged_books = cached_get_most_logged_books(since_days={
'daily': 1,
'weekly': 7,
'monthly': 30,
'yearly': 365,
'forever': None,
}[page], limit=limit)

Expand All @@ -138,7 +147,8 @@ def GET(self, page=''):
work_index[work_key]['availability'] = availability_index[work_key]
for i, logged_book in enumerate(logged_books):
key = f"/works/OL{logged_book['work_id']}W"
logged_books[i]['work'] = work_index[key]
if key in work_index:
logged_books[i]['work'] = work_index[key]
return app.render_template("trending", logged_books=logged_books, mode=page)


Expand Down
2 changes: 1 addition & 1 deletion static/css/base/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ td {
padding: 0;
}
h1 {
margin: 20px;
margin: 20px 0;
}
button {
outline: none;
Expand Down

0 comments on commit d8c9f87

Please sign in to comment.