Skip to content

Commit 5034243

Browse files
committed
Restore and test DictCache
1 parent 7806d05 commit 5034243

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def view():
9292
### Cache example
9393

9494
Flask-Compress can be integrated with caching mechanisms to serve compressed responses directly from the cache. This can significantly reduce server load and response times.
95-
Here is an example of how to configure Flask-Compress with caching using Flask-Caching.
95+
Here is an example of how to configure Flask-Compress with caching using [Flask-Caching](https://pypi.org/project/Flask-Caching/).
9696
The example demonstrates how to create a simple cache instance with a 1-hour timeout, and use it to cache compressed responses for incoming requests.
9797

9898
```python
@@ -122,6 +122,8 @@ compress.cache = cache
122122
compress.cache_key = get_cache_key
123123
```
124124

125+
If you do not want to pull an external dependency, you can use a simple in-memory cache using `compress.cache = flask_compress.DictCache()`.
126+
125127
## Options
126128

127129
Within your Flask application's settings you can provide the following settings to control the behavior of Flask-Compress. None of the settings are required.

flask_compress/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .flask_compress import Compress
1+
from .flask_compress import Compress, DictCache
22

33
# _version.py is generated by setuptools_scm when building the package.
44
# It is not version-controlled, so if it is missing, this likely means that
@@ -9,4 +9,4 @@
99
__version__ = "0"
1010

1111

12-
__all__ = ("Compress",)
12+
__all__ = ("Compress", "DictCache")

flask_compress/flask_compress.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
from flask import after_this_request, current_app, request
1919

2020

21+
class DictCache:
22+
23+
def __init__(self):
24+
self.data = {}
25+
26+
def get(self, key):
27+
return self.data.get(key)
28+
29+
def set(self, key, value):
30+
self.data[key] = value
31+
32+
2133
@lru_cache(maxsize=128)
2234
def _choose_algorithm(enabled_algorithms, accept_encoding):
2335
"""

tests/test_flask_compress.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from flask import Flask, render_template
77
from flask_caching import Cache
88

9-
from flask_compress import Compress
9+
from flask_compress import Compress, DictCache
1010
from flask_compress.flask_compress import _choose_algorithm
1111

1212

@@ -476,6 +476,7 @@ def test_disabled_stream(self):
476476

477477
class CachingCompressionTests(unittest.TestCase):
478478
def setUp(self):
479+
# We keep track of the number of times the view is called
479480
self.view_calls = 0
480481
self.tmpdir = tempfile.TemporaryDirectory()
481482

@@ -530,5 +531,44 @@ def test_compression(self):
530531
_ = gzip.decompress(response.data)
531532

532533

534+
class DictCacheTests(unittest.TestCase):
535+
def setUp(self):
536+
# We keep track of the number of times the cache key function is called
537+
self.cache_key_calls = 0
538+
self.app = Flask(__name__)
539+
self.app.testing = True
540+
541+
def get_cache_key(request):
542+
self.cache_key_calls += 1
543+
return request.url
544+
545+
compress = Compress()
546+
compress.init_app(self.app)
547+
548+
compress.cache = DictCache()
549+
compress.cache_key = get_cache_key
550+
551+
@self.app.route("/route/")
552+
def view():
553+
return render_template("large.html")
554+
555+
def test_compression(self):
556+
client = self.app.test_client()
557+
558+
headers = [("Accept-Encoding", "deflate")]
559+
response = client.get("/route/", headers=headers)
560+
self.assertEqual(response.status_code, 200)
561+
self.assertIn("Content-Encoding", response.headers)
562+
self.assertEqual(response.headers.get("Content-Encoding"), "deflate")
563+
self.assertEqual(self.cache_key_calls, 1)
564+
565+
headers = [("Accept-Encoding", "deflate")]
566+
response = client.get("/route/", headers=headers)
567+
self.assertEqual(response.status_code, 200)
568+
self.assertIn("Content-Encoding", response.headers)
569+
self.assertEqual(response.headers.get("Content-Encoding"), "deflate")
570+
self.assertEqual(self.cache_key_calls, 2)
571+
572+
533573
if __name__ == "__main__":
534574
unittest.main()

0 commit comments

Comments
 (0)