Problem with Flask-Caching/ Redis

I'm trying to setup a simple caching mecanism to my Plotly Dash (Flask) application deployed to railway. I'm not being able to setup it correctly. Is this the correct implementation? Project ID: eda27a88-d8f4-495f-b917-1fd2269f5764
No description
56 Replies
Percy
Percy15mo ago
Project ID: eda27a88-d8f4-495f-b917-1fd2269f5764
Brody
Brody15mo ago
please setup a MONGO_URL and a REDIS_URL reference variable in your service variables https://docs.railway.app/develop/variables#reference-variables once done please provide a screenshot of your service variables
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Ok.
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Something like this?
No description
Brody
Brody15mo ago
please use reference variables, remove them, and have a look at the docs section I linked
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
My mongo DB must be in the same project?
No description
Brody
Brody15mo ago
yes it does need to be
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
I apologize for my knowledge level.
No description
Brody
Brody15mo ago
no worries, that looks good now now use those variables in your code when connecting to the database
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
ok. I should use like this: variable = ${{MongoDB.MONGO_URL}} or variable = MONGO_URL of course option 1, good?
Brody
Brody15mo ago
neither of those options are python related I think for python it's os.getenv() but definitely read the docs for that
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
I will do it. Thanks for you fast and incredible help, really impressed by this support. It looks like its working. Any way I can assure the redis caching is working?
Brody
Brody15mo ago
do something in your app that would put something in the cache, then use the data tab of redis to see if you have any cache entries
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Unfortunately no logs and no data saved.
Brody
Brody15mo ago
and you dont have any database connection errors?
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Only a warning on redis db log
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
No description
Brody
Brody15mo ago
im talking about in your own service
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
I'm getting no errors in my service (https://heatmapteste2-production.up.railway.app/)
Brody
Brody15mo ago
make sure you are either logging a success message or an error message for your database connections
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
No description
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Looks good
Brody
Brody15mo ago
can you show me the database connection code?
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
No description
Brody
Brody15mo ago
you are still hard coding database variables, please use environment variables
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
i will try to comment line 21
Brody
Brody15mo ago
and you also aren't logging any success or error messages for the database connections
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
No description
Brody
Brody15mo ago
okay thats looking much better
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Here is the updated code logging sucess or error mesages. And the variables arent hard coded
Brody
Brody15mo ago
well done
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Thanks for your advice.
Brody
Brody15mo ago
can i assume you have added the new variables to the service variables? can i see a screenshot?
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
No description
Brody
Brody15mo ago
looking good and you arent getting any errors in the deployment logs?
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
all good
No description
Brody
Brody15mo ago
how do you run this app locally?
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
I didnt tried to run locally with the caching mechanism I would have to change the cache_type to something local aswell
Brody
Brody15mo ago
you can still use the railway redis and mongo databases locally install the railway cli https://docs.railway.app/develop/cli#installation once you have that all setup and your service linked you can run railway run <the command you use to start the app locally> this will run the given command with the railway service variables available note: do not use any .env files for this, the variables in the .env file will overwrite the variables injected by railway run this way you could work out any bugs in your code locally without having to deploy to railway for every change
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Ok, tks for advice I will run it
Brody
Brody15mo ago
👍
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Damn, I got some data on Redis running locally with railway CLI
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
No description
Brody
Brody15mo ago
awsome, now go to your railway services settings and disable private networking
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
done
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
No description
Brody
Brody15mo ago
now do more stuff with the railway hosted app and see if you get more data in redis
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
yeah thats working means its solved... i believe
Brody
Brody15mo ago
awsome!
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
Brody, you are a monster... I wish I had 10% of your knowledge. Awesome job! Kiss on your cheek and my regards from Brazil. I can't be thankfull enought. So its a good practice to build using railway CLI and railway env variables
Brody
Brody15mo ago
it sure is, now you have no need for an .env file since you can safely store everything in your service variables
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
stonks are a monster of computing
Brody
Brody15mo ago
thank you 🙂
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
sorry to bother again, but why private networking was blocking my redis db?
Brody
Brody15mo ago
good question, private networking takes 2 seconds to initialize and if your database connection timeout isnt set high enough funky things can happen, you wernt using private networking so the easiest option was simply disabling it
BIGHOUSE OG
BIGHOUSE OGOP15mo ago
tks! you would do a great teacher. You can consider this ticket closed. Tks a lot again.
Want results from more Discord servers?
Add your server