Skip to content

Commit 05c06eb

Browse files
authored
Try to optimize xmlrpc further (#17714)
* move from json to orjson for xmlrpc caching * refactor dict_comprehension to a dict( call
1 parent c5c1921 commit 05c06eb

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

warehouse/legacy/api/xmlrpc/cache/derivers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13-
import json
13+
import orjson
1414

1515
from warehouse.legacy.api.xmlrpc.cache import interfaces
1616

@@ -31,7 +31,7 @@ def wrapper_view(context, request):
3131
except LookupError:
3232
return view(context, request)
3333
try:
34-
key = json.dumps(request.rpc_args[slice_obj])
34+
key = orjson.dumps(request.rpc_args[slice_obj])
3535
_tag = tag
3636
if arg_index is not None:
3737
_tag = tag % (tag_processor(str(request.rpc_args[arg_index])))

warehouse/legacy/api/xmlrpc/cache/fncache.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13-
import json
14-
13+
import orjson
1514
import redis
1615

1716
from warehouse.legacy.api.xmlrpc.cache.interfaces import CacheError
@@ -27,7 +26,7 @@ def increment(self, metric_name):
2726
class RedisLru:
2827
"""
2928
Redis backed LRU cache for functions which return an object which
30-
can survive json.dumps() and json.loads() intact
29+
can survive orjson.dumps() and orjson.loads() intact
3130
"""
3231

3332
def __init__(self, conn, name="lru", expires=None, metric_reporter=None):
@@ -58,14 +57,16 @@ def get(self, func_name, key, tag):
5857
return None
5958
if value:
6059
self.metric_reporter.increment(f"{self.name}.cache.hit")
61-
value = json.loads(value)
60+
value = orjson.loads(value)
6261
return value
6362

6463
def add(self, func_name, key, value, tag, expires):
6564
try:
6665
self.metric_reporter.increment(f"{self.name}.cache.miss")
6766
pipeline = self.conn.pipeline()
68-
pipeline.hset(self.format_key(func_name, tag), str(key), json.dumps(value))
67+
pipeline.hset(
68+
self.format_key(func_name, tag), str(key), orjson.dumps(value)
69+
)
6970
ttl = expires if expires else self.expires
7071
pipeline.expire(self.format_key(func_name, tag), ttl)
7172
pipeline.execute()

warehouse/legacy/api/xmlrpc/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ def changelog_since_serial(request, serial: StrictInt):
269269

270270
@xmlrpc_cache_all_projects(method="list_packages_with_serial")
271271
def list_packages_with_serial(request):
272-
serials = request.db.query(Project.name, Project.last_serial).all()
273-
return {serial[0]: serial[1] for serial in serials}
272+
package_serial_tuples = request.db.query(Project.name, Project.last_serial).all()
273+
return dict(package_serial_tuples)
274274

275275

276276
# Package querying methods

0 commit comments

Comments
 (0)