Skip to content

Commit

Permalink
Restore and test DictCache
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprengere committed Jan 6, 2025
1 parent 7806d05 commit 5034243
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def view():
### Cache example

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.
Here is an example of how to configure Flask-Compress with caching using Flask-Caching.
Here is an example of how to configure Flask-Compress with caching using [Flask-Caching](https://pypi.org/project/Flask-Caching/).
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.

```python
Expand Down Expand Up @@ -122,6 +122,8 @@ compress.cache = cache
compress.cache_key = get_cache_key
```

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

## Options

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.
Expand Down
4 changes: 2 additions & 2 deletions flask_compress/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .flask_compress import Compress
from .flask_compress import Compress, DictCache

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


__all__ = ("Compress",)
__all__ = ("Compress", "DictCache")
12 changes: 12 additions & 0 deletions flask_compress/flask_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
from flask import after_this_request, current_app, request


class DictCache:

def __init__(self):
self.data = {}

def get(self, key):
return self.data.get(key)

def set(self, key, value):
self.data[key] = value


@lru_cache(maxsize=128)
def _choose_algorithm(enabled_algorithms, accept_encoding):
"""
Expand Down
42 changes: 41 additions & 1 deletion tests/test_flask_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flask import Flask, render_template
from flask_caching import Cache

from flask_compress import Compress
from flask_compress import Compress, DictCache
from flask_compress.flask_compress import _choose_algorithm


Expand Down Expand Up @@ -476,6 +476,7 @@ def test_disabled_stream(self):

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

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


class DictCacheTests(unittest.TestCase):
def setUp(self):
# We keep track of the number of times the cache key function is called
self.cache_key_calls = 0
self.app = Flask(__name__)
self.app.testing = True

def get_cache_key(request):
self.cache_key_calls += 1
return request.url

compress = Compress()
compress.init_app(self.app)

compress.cache = DictCache()
compress.cache_key = get_cache_key

@self.app.route("/route/")
def view():
return render_template("large.html")

def test_compression(self):
client = self.app.test_client()

headers = [("Accept-Encoding", "deflate")]
response = client.get("/route/", headers=headers)
self.assertEqual(response.status_code, 200)
self.assertIn("Content-Encoding", response.headers)
self.assertEqual(response.headers.get("Content-Encoding"), "deflate")
self.assertEqual(self.cache_key_calls, 1)

headers = [("Accept-Encoding", "deflate")]
response = client.get("/route/", headers=headers)
self.assertEqual(response.status_code, 200)
self.assertIn("Content-Encoding", response.headers)
self.assertEqual(response.headers.get("Content-Encoding"), "deflate")
self.assertEqual(self.cache_key_calls, 2)


if __name__ == "__main__":
unittest.main()

0 comments on commit 5034243

Please sign in to comment.