How to cache remote URLs in Flask

Published on Aug. 22, 2023, 12:16 p.m.

To cache remote URLs in Flask, you can use the Flask-Caching extension, which provides caching support for Flask applications. Here’s an example code that demonstrates how to cache a remote URL using Flask-Caching:

from flask import Flask, request
from flask_caching import Cache
import requests

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})

@app.route('/')
@cache.cached(timeout=300, key_prefix='remote_url_cache')
def remote_url():
    url = request.args.get('url')
    if url:
        response = requests.get(url)
        return response.content
    else:
        return 'No URL provided.'

if __name__ == '__main__':
    app.run()

In this code, we import the Flask-Caching extension and initialize it with the Flask app. We then define a route that takes a url parameter in the query string. We use the cache.cached() decorator to cache the response of the route for 300 seconds (5 minutes), and set the key_prefix to remote_url_cache to differentiate it from other cache keys. If the url parameter is provided, we use the requests library to fetch the resource from the remote URL and return the response content. Otherwise, we return a message indicating that no URL was provided.

http://127.0.0.1:5000/?url=https%3A//cn.bing.com

Note that this example uses the simple caching type, which stores the cache in the Flask application’s memory. You can use other caching types supported by Flask-Caching, such as redis, memcached, or filesystem, depending on your caching needs.

To encode a URL in Python

To encode a URL in Python using the quote() function from the urllib.parse module. Here’s an example code that shows how to do that:

from urllib.parse import quote

url = 'https://www.example.com/search?q=python tutorials'
encoded_url = quote(url)

print(encoded_url)

In this code, we have a sample URL that contains spaces in the query string. We use the quote() function to encode the URL, and then print the encoded URL.

The output of this code will be https%3A//www.example.com/search%3Fq%3Dpython%20tutorials, which is the encoded URL with all the special characters correctly encoded.