|
1 | 1 | """Base protocol class and common code."""
|
2 | 2 | import copy
|
| 3 | +from datetime import timedelta |
3 | 4 | import logging
|
4 | 5 | from threading import Lock
|
5 | 6 | from urllib.parse import urljoin
|
|
38 | 39 | 'video',
|
39 | 40 | )
|
40 | 41 |
|
| 42 | +OBJECT_REFRESH_AGE = timedelta(days=30) |
| 43 | + |
41 | 44 | # activity ids that we've already handled and can now ignore.
|
42 | 45 | # used in Protocol.receive
|
43 | 46 | seen_ids = LRUCache(100000)
|
@@ -1099,44 +1102,48 @@ def load(cls, id, remote=None, local=True, **kwargs):
|
1099 | 1102 | requests.HTTPError: anything that :meth:`fetch` raises
|
1100 | 1103 | """
|
1101 | 1104 | assert local or remote is not False
|
1102 |
| - |
1103 |
| - if remote is not True: |
1104 |
| - with objects_cache_lock: |
1105 |
| - cached = objects_cache.get(id) |
1106 |
| - if cached: |
1107 |
| - # make a copy so that if the client modifies this entity in |
1108 |
| - # memory, those modifications aren't applied to the cache |
1109 |
| - # until they explicitly put() the modified entity. |
1110 |
| - # NOTE: keep in sync with Object._post_put_hook! |
1111 |
| - return Object(id=cached.key.id(), **cached.to_dict( |
1112 |
| - # computed properties |
1113 |
| - exclude=['as1', 'expire', 'object_ids', 'type'])) |
| 1105 | + logger.debug(f'Loading Object {id} local={local} remote={remote}') |
1114 | 1106 |
|
1115 | 1107 | obj = orig_as1 = None
|
1116 |
| - if local: |
| 1108 | + with objects_cache_lock: |
| 1109 | + cached = objects_cache.get(id) |
| 1110 | + if cached: |
| 1111 | + # make a copy so that if the client modifies this entity in |
| 1112 | + # memory, those modifications aren't applied to the cache |
| 1113 | + # until they explicitly put() the modified entity. |
| 1114 | + # NOTE: keep in sync with Object._post_put_hook! |
| 1115 | + logger.debug(' got from cache') |
| 1116 | + obj = Object(id=cached.key.id(), **cached.to_dict( |
| 1117 | + # computed properties |
| 1118 | + exclude=['as1', 'expire', 'object_ids', 'type'])) |
| 1119 | + |
| 1120 | + if local and not obj: |
1117 | 1121 | obj = Object.get_by_id(id)
|
1118 |
| - if obj and (obj.as1 or obj.raw or obj.deleted): |
1119 |
| - logger.info(' got from datastore') |
| 1122 | + if not obj: |
| 1123 | + logger.debug(f' not in datastore') |
| 1124 | + elif obj.as1 or obj.raw or obj.deleted: |
| 1125 | + logger.debug(' got from datastore') |
1120 | 1126 | obj.new = False
|
1121 |
| - orig_as1 = obj.as1 |
1122 | 1127 | if remote is not True:
|
1123 | 1128 | with objects_cache_lock:
|
1124 | 1129 | objects_cache[id] = obj
|
1125 |
| - return obj |
1126 | 1130 |
|
1127 |
| - if remote is True: |
1128 |
| - logger.debug(f'Loading Object {id} local={local} remote={remote}, forced refresh requested') |
1129 |
| - elif remote is False: |
1130 |
| - logger.debug(f'Loading Object {id} local={local} remote={remote} {"empty" if obj else "not"} in datastore') |
| 1131 | + if remote is False: |
1131 | 1132 | return obj
|
| 1133 | + elif remote is None and obj: |
| 1134 | + if obj.updated < util.as_utc(util.now() - OBJECT_REFRESH_AGE): |
| 1135 | + logger.debug(f' last updated {obj.updated}, refreshing') |
| 1136 | + else: |
| 1137 | + return obj |
1132 | 1138 |
|
1133 | 1139 | if obj:
|
| 1140 | + orig_as1 = obj.as1 |
1134 | 1141 | obj.clear()
|
1135 | 1142 | obj.new = False
|
1136 | 1143 | else:
|
1137 | 1144 | obj = Object(id=id)
|
1138 | 1145 | if local:
|
1139 |
| - logger.info(' not in datastore') |
| 1146 | + logger.debug(' not in datastore') |
1140 | 1147 | obj.new = True
|
1141 | 1148 | obj.changed = False
|
1142 | 1149 |
|
|
0 commit comments