From 947a459b8ae1e8e69492441c209f03a09c349edd Mon Sep 17 00:00:00 2001 From: Raul Castro Date: Fri, 26 Dec 2025 02:06:35 -0300 Subject: [PATCH 1/2] feat: redis cache --- api.py | 24 ++++++++++++++++++++++++ requirements.txt | 2 ++ 2 files changed, 26 insertions(+) diff --git a/api.py b/api.py index be21c74..31e31be 100644 --- a/api.py +++ b/api.py @@ -4,6 +4,7 @@ from dotenv import load_dotenv import os from controllers import genai, brapi +from flask_caching import Cache load_dotenv() cors_origin = os.getenv('CORS_ORIGIN', '*') @@ -11,6 +12,23 @@ cors=CORS(api, resources={r"/*": {"origins": cors_origin}}) api.config['CORS_HEADERS'] = 'Content-Type' +# Cache Configuration +is_prod = os.getenv('FLASK_ENV') == 'production' +if is_prod: + cache_config = { + 'CACHE_TYPE': 'RedisCache', + 'CACHE_REDIS_HOST': os.getenv('REDIS_HOST'), + 'CACHE_REDIS_PORT': os.getenv('REDIS_PORT'), + 'CACHE_REDIS_PASSWORD': os.getenv('REDIS_PASSWORD') + } +else: + cache_config = { + 'CACHE_TYPE': 'NullCache' + } + +api.config.from_mapping(cache_config) +cache = Cache(api) + def require_api_key(f): @wraps(f) def decorated_function(*args, **kwargs): @@ -63,6 +81,7 @@ def ticket_resume_show(name): @api.route('/brapi/quote/') @cross_origin() @require_api_key +@cache.cached(timeout=300) def brapi_quote_show(name): result = brapi.Brapi.get_stock_data(name) if result: @@ -72,6 +91,7 @@ def brapi_quote_show(name): @api.route('/brapi/async_quote/') @cross_origin() @require_api_key +@cache.cached(timeout=300) async def brapi_async_quote_show(name): result = await brapi.Brapi.get_async_stock_data(name) if result: @@ -81,6 +101,7 @@ async def brapi_async_quote_show(name): @api.route('/brapi/sync_quote/') @cross_origin() @require_api_key +@cache.cached(timeout=300) def brapi_sync_quote_show(name): result = brapi.Brapi.get_sync_stock_data(name) if result: @@ -90,6 +111,7 @@ def brapi_sync_quote_show(name): @api.route('/brapi/sync_quote_list') @cross_origin() @require_api_key +@cache.cached(timeout=300) def brapi_sync_quote_list_show(): result = brapi.Brapi.get_sync_stock_data_list() if result: @@ -99,6 +121,7 @@ def brapi_sync_quote_list_show(): @api.route('/brapi/sync_quote_by_sector/') @cross_origin() @require_api_key +@cache.cached(timeout=300) def brapi_sync_quote_by_sector_show(sector): result = brapi.Brapi.get_sync_stock_data_list_by_sector(sector) if result: @@ -109,6 +132,7 @@ def brapi_sync_quote_by_sector_show(sector): @api.route('/brapi/sync_quote_list_sectors') @cross_origin() @require_api_key +@cache.cached(timeout=300) def brapi_sync_quote_list_sectors_show(): result = brapi.Brapi.get_available_sectors_list() if result: diff --git a/requirements.txt b/requirements.txt index e776251..2445ec8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -63,3 +63,5 @@ zipp==3.7.0 zope.event==5.0 zope.interface==6.4.post2 brapi==1.0.0 +Flask-Caching==2.3.0 +redis==5.0.1 From f84361dc00bca1f6938a3dc2bf20ee2ce245d303 Mon Sep 17 00:00:00 2001 From: Raul Castro Date: Fri, 26 Dec 2025 02:07:59 -0300 Subject: [PATCH 2/2] fix: update readme --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 2265905..27801a5 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,15 @@ To run this project, you will need to add the following environment variables to | `BRAPI_TOKEN` | Token for accessing Brapi.dev API | Yes | - | | `CLIENT_API_KEY` | Secret key to authenticate clients accessing this API | Yes | - | | `CORS_ORIGIN` | Allowed origin for CORS | No | `*` | +| `FLASK_ENV` | Environment mode (`production` enables Redis) | No | `development` | +| `REDIS_HOST` | Redis Hostname (Required if Prod) | No | - | +| `REDIS_PORT` | Redis Port (Required if Prod) | No | - | +| `REDIS_PASSWORD` | Redis Password (Required if Prod) | No | - | + +### Caching + +This application uses **Redis** for caching in **production** environments (`FLASK_ENV=production`). +In **development** mode (default), caching is disabled (`NullCache`) to facilitate debugging. ### Setting up variables @@ -40,6 +49,12 @@ GENAI_API_KEY=your_genai_key BRAPI_TOKEN=your_brapi_token CLIENT_API_KEY=your_client_secret_key CORS_ORIGIN=http://localhost:3000 + +# Redis (Production only) +FLASK_ENV=production +REDIS_HOST=mx.redis-server.com +REDIS_PORT=6379 +REDIS_PASSWORD=secret ``` Or export them in your terminal: