Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Added documentation on how to use Flask-Compress with caching.
  • Loading branch information
DaveGUI authored and alexprengere committed Mar 23, 2023
1 parent 746ca9d commit 8ebd19a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ def view():
pass
```

### 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. 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
# Initializing flask app
app = Flask(__name__)
cache = Cache(app, config={
'CACHE_TYPE': 'simple',
'CACHE_DEFAULT_TIMEOUT': 60*60 # 1 hour cache timeout
})

# Define a function to return cache key for incoming requests
def get_cache_key(request):
return request.url

# Initialize Flask-Compress
compress = Compress()
compress.init_app(app)

# Set up cache for compressed responses
compress.cache = cache
compress.cache_key = get_cache_key
'''

## 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

0 comments on commit 8ebd19a

Please sign in to comment.