|
9 | 9 | PRAW API docs:
|
10 | 10 | https://praw.readthedocs.io/
|
11 | 11 | """
|
12 |
| - |
13 |
| -from . import source |
14 | 12 | import logging
|
15 |
| -from oauth_dropins import reddit |
16 |
| -from oauth_dropins.webutil import appengine_info, util |
| 13 | +import operator |
17 | 14 | import re
|
18 | 15 | import urllib.parse, urllib.request
|
| 16 | +import threading |
19 | 17 |
|
| 18 | +from cachetools import cachedmethod, TTLCache |
| 19 | +from oauth_dropins import reddit |
| 20 | +from oauth_dropins.webutil import appengine_info, util |
20 | 21 | import praw
|
21 | 22 | from prawcore.exceptions import NotFound
|
22 | 23 |
|
| 24 | +from . import source |
| 25 | + |
| 26 | +USER_CACHE_TIME = 5 * 60 # 5 minute expiration, in seconds |
| 27 | +user_cache = TTLCache(1000, USER_CACHE_TIME) |
| 28 | +user_cache_lock = threading.RLock() |
| 29 | + |
23 | 30 |
|
24 | 31 | class Reddit(source.Source):
|
25 | 32 | """Reddit source class. See file docstring and Source class for details."""
|
@@ -57,13 +64,23 @@ def post_id(self, url):
|
57 | 64 | if len(path_parts) >= 2:
|
58 | 65 | return path_parts[-2]
|
59 | 66 |
|
| 67 | + @cachedmethod(lambda self: user_cache, lock=lambda self: user_cache_lock, |
| 68 | + key=lambda user: getattr(user, 'name', None)) |
60 | 69 | def praw_to_actor(self, praw_user):
|
61 | 70 | """Converts a PRAW Redditor to an actor.
|
62 | 71 |
|
63 | 72 | Makes external calls to fetch data from the Reddit API.
|
64 | 73 |
|
65 | 74 | https://praw.readthedocs.io/en/latest/code_overview/models/redditor.html
|
66 | 75 |
|
| 76 | + Caches fetched user data for 5m to avoid repeating user profile API requests |
| 77 | + when fetching multiple comments or posts from the same author. Background: |
| 78 | + https://github.com/snarfed/bridgy/issues/1021 |
| 79 | +
|
| 80 | + Ideally this would be part of PRAW, but they seem uninterested: |
| 81 | + https://github.com/praw-dev/praw/issues/131 |
| 82 | + https://github.com/praw-dev/praw/issues/1140 |
| 83 | +
|
67 | 84 | Args:
|
68 | 85 | user: PRAW Redditor object
|
69 | 86 |
|
|
0 commit comments