├── .env_example ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── docker-compose.yml ├── requirements.txt └── src ├── __init__.py ├── app.py └── config.py /.env_example: -------------------------------------------------------------------------------- 1 | CACHE_TYPE=redis 2 | CACHE_REDIS_HOST=redis 3 | CACHE_REDIS_PORT=6379 4 | CACHE_REDIS_DB=0 5 | CACHE_REDIS_URL=redis://redis:6379/0 6 | CACHE_DEFAULT_TIMEOUT=500 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .env 3 | .idea/** 4 | __pycache__/** -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.9.6-slim-buster 2 | 3 | # Set the working directory inside the container 4 | WORKDIR /app 5 | 6 | # Copy requirements from local to docker image 7 | COPY requirements.txt /app 8 | 9 | # Install the dependencies in the docker image 10 | RUN pip3 install -r requirements.txt --no-cache-dir 11 | 12 | # Copy everything from the current dir to the image 13 | COPY ./src . 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 Valon Januzaj 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flask-cache-redis 2 | 3 | ## Installation & Usage 4 | 5 | To use this project, start by cloning the repository: 6 | ```bash 7 | git clone https://github.com/vjanz/flask-cache-redis.git 8 | ``` 9 | cd into the project, and make a copy of `.env_example` to `.env` 10 | ```bash 11 | $ cp .env_example .env 12 | ``` 13 | 14 | ### Build and run the stack 15 | You can run the application stack (Flask application and Redis) with Docker, respectively docker-compose. 16 | ```docker 17 | docker-compose up -d --build 18 | ``` 19 | 20 | #### Remove the stack 21 | ```docker 22 | docker-compose down -v 23 | ``` 24 | 25 | ### Test the application (API) 26 | We can use `curl` to make requests to our API. There is one endpoint `/universities`, so let's test that out. 27 | 28 | ```curl 29 | curl localhost:5000/universities?country=Germany 30 | ``` 31 | 32 | ### License 33 | This project is licensed under the terms of the MIT license. 34 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | api: 4 | container_name: flask-container 5 | build: . 6 | entrypoint: python app.py 7 | env_file: 8 | - .env 9 | ports: 10 | - '5000:5000' 11 | 12 | redis: 13 | image: redis 14 | container_name: redis-container 15 | ports: 16 | - "6379:6379" -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2021.5.30 2 | chardet==4.0.0 3 | click==8.0.1 4 | colorama==0.4.4 5 | Flask==1.1.2 6 | Flask-Caching==1.10.1 7 | idna==2.10 8 | isort==5.8.0 9 | itsdangerous==2.0.1 10 | Jinja2==3.0.1 11 | MarkupSafe==2.0.1 12 | redis==3.5.3 13 | requests==2.25.1 14 | urllib3==1.26.5 15 | Werkzeug==2.0.1 16 | -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vjanz/flask-cache-redis/af8cfc05097273389fa5f2e496cb4447ff1a0ceb/src/__init__.py -------------------------------------------------------------------------------- /src/app.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from flask import Flask, jsonify, request 3 | from flask_caching import Cache # Import Cache from flask_caching module 4 | 5 | app = Flask(__name__) 6 | app.config.from_object('config.Config') # Set the configuration variables to the flask application 7 | cache = Cache(app) # Initialize Cache 8 | 9 | 10 | @app.route("/universities") 11 | @cache.cached(timeout=30, query_string=True) 12 | def get_universities(): 13 | API_URL = "http://universities.hipolabs.com/search?country=" 14 | search = request.args.get('country') 15 | r = requests.get(f"{API_URL}{search}") 16 | return jsonify(r.json()) 17 | 18 | 19 | if __name__ == "__main__": 20 | app.run(debug=True, host="0.0.0.0", port=5000) 21 | -------------------------------------------------------------------------------- /src/config.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | class Config(object): 5 | CACHE_TYPE = os.environ['CACHE_TYPE'] 6 | CACHE_REDIS_HOST = os.environ['CACHE_REDIS_HOST'] 7 | CACHE_REDIS_PORT = os.environ['CACHE_REDIS_PORT'] 8 | CACHE_REDIS_DB = os.environ['CACHE_REDIS_DB'] 9 | CACHE_REDIS_URL = os.environ['CACHE_REDIS_URL'] 10 | CACHE_DEFAULT_TIMEOUT = os.environ['CACHE_DEFAULT_TIMEOUT'] 11 | --------------------------------------------------------------------------------