Skip to content

Commit f0c06c8

Browse files
committed
Cache the results of _choose_algorithm
1 parent e0fad02 commit f0c06c8

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

flask_compress/flask_compress.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import functools
66
import zlib
77
from collections import defaultdict
8+
from functools import lru_cache
89
from gzip import GzipFile
910
from io import BytesIO
1011

@@ -17,14 +18,15 @@
1718
from flask import after_this_request, current_app, request
1819

1920

21+
@lru_cache(maxsize=128)
2022
def _choose_algorithm(enabled_algorithms, accept_encoding):
2123
"""
2224
Determine which compression algorithm we're going to use based on the
2325
client request. The `Accept-Encoding` header may list one or more desired
2426
algorithms, together with a "quality factor" for each one (higher quality
2527
means the client prefers that algorithm more).
2628
27-
:param enabled_algorithms: List of supported compression algorithms
29+
:param enabled_algorithms: Tuple of supported compression algorithms
2830
:param accept_encoding: Content of the `Accept-Encoding` header
2931
:return: name of a compression algorithm (`gzip`, `deflate`, `br`, 'zstd')
3032
or `None` if the client and server don't agree on any.
@@ -167,9 +169,9 @@ def init_app(self, app):
167169

168170
algo = app.config["COMPRESS_ALGORITHM"]
169171
if isinstance(algo, str):
170-
self.enabled_algorithms = [i.strip() for i in algo.split(",")]
172+
self.enabled_algorithms = tuple(i.strip() for i in algo.split(","))
171173
else:
172-
self.enabled_algorithms = list(algo)
174+
self.enabled_algorithms = tuple(algo)
173175

174176
if app.config["COMPRESS_REGISTER"] and app.config["COMPRESS_MIMETYPES"]:
175177
app.after_request(self.after_request)

tests/test_flask_compress.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,19 @@ def test_setting_compress_algorithm_simple_string(self):
260260
This is a backwards-compatibility test."""
261261
self.app.config["COMPRESS_ALGORITHM"] = "gzip"
262262
c = Compress(self.app)
263-
self.assertListEqual(c.enabled_algorithms, ["gzip"])
263+
self.assertTupleEqual(c.enabled_algorithms, ("gzip",))
264264

265265
def test_setting_compress_algorithm_cs_string(self):
266266
"""Test that `COMPRESS_ALGORITHM` can be a comma-separated string"""
267267
self.app.config["COMPRESS_ALGORITHM"] = "gzip, br, zstd"
268268
c = Compress(self.app)
269-
self.assertListEqual(c.enabled_algorithms, ["gzip", "br", "zstd"])
269+
self.assertTupleEqual(c.enabled_algorithms, ("gzip", "br", "zstd"))
270270

271271
def test_setting_compress_algorithm_list(self):
272272
"""Test that `COMPRESS_ALGORITHM` can be a list of strings"""
273273
self.app.config["COMPRESS_ALGORITHM"] = ["gzip", "br", "deflate"]
274274
c = Compress(self.app)
275-
self.assertListEqual(c.enabled_algorithms, ["gzip", "br", "deflate"])
275+
self.assertTupleEqual(c.enabled_algorithms, ("gzip", "br", "deflate"))
276276

277277
def test_one_algo_supported(self):
278278
"""Tests requesting a single supported compression algorithm"""

0 commit comments

Comments
 (0)