Skip to content

Commit

Permalink
Cache the results of _choose_algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprengere committed Oct 14, 2024
1 parent e0fad02 commit f0c06c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions flask_compress/flask_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,14 +18,15 @@
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
client request. The `Accept-Encoding` header may list one or more desired
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.
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_flask_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit f0c06c8

Please sign in to comment.