|
5 | 5 | import functools
|
6 | 6 | import zlib
|
7 | 7 | from collections import defaultdict
|
| 8 | +from functools import lru_cache |
8 | 9 | from gzip import GzipFile
|
9 | 10 | from io import BytesIO
|
10 | 11 |
|
|
17 | 18 | from flask import after_this_request, current_app, request
|
18 | 19 |
|
19 | 20 |
|
| 21 | +@lru_cache(maxsize=128) |
20 | 22 | def _choose_algorithm(enabled_algorithms, accept_encoding):
|
21 | 23 | """
|
22 | 24 | Determine which compression algorithm we're going to use based on the
|
23 | 25 | client request. The `Accept-Encoding` header may list one or more desired
|
24 | 26 | algorithms, together with a "quality factor" for each one (higher quality
|
25 | 27 | means the client prefers that algorithm more).
|
26 | 28 |
|
27 |
| - :param enabled_algorithms: List of supported compression algorithms |
| 29 | + :param enabled_algorithms: Tuple of supported compression algorithms |
28 | 30 | :param accept_encoding: Content of the `Accept-Encoding` header
|
29 | 31 | :return: name of a compression algorithm (`gzip`, `deflate`, `br`, 'zstd')
|
30 | 32 | or `None` if the client and server don't agree on any.
|
@@ -167,9 +169,9 @@ def init_app(self, app):
|
167 | 169 |
|
168 | 170 | algo = app.config["COMPRESS_ALGORITHM"]
|
169 | 171 | 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(",")) |
171 | 173 | else:
|
172 |
| - self.enabled_algorithms = list(algo) |
| 174 | + self.enabled_algorithms = tuple(algo) |
173 | 175 |
|
174 | 176 | if app.config["COMPRESS_REGISTER"] and app.config["COMPRESS_MIMETYPES"]:
|
175 | 177 | app.after_request(self.after_request)
|
|
0 commit comments