diff --git a/flask_compress/flask_compress.py b/flask_compress/flask_compress.py index 11edbee..e67fefd 100644 --- a/flask_compress/flask_compress.py +++ b/flask_compress/flask_compress.py @@ -5,6 +5,7 @@ import functools import zlib from collections import defaultdict +from functools import lru_cache from gzip import GzipFile from io import BytesIO @@ -17,6 +18,7 @@ from flask import after_this_request, current_app, request +@lru_cache(maxsize=128) def _choose_algorithm(enabled_algorithms, accept_encoding): """ Determine which compression algorithm we're going to use based on the @@ -24,7 +26,7 @@ def _choose_algorithm(enabled_algorithms, accept_encoding): algorithms, together with a "quality factor" for each one (higher quality means the client prefers that algorithm more). - :param enabled_algorithms: List of supported compression algorithms + :param enabled_algorithms: Tuple of supported compression algorithms :param accept_encoding: Content of the `Accept-Encoding` header :return: name of a compression algorithm (`gzip`, `deflate`, `br`, 'zstd') or `None` if the client and server don't agree on any. @@ -167,9 +169,9 @@ def init_app(self, app): algo = app.config["COMPRESS_ALGORITHM"] if isinstance(algo, str): - self.enabled_algorithms = [i.strip() for i in algo.split(",")] + self.enabled_algorithms = tuple(i.strip() for i in algo.split(",")) else: - self.enabled_algorithms = list(algo) + self.enabled_algorithms = tuple(algo) if app.config["COMPRESS_REGISTER"] and app.config["COMPRESS_MIMETYPES"]: app.after_request(self.after_request) diff --git a/tests/test_flask_compress.py b/tests/test_flask_compress.py index afe199e..c054a65 100644 --- a/tests/test_flask_compress.py +++ b/tests/test_flask_compress.py @@ -260,19 +260,19 @@ def test_setting_compress_algorithm_simple_string(self): This is a backwards-compatibility test.""" self.app.config["COMPRESS_ALGORITHM"] = "gzip" c = Compress(self.app) - self.assertListEqual(c.enabled_algorithms, ["gzip"]) + self.assertTupleEqual(c.enabled_algorithms, ("gzip",)) def test_setting_compress_algorithm_cs_string(self): """Test that `COMPRESS_ALGORITHM` can be a comma-separated string""" self.app.config["COMPRESS_ALGORITHM"] = "gzip, br, zstd" c = Compress(self.app) - self.assertListEqual(c.enabled_algorithms, ["gzip", "br", "zstd"]) + self.assertTupleEqual(c.enabled_algorithms, ("gzip", "br", "zstd")) def test_setting_compress_algorithm_list(self): """Test that `COMPRESS_ALGORITHM` can be a list of strings""" self.app.config["COMPRESS_ALGORITHM"] = ["gzip", "br", "deflate"] c = Compress(self.app) - self.assertListEqual(c.enabled_algorithms, ["gzip", "br", "deflate"]) + self.assertTupleEqual(c.enabled_algorithms, ("gzip", "br", "deflate")) def test_one_algo_supported(self): """Tests requesting a single supported compression algorithm"""